← Questions

Remaining path length at every waypoint of a batch of trajectories

0
TeslaCodingLast asked Feb 2026

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 — points has shape [B, N, 2]; rem has shape [B, N] with rem[b][i] = sum over j from i to N-2 of ||points[b][j+1] - points[b][j]|| and rem[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. With N = 1 every 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: diff along 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 = 1 and 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 N with 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding