FastPrepFastPrep
Problem Brief

Request Routing System

FULLTIMEOA
See Stripe online assessment and hiring insights

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. Return OK if the name is new, latitude is in [-90, 90], longitude is in [-180, 180], and capacity is greater than 0; otherwise return ERROR.
  • SET_HEALTHY name value: set health to true or false. Return OK for an existing datacenter and valid boolean value; otherwise return ERROR.
  • DISTANCE lat1 lon1 lat2 lon2: compute the great-circle distance in kilometers using Earth radius 6371 and the Haversine formula. Return the nearest integer distance. Return ERROR for 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 by 1, and output name distance candidates. If no healthy datacenter has capacity left, output None candidates. candidates is 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 Description

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.

1Example 1

Input
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"
Output
["OK","OK","ERROR","ERROR","ERROR","OK","ERROR"]
Explanation

The duplicate datacenter name, invalid latitude, invalid capacity, and unknown datacenter update are rejected.

2Example 2

Input
input = "DISTANCE 38 -122 41 -74\nDISTANCE 0 0 0 0\nDISTANCE 91 0 0 0"
Output
["4080","0","ERROR"]
Explanation

The first distance is rounded to the nearest kilometer; the last command has an invalid latitude.

3Example 3

Input
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"
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

The unhealthy node-C is not considered. The two healthy nodes are selected once each, then both are at capacity.

Constraints

Limits and guarantees your solution can rely on.

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.

public String[] solveRequestRoutingSystem(String input) {
    // write your code here
}
Input

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"

Output

["OK", "OK", "ERROR", "ERROR", "ERROR", "OK", "ERROR"]

Sign in to submit your solution.