HTTP Request Redirection
Learn this problemProblem statement
Amazon engineers are investigating an HTTP request that is redirected among servers.
There are n servers on an infinite two-dimensional plane. The coordinates of server i are given by locations[i] = [x, y]. The request starts at locations[0], and that server is marked as visited.
Each value in redirectRecords specifies one redirect direction from the current server (a, b). In every formula below, Z is an arbitrary positive integer:
- Direction
1:(a, b) -> (a + Z, b + Z). - Direction
2:(a, b) -> (a + Z, b - Z). - Direction
3:(a, b) -> (a - Z, b + Z). - Direction
4:(a, b) -> (a - Z, b - Z).
Process the redirect records in order. For each record, redirect the request to the nearest server in the specified direction that has not previously been visited. If no eligible server exists in that direction, skip that redirect. Whenever the request reaches a server, mark it as visited.
Return the coordinates [x, y] of the server holding the request after all redirect records have been processed.
Function
findFinalServer(locations: int[][], redirectRecords: int[]) → int[]Examples
Example 1
locations = [[3,4],[1,2],[7,8],[5,6]]redirectRecords = [1,4]return = [1,2]The request starts at [3, 4]. Direction 1 points toward both [5, 6] and [7, 8], so the nearest unvisited server is [5, 6].
Direction 4 points back toward [3, 4] and then [1, 2]. Because [3, 4] has already been visited, the request moves to [1, 2].