Implement a Datacenter Request Router
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates command parsing, stateful data structure management, input validation, geospatial computation using the Haversine formula, deterministic sorting/tie-breaking, and capacity-based routing logic.
Constraints
- 0 <= len(commands) <= 2000
- Datacenter names are non-empty strings without spaces
- Latitude and longitude values may be integers or decimals
- Capacity is intended to be an integer and must be greater than 0
- At most 2000 datacenters will be registered
Examples
Input: []
Expected Output: []
Explanation: No commands means no outputs.
Input: ["REGISTER us-west 38 -122 2", "REGISTER us-east 41 -74 3", "REGISTER us-west 50 -100 1", "REGISTER invalid-node 91 0 100", "REGISTER invalid-cap 0 0 0", "SET_HEALTHY us-east false", "SET_HEALTHY fake-node true"]
Expected Output: ["OK", "OK", "ERROR", "ERROR", "ERROR", "OK", "ERROR"]
Explanation: This checks successful registration, duplicate names, invalid coordinates, invalid capacity, updating an existing datacenter, and failing to update a missing one.
Input: ["DISTANCE 0 0 0 0", "DISTANCE 0 0 90 0", "DISTANCE 0 0 0 180", "DISTANCE 91 0 0 0"]
Expected Output: ["0", "10008", "20015", "ERROR"]
Explanation: Same point is 0 km, equator to pole is a quarter of Earth's circumference, opposite points on the equator are half the circumference, and invalid coordinates produce ERROR.
Input: ["REGISTER node-B 0 0 1", "REGISTER node-A 0 0 1", "REGISTER node-C 10 10 100", "SET_HEALTHY node-C false", "ROUTE 0 0", "ROUTE 0 0", "ROUTE 0 0"]
Expected Output: ["OK", "OK", "OK", "OK", "node-A 0 node-A,node-B", "node-B 0 node-A,node-B", "None node-A,node-B"]
Explanation: node-A and node-B are tied at distance 0, so lexicographic order breaks the tie. After both reach capacity 1, routing returns None along with the healthy sorted list.
Input: ["REGISTER gamma 0 0 1", "REGISTER alpha 0 180 1", "REGISTER beta 90 0 1", "ROUTE 0 0", "ROUTE 0 0", "ROUTE 95 0", "SET_HEALTHY gamma false", "ROUTE 0 0"]
Expected Output: ["OK", "OK", "OK", "gamma 0 gamma,beta,alpha", "beta 10008 gamma,beta,alpha", "ERROR", "OK", "alpha 20015 beta,alpha"]
Explanation: The first route chooses the closest node, the second skips a full but still healthy node, invalid user coordinates return ERROR, and after gamma is marked unhealthy only beta and alpha remain in sorted order.
Input: ["REGISTER solo 0 0 1", "SET_HEALTHY solo false", "ROUTE 0 0"]
Expected Output: ["OK", "OK", "None"]
Explanation: When there are no healthy datacenters, ROUTE returns exactly None.
Hints
- Use a hash map keyed by datacenter name so REGISTER, duplicate checks, and SET_HEALTHY updates are O(1).
- For each ROUTE command, build a list of tuples like (exact_distance, name), sort it once, and then scan for the first datacenter that still has remaining capacity.