Request Routing System
Learn this problemProblem statement
Complete the function below. The function receives the full standard input as a single string and returns the exact standard output lines for a request-routing command processor.
Problem
You are building a request routing system for datacenters. The system supports commands that register datacenters, update their health, compute geographic distance, and route user requests to the nearest available healthy datacenter.
Each datacenter has a unique name, latitude, longitude, capacity, health status, and current load. A newly registered datacenter is healthy and has load 0.
Supported commands:
REGISTER name latitude longitude capacity: add a datacenter. ReturnOKif the name is new, latitude is in[-90, 90], longitude is in[-180, 180], and capacity is greater than0; otherwise returnERROR.SET_HEALTHY name value: set health totrueorfalse. ReturnOKfor an existing datacenter and valid boolean value; otherwise returnERROR.DISTANCE lat1 lon1 lat2 lon2: compute the great-circle distance in kilometers using Earth radius6371and the Haversine formula. Return the nearest integer distance. ReturnERRORfor invalid coordinates.ROUTE latitude longitude: among healthy datacenters, sort by distance to the user ascending, breaking ties by datacenter name. Choose the first datacenter whose current load is less than capacity, increment its load by1, and outputname distance candidates. If no healthy datacenter has capacity left, outputNone candidates.candidatesis the comma-separated ordered list of healthy datacenter names considered by the router.
Process commands in order and return one output line per command.
Function
solveRequestRoutingSystem(input: String) → String[]Complete solveRequestRoutingSystem. It has one parameter, String input, containing newline-separated commands. Return the stdout payload as an array of lines, without trailing newline characters.
Examples
Example 1
input = "REGISTER us-west 38 -122 100\nREGISTER us-east 41 -74 150\nREGISTER us-west 50 -100 50\nREGISTER invalid-node 91 0 100\nREGISTER invalid-cap 0 0 0\nSET_HEALTHY us-east false\nSET_HEALTHY fake-node true"return = ["OK","OK","ERROR","ERROR","ERROR","OK","ERROR"]The duplicate datacenter name, invalid latitude, invalid capacity, and unknown datacenter update are rejected.
Example 2
input = "DISTANCE 38 -122 41 -74\nDISTANCE 0 0 0 0\nDISTANCE 91 0 0 0"return = ["4080","0","ERROR"]The first distance is rounded to the nearest kilometer; the last command has an invalid latitude.
Example 3
input = "REGISTER node-A 0 0 1\nREGISTER node-B 0 0 1\nREGISTER node-C 10 10 100\nSET_HEALTHY node-C false\nROUTE 0 0\nROUTE 0 0\nROUTE 0 0"return = ["OK","OK","OK","OK","node-A 0 node-A,node-B","node-B 0 node-A,node-B","None node-A,node-B"]The unhealthy node-C is not considered. The two healthy nodes are selected once each, then both are at capacity.
Constraints
Coordinate validation follows the exact bounds stated in the prompt.
Capacity must be a positive integer.
Distances use Earth radius 6371 km and are rounded to the nearest integer.
More Stripe problems
- Deployment Window SchedulerOA · Seen Jul 2026
- Directly Linked UsersOA · Seen Jun 2026
- Fraud Ring SizeOA · Seen Jun 2026
- Risky Fraud RingOA · Seen Jun 2026
- Account Balance Manager Part 3 - Platform CoverageONSITE INTERVIEW · Seen Jun 2026
- BitFont Part 3 - Decode Run-Length-Encoded RowsONSITE INTERVIEW · Seen Jun 2026
- Record Linkage Part 3 - Full Connected ComponentPHONE SCREEN · Seen Jun 2026
- Shipping Cost Calculator Part 3 - Mixed Fixed/Incremental TiersONSITE INTERVIEW · Seen Jun 2026