Compute suffix sums over waypoints
TeslaMachine Learning Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to compute geometric suffix sums over batched 2D trajectories, testing skills in array manipulation, Euclidean distance computation, and runtime-conscious algorithm design within the Coding & Algorithms domain.
Problem
You are given a batch of 2D waypoint trajectories.
- Input:
pointswith shape [B, N, 2], wherepoints[b][i] = (x, y)is the i-th waypoint for batch itemb. - Output:
remwith shape [B, N].
For each batch item b and waypoint index i, define rem[b][i] as the remaining path length from waypoint i to the end, computed as the suffix sum of Euclidean segment lengths:
[ rem[b][i] = \sum_{j=i}^{N-2} |points[b][j+1] - points[b][j]|_2 ]
Also define rem[b][N-1] = 0.
Requirements
- Time complexity: O(B·N).
- Handle edge cases such as
N = 1.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.