Google’s perception team keeps a small offline utility, merge_tracks, that chains per-frame detector boxes into (track_id, start_frame, end_frame) tracks so reviewers can scrub one row per object instead of one row per box. In this bug-hunt round you receive its 45 lines as-is. The function runs without errors, and on the team’s single-object smoke test its output matches the hand-labeled answer exactly.
On a 20-frame 1280×720 dashcam replay that was labeled frame by frame, the same function misbehaves (Figure 1). A van that stays in view the whole time comes back as four short tracks. A car that vanishes for two frames returns under its original id, although the docstring says that absence should split it. A pedestrian who walks in after every other object has left inherits an old id instead of getting a new one. When two detections in one frame both overlap the same track, swapping their order in the input changes which one keeps the id.
Find the 4 silent bugs, fix each one in place without restructuring the function, and prove the repair against a frame-by-frame oracle: IoU against an independent pixel count, an overlapping pair of cars, a 1-frame and a 2-frame gap at split_gap = 2, two same-frame contenders fed in both orders, and unique track ids.
Buggy Code (Find 4 Silent Bugs):
01 from collections import defaultdict
02
03 def iou(a, b):
04 """IoU of two (x, y, w, h) boxes; (x, y) is the top-left corner."""
05 ix = max(0.0, min(a[2], b[2]) - max(a[0], b[0]))
06 iy = max(0.0, min(a[3], b[3]) - max(a[1], b[1]))
07 inter = ix * iy
08 union = a[2] * a[3] + b[2] * b[3] - inter
09 return inter / union if union > 0 else 0.0
10
11 def merge_tracks(dets, iou_min=0.5, split_gap=2):
12 """Chain (frame, (x, y, w, h)) detections into sorted (id, start, end).
13 Highest-IoU pairs match first and need IoU >= iou_min. An unmatched
14 detection opens a new track id. A track ends once split_gap or more
15 consecutive frames pass without a detection for it."""
16 by_frame = defaultdict(list)
17 for frame, box in dets:
18 by_frame[frame].append(box)
19 active, finished, next_id = [], [], 0
20 for frame, boxes in sorted(by_frame.items()):
21 for track in list(active):
22 if frame - track["end"] - 1 > split_gap:
23 active.remove(track)
24 finished.append(track)
25 pairs = sorted(((iou(track["box"], box), det_i, trk_i)
26 for det_i, box in enumerate(boxes)
27 for trk_i, track in enumerate(active)),
28 key=lambda pair: pair[0], reverse=True)
29 used_dets, used_tracks = set(), set()
30 for score, det_i, trk_i in pairs:
31 if det_i in used_dets or trk_i in used_tracks:
32 continue
33 used_dets.add(det_i)
34 used_tracks.add(trk_i)
35 active[trk_i]["end"], active[trk_i]["box"] = frame, boxes[det_i]
36 if score < iou_min:
37 break
38 for det_i, box in enumerate(boxes):
39 if det_i in used_dets:
40 continue
41 track = finished.pop() if finished else {"id": next_id, "start": frame}
42 next_id = max(next_id, track["id"] + 1)
43 track["end"], track["box"] = frame, box
44 active.append(track)
45 return sorted((track["id"], track["start"], track["end"]) for track in active + finished)
Figure 1: on the labeled replay clip, one output id swallows three different objects and one van is spread across four ids, while the single-object smoke test still passes.
1Inspection Checklist (Where to Look)
(1) Box Contract: Write down what each index of a box tuple means under the docstring’s (x, y, w, h) format, then confirm every expression that reads a box agrees with it.
(2) Acceptance Floor: Trace a frame whose best remaining pair scores exactly 0.0 and confirm that no track’s end frame moves.
(3) Identity Contract: Follow one detection that matches nothing all the way to the output and ask whether its id could already exist.
(4) Gap Boundary: Test the boundary case where the number of missed frames equals split_gap exactly, against the docstring’s “or more” wording.
2Visual Review Map (Figure 2)

Figure 2: The six stages of merge_tracks in execution order. Each “?” stage carries a docstring contract to verify line by line before you edit anything.
3Interviewer Nudge & Line Focus
Walk each stage and answer the Socratic check before editing:
iou((100, 50, 40, 30), (100, 50, 40, 30)), must score exactly 1.0. Evaluate the helper by hand: what does each of the two overlap lengths come out to, and why?
split_gap = 2, a car last seen at frame 4 reappears at frame 7. How many frames were missed, and according to the docstring should its track still be live when frame 7 is scored?
Leave a Reply