Distance to the nearest waypoint along a route
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 Aug 2025. It tests geometry, binary-search and prefix sums.
You are writing part of a car's navigation software. A route is a polyline: an ordered list of 2D waypoints coordinates, connected by straight segments from each waypoint to the next. The vehicle starts at coordinates[0] and drives along the route.
You are also given a list distances. Each value is the total distance driven since the start. For each value:
- Find the vehicle's exact position on the route after driving that far (interpolate along the segment it is on).
- Return the Euclidean distance from that position to the closest waypoint anywhere on the route, not only the two ends of the current segment.
Implement:
def nearest_checkpoint_distances(
coordinates: list[tuple[float, float]],
distances: list[float],
) -> list[float]: ...
Rules
-
1 <= len(coordinates) <= 10^5,1 <= len(distances) <= 10^5. Coordinates are floats. -
distancesis not necessarily sorted. - A distance at or beyond the route's total length puts the vehicle on the last waypoint.
- A negative distance is invalid. Raise an error.
- Consecutive waypoints may be identical (zero-length segments).
- Answers are compared with a tolerance of
1e-6.
Example 1
coordinates = [(0, 0), (4, 0), (4, 3)]
distances = [0, 1, 2, 5, 7, 100]
output = [0, 1, 2, 1, 0, 0]
After 5 units you have driven the 4-unit first segment and 1 unit up the second, so you are at (4, 1), which is 1 away from (4, 0). 100 is past the end (length 7), so you are on (4, 3).
Example 2
coordinates = [(0, 0), (10, 0), (10, 1), (-10, 1)]
distances = [21]
output = [1]
After 21 units you are at (0, 1) on the last segment. Both ends of that segment are 10 away, but the first waypoint (0, 0) is only 1 away.
Aim for better than checking every waypoint for every query when both lists are large.
Clarifying questions
- Is the output a list of floats in the same order as
distances? - With a single waypoint, is every answer
0? - Does a distance that lands exactly on a waypoint count as that waypoint (answer
0)? - How large are coordinate values, and does precision matter beyond the
1e-6tolerance?
What a strong answer covers
- Builds cumulative distances along the route once; for each query, binary searches the segment and interpolates the position, skipping zero-length segments.
- Clamps distances past the total length to the last waypoint and raises on negative ones.
- Finds the nearest waypoint with a spatial index (a k-d tree, or a uniform grid of buckets),
O(log n)per query on average, instead ofO(n)per query. - Notes that the nearest waypoint can be far along the route but close in space (example 2).
- Tests both examples, repeated waypoints, a distance of exactly the total length, and a brute-force comparison on random routes.
Follow-ups
- Sort the queries and sweep the route once instead of binary searching each.
- Return the nearest waypoint's index as well as the distance.
- Measure "nearest" by distance along the route instead of straight-line distance.
Where it's asked
- Tesla · Technical phone screen · Last asked Aug 202545 min · 10 to 20 minutes on your background, then one compact coding, debugging or design task in CoderPad that must run
What it tests
geometry · binary-search · prefix sums · spatial index
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
