Route a request along diagonal server redirects
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests linked list, hashing and geometry.
Servers sit at distinct integer points on an infinite 2D grid. locations[i] = [x, y] is the position of server i. A request starts at server 0, which counts as visited.
You are given a list redirect_records of directions, each from 1 to 4. From the current server at (a, b), a direction picks a diagonal ray:
| Direction | Ray (for some Z >= 1) |
|---|---|
| 1 |
(a + Z, b + Z) (northeast) |
| 2 |
(a + Z, b - Z) (southeast) |
| 3 |
(a - Z, b + Z) (northwest) |
| 4 |
(a - Z, b - Z) (southwest) |
For each direction in order, move the request to the unvisited server on that ray with the smallest Z, and mark it visited. If the ray has no unvisited server, skip the instruction and stay where you are.
Implement final_server(locations, redirect_records) -> [x, y], which returns the position of the server the request ends on.
Constraints: 1 <= n <= 10^5, 0 <= len(redirect_records) <= 10^5, coordinates in [-10^9, 10^9], all positions distinct. Aim for better than O(n * m).
Example 1
locations = [[0,0],[2,2],[5,5],[3,-3],[1,1],[-2,2]]
redirect_records = [1, 1, 1, 4]
Northeast goes to (1,1), then (2,2), then (5,5). Going southwest from (5,5), every server on that diagonal is visited, so the instruction is skipped. Answer: [5, 5].
Example 2
Same locations, redirect_records = [3, 2]. Northwest from (0,0) reaches (-2,2). Southeast from there passes the visited (0,0) and stops at (3,-3). Answer: [3, -3].
Clarifying questions
- Does the ray include only
Z >= 1, so the current server is never a candidate? - Is the instruction skipped (not the whole route ended) when no unvisited server is on the ray?
- Can redirect values outside 1 to 4 appear?
What a strong answer covers
- Groups servers by diagonal:
x - yfor directions 1 and 4,x + yfor directions 2 and 3, each group sorted byx. - Finds the nearest unvisited server along a ray by moving to the next or previous position in the sorted group, skipping visited ones in near O(1) amortised with 'next unvisited' pointers (union-find style), or a sorted set per diagonal with deletions.
- Total time O(n log n + m log n) or near-linear, against O(n * m) for scanning.
- Marks a server visited in both of its diagonals, so it is skipped from either direction (Example 2 passes the visited origin).
- Tests a skipped instruction, passing over visited servers, empty
redirect_records, and a single server.
Follow-ups
- Return the full path of visited servers, not just the last one.
- Add the four straight directions (rows and columns) as well as diagonals.
- Let servers go offline and come back between instructions.
Where it's asked
What it tests
linked list · hashing · geometry · simulation
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.
