FastPrepRequest Routing System

Request Routing System

Stripe logoStripe● MediumFULLTIMECONTRACTOA
Learn

Problem statement

You are building a request-routing command processor for datacenters. The function receives the complete newline-separated input as one string and returns one output string per command.

Each registered datacenter has a unique name, latitude, longitude, positive integer capacity, health status, and current load. A new datacenter is healthy and has load 0.

The supported commands are:

  • REGISTER name latitude longitude capacity: return OK and add the datacenter when the name is new, the latitude is in [-90, 90], the longitude is in [-180, 180], and capacity is positive. Otherwise return ERROR.
  • SET_HEALTHY name value: for an existing datacenter, set its health from a case-insensitive true or false value and return OK. Otherwise return ERROR.
  • DISTANCE lat1 lon1 lat2 lon2: compute the Haversine great-circle distance with Earth radius 6371 km and return the nearest integer, or ERROR for invalid coordinates.
  • ROUTE latitude longitude: sort healthy datacenters by exact distance, breaking an exact tie by name. The candidate list contains all healthy names in that order. Choose the first candidate whose load is below capacity, increment its load, and return name rounded-distance candidates.

When healthy candidates exist but all are full, return None candidates. When there are no healthy candidates, return exactly None with no trailing space.

Function

solveRequestRoutingSystem(input: String) → String[]

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

  • Latitude is in [-90, 90] and longitude is in [-180, 180].
  • Capacity is a positive integer.
  • SET_HEALTHY accepts case-insensitive forms of true and false.
  • Distances use Earth radius 6371 km and are rounded only for display.
  • Routing order uses exact distance, then datacenter name for an exact tie.
  • No healthy candidates produce exactly None.

More Stripe problems

See Stripe hiring insights
public String[] solveRequestRoutingSystem(String input) {
    // write your code here
}
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"
expected["OK", "OK", "ERROR", "ERROR", "ERROR", "OK", "ERROR"]
Checking account…