Remaining path length at every waypoint of a batch of trajectories
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Tesla's technical phone screen, last asked Feb 2026. It tests numpy, tensors and prefix sums.
You get a batch of 2D trajectories. Each trajectory is a list of N waypoints (x, y). For every waypoint, compute the remaining path length: the sum of the straight-line distances between consecutive waypoints from that waypoint to the end of its trajectory. The last waypoint has 0 left to travel.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
remaining_length(points) -> rem—pointshas shape[B, N, 2];remhas shape[B, N]withrem[b][i] = sum over j from i to N-2 of ||points[b][j+1] - points[b][j]||andrem[b][N-1] = 0. - Work in NumPy or PyTorch. Do not loop over waypoints in Python.
Example
points = [[(0, 0), (3, 4), (6, 8)]]
-> [[10.0, 5.0, 0.0]] # segments 5 and 5
points = [[(0, 0), (0, 3), (4, 3), (4, 0)],
[(1, 1), (4, 5), (7, 9), (7, 9)]]
-> [[10.0, 7.0, 3.0, 0.0], # segments 3, 4, 3
[10.0, 5.0, 0.0, 0.0]] # segments 5, 5, 0 (the last two points are the same)
Constraints
-
B >= 0,N >= 1. WithN = 1every value is 0. - O(B·N) time.
Clarifying questions
- Can trajectories in a batch have different lengths (padding with a mask)?
- Float32 or float64? How much rounding error is acceptable on long trajectories?
- Should it be differentiable (PyTorch, gradients through the distances)?
What a strong answer covers
- Segment lengths with one vectorized difference:
diffalong the waypoint axis, then the norm of the last axis, giving shape[B, N-1]. - A reverse cumulative sum (flip,
cumsum, flip) and a zero appended at the end. - Handling
N = 1and an empty batch without special loops. - The gradient of the norm at a zero-length segment (repeated points): the NaN it can cause in PyTorch and how to avoid it.
- A test against a plain loop on random input.
Follow-ups
- Trajectories are padded to the same
Nwith a validity mask. Return the remaining length that ignores padding. - Return the waypoint index where the remaining length first drops below a threshold.
- Compute the remaining travel time given a speed per segment.
Where it's asked
- Tesla · Technical phone screen · Last asked Feb 202645 min · 10 to 20 minutes on your background, then one compact coding, debugging or design task in CoderPad that must run
What it tests
numpy · tensors · prefix sums · vectorization
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
