Compute nearest index within threshold after walking distances
Company: Tesla
Role: Machine Learning Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: Medium
Interview Round: Technical Screen
You are given:
(
1) points: a list of N 2D coordinates in miles, points[i] = [x_i, y_i], ordered;
(
2) distances: a list of M nonnegative floats (miles); and
(
3) a threshold t > 0 (miles). Starting at points[0], walk along the polyline connecting points[0] -> points[1] -> ... -> points[N-1]. For each step value d in distances, advance exactly d miles along this polyline from the current position, continuing across segments as needed; if the step would pass the final vertex, clamp the position to points[N-1]. After each step, determine whether there exists any index j such that the Euclidean distance between the current position and points[j] is <= t. If such points exist, output the index j of the nearest point (break ties by choosing the smallest index); otherwise output None. Return a list of length M with these results. Implement an efficient NumPy-based solution that handles large N and M (e.g., up to 1e
5), using cumulative segment lengths and within-segment interpolation; avoid naive O(N*M) scanning when possible. Specify time and space complexity and include tests for edge cases (d = 0, repeated points, zero-length segments, empty distances).
Quick Answer: This question evaluates a candidate's proficiency in data manipulation, numerical computing with NumPy, spatial reasoning for Euclidean distance and interpolation along polylines, and designing time- and space-efficient algorithms for large-scale inputs.