Request Routing System
Updated
Problem statement
You are building a command processor that routes requests to datacenters. You are given an array of commands, commands. Each entry is one command, and you run the commands in the order they appear. Return an array with one result string for each command. A blank command is the one exception: it gives no result.
Each registered datacenter has a unique name, a latitude, a longitude, a positive whole-number capacity, a health status and a current load. A new datacenter is healthy and has load 0.
The commands are:
REGISTER name latitude longitude capacity: if the name is new, the latitude is in[-90, 90], the longitude is in[-180, 180], and the capacity is positive, add the datacenter and giveOK. Otherwise giveERROR.SET_HEALTHY name value: if the datacenter exists andvalueistrueorfalsein any letter case, set its health to that value and giveOK. Otherwise giveERROR.DISTANCE lat1 lon1 lat2 lon2: work out the Haversine great-circle distance between the two points, using Earth radius6371km, and give it rounded to the nearest whole number. GiveERRORif a coordinate is not valid.ROUTE latitude longitude: sort the healthy datacenters by their exact distance from this point. If two distances are exactly equal, the smaller name comes first. The candidate list is all the healthy names in that order, joined with commas and no spaces. Choose the first candidate whose load is below its capacity, add1to its load, and givename rounded-distance candidates, separated by single spaces.
If there are healthy datacenters but all are full, give None candidates. If there are no healthy datacenters, give exactly None, with no space after it.
Input checks
A command that breaks one of these rules gives ERROR and changes nothing:
- The command name must match exactly, in uppercase.
registerandPINGare not commands. - A command must have exactly the fields listed above, no more and no fewer.
- A latitude or longitude must be a plain decimal number: an optional leading
-, then digits, then optionally a.and more digits. So38,-74.0and007are numbers.abc,+5,.5,5.,1e2andNaNare not. - A latitude or longitude must be in range in every command, including
DISTANCEandROUTE. - A capacity must be written with digits only and be from
1to1000000000. So1.0,+5and1000000001giveERROR.
Some commands are fine as written. A blank command, which is empty or has only spaces, is not a command: skip it and give no result. Fields are separated by one or more spaces, and spaces at the start or end of a command are ignored.
A name can be any field. Names are case-sensitive, and an exact distance tie compares them by character code, so Z comes before a.
For example, take REGISTER a 0 0 1, REGISTER b 0 0 1, ROUTE 0 0, ROUTE 0 0, ROUTE 0 0. Both datacenters are at distance 0, so the name a comes first. The first ROUTE picks a, which is then full. The second picks b. The third finds both full. The answer is ["OK", "OK", "a 0 a,b", "b 0 a,b", "None a,b"].
Function
routeRequests(commands: String[]) → String[]Examples
Example 1
commands = ["REGISTER us-west 38 -122 100", "REGISTER us-east 41 -74 150", "REGISTER us-west 50 -100 50", "REGISTER invalid-node 91 0 100", "REGISTER invalid-cap 0 0 0", "SET_HEALTHY us-east false", "SET_HEALTHY fake-node true"]return = ["OK", "OK", "ERROR", "ERROR", "ERROR", "OK", "ERROR"]There are seven commands, so the answer has seven results. The first two REGISTER commands add us-west and us-east, so they give OK. The third REGISTER reuses the name us-west, so it gives ERROR. The fourth has latitude 91, which is out of range, and the fifth has capacity 0, so both give ERROR. SET_HEALTHY us-east false works on an existing datacenter and gives OK. SET_HEALTHY fake-node true names a datacenter that does not exist, so it gives ERROR. The answer is ["OK", "OK", "ERROR", "ERROR", "ERROR", "OK", "ERROR"].
Example 2
commands = ["DISTANCE 38 -122 41 -74", "DISTANCE 0 0 0 0", "DISTANCE 91 0 0 0"]return = ["4080", "0", "ERROR"]The distance from (38, -122) to (41, -74) is about 4080 km, which is already rounded to a whole number. Between a point and itself the distance is 0. The last command has latitude 91, which is out of range, so it gives ERROR. The answer is ["4080", "0", "ERROR"].
Example 3
commands = ["REGISTER node-A 0 0 1", "REGISTER node-B 0 0 1", "REGISTER node-C 10 10 100", "SET_HEALTHY node-C false", "ROUTE 0 0", "ROUTE 0 0", "ROUTE 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"]node-C is set to unhealthy, so it is never a candidate. node-A and node-B are both at distance 0 with capacity 1, so the name decides: the first ROUTE picks node-A and the second picks node-B. The third ROUTE finds both full, so it gives None with the candidate list. The answer is ["OK", "OK", "OK", "OK", "node-A 0 node-A,node-B", "node-B 0 node-A,node-B", "None node-A,node-B"].
Example 4
commands = ["register a 0 0 1", "REGISTER a abc 0 1", "REGISTER a 0 0 1.5", "REGISTER a 0 0", "", "REGISTER a 1.5 -2 1", "ROUTE 91 0", "DISTANCE 0 0 0", "PING", "ROUTE 1.5 -2"]return = ["ERROR", "ERROR", "ERROR", "ERROR", "OK", "ERROR", "ERROR", "ERROR", "a 0 a"]The first command has the wrong letter case, so it gives ERROR. The next three give ERROR too: the latitude abc is not a number, the capacity 1.5 is not a whole number, and the fourth has a missing field. None of them takes the name a. The blank command gives no result. The next REGISTER works, because decimals are fine for coordinates. ROUTE 91 0 is out of range, DISTANCE 0 0 0 has too few fields, and PING is not a command, so those three give ERROR. The last command routes to a at distance 0. The answer is ["ERROR", "ERROR", "ERROR", "ERROR", "OK", "ERROR", "ERROR", "ERROR", "a 0 a"].
Example 5
commands = ["REGISTER us-west 38 -122 100", "REGISTER us-east 41 -74 150", "REGISTER eu-west 52 0 200", "SET_HEALTHY us-east false", "REGISTER us-west 50 -100 50", "REGISTER invalid 91 0 100", "REGISTER invalid2 0 0 0"]return = ["OK", "OK", "OK", "OK", "ERROR", "ERROR", "ERROR"]The first three commands register three different datacenters with valid coordinates and positive capacities, so they give OK. The fourth marks the existing us-east unhealthy and gives OK. Reusing the name us-west, using latitude 91, and using capacity 0 each give ERROR. The answer is ["OK", "OK", "OK", "OK", "ERROR", "ERROR", "ERROR"].
Constraints
- Latitude is in
[-90, 90]and longitude is in[-180, 180]. - Capacity is a whole number from
1to1000000000. SET_HEALTHYaccepts case-insensitive forms oftrueandfalse.- Distances use Earth radius
6371km and are rounded only for display. - Routing order uses exact distance, then datacenter name for an exact tie.
- No healthy candidates give exactly
None. - Together, the commands have at most
20000characters, counting one extra character between each two commands. - The commands hold only printable ASCII characters (codes 32 to 126), and every field has at most
15characters.
More Stripe Online Assessment questions
- Deployment Window SchedulerOA · Seen Nov 2026
- Subscription and Usage-Based Billing CalculatorOA · Seen Nov 2026
- Split Tender Part 4 - RefundsOA · PHONE SCREEN · Seen Nov 2026
- Split Tender Part 3 - SurchargesOA · Seen Nov 2026
- Split Tender Part 2 - Expiration DatesOA · Seen Nov 2026
- Split Tender Part 1 - Basic Split TenderingOA · Seen Nov 2026
- Asynchronous Event ProcessingOA · Seen Nov 2026
- Payment Partner RoutingOA · Seen Nov 2026
- Financial Account LedgerOA · PHONE SCREEN · Seen Nov 2026
- Maximum Game Score with Fixed Forward JumpsOA · Seen Nov 2026
Similar questions at other companies
- HTTP Request RedirectionAmazon · OA
- Light Source Hits: Sample, Compare, and FitOpenAI · PHONE SCREENOpenAI interview questions
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
- Build Sessions from Unordered Events (Part 1)Scale AI · OAScale AI interview questions
- Sorted Cyclic Shift Difference SumsCapital One · OACapital One interview questions
- Track Received Byte RangesRoblox · OARoblox interview questions
- Clean the TapeMillennium · OAMillennium interview questions
- Temporal Key-Field Database with Prefix ScansThe Trade Desk · OA