Today's Focus
Friday is the fixed slot for Coding, narrowed to the ML-flavored implementation problems that show up most: batch inference, tokenizers, data processing, numpy. These rounds aren't checking whether you can call a library function — they're checking whether you know what's happening underneath it, and whether you can turn "the batch dimension isn't a clean rectangle" into tensor operations instead of a loop.
Today's pick is a vectorization problem that perception and prediction teams like to ask: given a batch of multi-modal trajectory predictions, compute minADE and minFDE without a single Python for-loop. It tests two things at once — can you write clean NumPy broadcasting, and do you actually understand what the metric is scoring. Getting the loop-free code to run is the baseline; being able to explain why minADE/minFDE takes a minimum over K candidates, and where that metric falls short of what a downstream planner actually needs, is what separates candidates here.
Core Concepts, Fast
minADE/minFDE: multi-modal prediction only penalizes the closest miss
For a single trajectory, ADE (Average Displacement Error) is the mean L2 distance between predicted and ground-truth positions across every timestep, and FDE (Final Displacement Error) looks only at the last timestep. A multi-modal model doesn't output one trajectory — it outputs K candidates (think of a car approaching an intersection: going straight, turning left, and turning right are all plausible futures). Scoring a single prediction against the one future that happened would unfairly punish a model for correctly hedging across other plausible outcomes. The standard fix takes the minimum over K: minADE = min_k ADE_k, minFDE = min_k FDE_k — as long as one of the K candidates lands close to what actually happened, the model isn't penalized for the other candidates being wrong. Argoverse reports minADE₆/minFDE₆ (6 modes), nuScenes reports minADE₅/minFDE₅ (5 modes) — same logic, different mode counts.
The core of NumPy vectorization: let broadcasting absorb three nested loops into one tensor op
The intuitive approach is three nested loops: for every scene, every candidate, every timestep, compute a distance. Vectorizing means aligning shapes so NumPy pushes the looping down into its own compiled code. pred has shape (B, K, T, 2), gt has shape (B, T, 2) — broadcast gt to (B, 1, T, 2), subtract it directly from pred to get a (B, K, T, 2) displacement tensor, then take the L2 norm over the last axis to get a (B, K, T) per-step distance in one shot. There's no Python-level loop anywhere in that chain; all three dimensions are handled by broadcasting.
Variable-length ground truth: multiply by zero and divide by the valid count, don't filter and re-stack
Real motion datasets don't guarantee every scene has valid ground truth at every future timestep — an agent can leave the sensor's range early, get occluded, or have its track terminated partway through. That comes with a valid_mask of shape (B, T) marking which steps are trustworthy. The catch is every scene has a different number of valid steps, so if you filter out the invalid ones with boolean indexing, each scene ends up a different length and you can no longer stack them back into a rectangular batch tensor — vectorization breaks right there. The fix goes the other way: never drop anything, multiply the invalid steps' distance by zero, sum along the time axis, then divide by that scene's actual valid count (valid_mask.sum(axis=-1), guarding against division by zero). The tensor shape never changes; only the denominator shifts per scene. FDE's "last step" can't be hardcoded to index -1 either — reverse the mask along the time axis and use argmax to find each scene's actual last valid timestep.
This is an offline training metric — and it falls short of what a downstream planner needs
minADE/minFDE scores "how close is the best of K candidates to what actually happened," which pushes a model's candidate set to cover the true outcome — but it never requires the model to know which candidate is more likely. A model that scatters its probability mass randomly, but happens to have one candidate land on the right spot, still gets a low minADE. A planner making safety-critical decisions needs calibrated per-mode probabilities (Brier-minFDE adds (1-p)² to the score specifically to penalize uncalibrated confidence) — not just "one of the guesses was right." Volunteering this gap when pressed gets you further than stopping at "the NumPy is correct."
- Broadcast gt along the K dimension and subtract it directly from the K candidate trajectories at every timestep, then take the L2 norm — one shot gets a (B,K,T) per-step distance tensor, with no for-loop over B, K, or T.
- Multiply that distance by valid_mask and divide by each scene's valid-step count to get ADE. For FDE, reverse valid_mask along the time axis and use argmax to find each scene's actual last valid timestep, then index straight into the distance there — both are tensor operations along the T axis, still loop-free.
- ADE and FDE are both still shape (B,K); take the minimum over K to get each scene's minADE and minFDE, shape (B,) — multi-modal prediction only needs one candidate to land close to the true future to avoid being penalized for the rest.
Simplified for clarity: different K per scene and joint multi-agent prediction are omitted. The point is that all three stages — computing distance, applying the mask for ADE/FDE, and taking the min over K — run without a single Python loop.
Today's Practice Question
The Question
Given a batch of multi-modal trajectory predictions pred with shape (B, K, T, 2) (B scenes, K candidate future trajectories per scene, T timesteps per trajectory, 2D coordinates), the corresponding ground truth gt with shape (B, T, 2), and a per-timestep valid_mask with shape (B, T) (1 means that step's ground truth is trustworthy; 0 means the agent had already left sensor range or was occluded at that step), compute minADE and minFDE for the whole batch in NumPy. No Python for-loops are allowed over B, K, or T.
Source: Waymo (an onsite coding round for perception/prediction teams, leaning numerical/vectorized), from a publicly maintained company-by-company interview question bank (maintained by Outcome School, still being updated as of 2026). Difficulty: Advanced Round: Onsite coding round
How to Break It Down
- Clarify the problem first: Is K fixed across scenes, or does it vary (usually fixed — it's set by the decoder's number of heads)? Can
valid_maskbe entirely zero for a scene (the edge case of an agent that vanishes right after appearing — handle division by zero before dividing by the valid count)? Does "final" in FDE mean a fixed index-1, or "that scene's last valid timestep" (both definitions show up across datasets — ask; Waymo-style setups usually mean the latter)? Is the distance metric just L2? - Build a framework: Split the problem into three tensor operations — (a) broadcast-subtract to get per-candidate, per-timestep displacement distance, shape
(B,K,T); (b) applyvalid_maskby multiplying by zero and dividing by the valid count to get ADE, and useargmaxon the reversed mask to find the last valid step for FDE, both shape(B,K); (c) takeminalong the K axis to get(B,)minADE and minFDE. Everything is reshape, broadcasting, and axis-wise sum/min/argmax — no for-loops. - Go deep on the core: The most important trade-off has two parts. First, the mask has to be applied by "multiply by zero, then divide by the valid count," not by filtering with boolean indexing — because each scene has a different valid-step count, filtering produces ragged lengths that can't be re-stacked into a rectangular batch tensor, so only the multiply-sum-divide pattern keeps the whole thing vectorized. Second, FDE's "last step" has to come from
argmaxon the reversed mask, not a hardcoded index-1— tracing from "the first 1 in the reversed array" back to "the last 1 in the original" is the part that trips people up on a whiteboard. - Close strong: Emphasize that this question is really testing whether you can turn "the batch dimension and each scene's valid length aren't uniform" into tensor operations, not whether you can call
np.linalg.norm. Volunteer that minADE/minFDE is only an offline training metric, and that what a downstream planner actually needs — calibrated per-mode probabilities — is a separate thing the metric doesn't capture. That shows you understand where this sits in the perception-prediction-planning pipeline, rather than just stopping once the code runs.
A Sample Answer (how you'd actually say this in an interview)
I'd split this into "computing the distance" and "applying the mask" as two separate layers, because that's usually where people get stuck. For the distance: reshape or just let broadcasting expand
gtfrom(B,T,2)to(B,1,T,2), subtract it directly frompred's(B,K,T,2)— NumPy replicates the K dimension across scenes automatically — then take the L2 norm over the last axis to get a(B,K,T)per-step distance in one shot. No Python-level loop anywhere in that step; all three dimensions are handled underneath.The masking layer is where this question actually differentiates candidates. I wouldn't use boolean indexing to drop invalid steps, because each scene ends up a different length and you can't stack that back into a tensor. Instead, I'd multiply the distance by
valid_mask(broadcast over K), sum along the time axis, and divide by each scene's valid-step count fromvalid_mask.sum(axis=-1)to get ADE. FDE is trickier: hardcoding index-1breaks for scenes without a valid ground truth at the very last step, so I'd reverse the mask along the time axis, useargmaxto find where the first 1 shows up, and map that back to the actual last-valid-step index in the original order to pull out the right distance. Finally, both(B,K)tensors get aminover the K axis to produce(B,)minADE and minFDE. Taking the min across K makes sense because the real future is genuinely uncertain — if one of K candidates is right, the model shouldn't be penalized for the others being wrong — but I'd add that this metric doesn't require the model to know which candidate is more likely. A planner that actually has to drive needs calibrated per-mode probabilities, which is exactly what minADE/minFDE doesn't measure.
Self-Check
Use this table to check whether your answer covered the key points:
| Checklist item | Covered? |
|---|---|
| Explained that minADE/minFDE takes the minimum over K candidates, not plain ADE/FDE on a single prediction | |
Explained how broadcasting expands (B,T,2) gt against (B,K,T,2) pred to get (B,K,T) distance in one pass | |
| Explained the mask needs "multiply by zero, divide by valid count," not boolean-index filtering (which breaks the batch's rectangular shape) | |
| Explained FDE's "last step" needs argmax on a reversed mask, not a hardcoded index -1 | |
| Mentioned handling the division-by-zero edge case when a whole scene's mask is invalid | |
| Bonus: volunteered that minADE/minFDE is an offline metric with a gap against the calibrated probabilities a downstream planner needs |
Further Reading
- AI Engineering Interview Questions Company Wise — GitHub — today's question comes from the Waymo section of this bank, which also covers real interview questions from OpenAI, Anthropic, NVIDIA, Tesla, and 35 companies total
- ad-metrics: TRAJECTORY_PREDICTION.md — GitHub — a runnable reference implementation of minADE/minFDE, with the mode-count conventions used by Argoverse (minADE₆/minFDE₆) and nuScenes (minADE₅/minFDE₅)
- Towards trustworthy multi-modal motion prediction — arXiv — lines up Miss Rate, Brier-minADE/minFDE, and scene-level metrics side by side if you want to know what else gets measured besides minADE/minFDE
References
- AI Engineering Interview Questions Company Wise — GitHub — source for today's practice question; the Waymo section's Coding and Data Structures list includes "In NumPy, compute minADE and minFDE for multi-modal trajectory predictions with variable-length ground truth. No Python loops."
- Interaction Prediction — Waymo Open Dataset — source for "minADE/minFDE: multi-modal prediction only penalizes the closest miss"; the official formulas and definitions come from this page
- ad-metrics: TRAJECTORY_PREDICTION.md — GitHub — source for "The core of NumPy vectorization" and "Variable-length ground truth"; the
(K,T,2)tensor shape and Argoverse/nuScenes mode-count conventions come from this document - Towards trustworthy multi-modal motion prediction — arXiv — source for "This is an offline training metric"; the Brier-minADE/minFDE definition adding
(1-p)²for probability calibration comes from this paper