Katkat

Register Data Centers and Route to the Nearest Healthy Region

StripeSoftware Engineer · Online Assessment

Last updated

Implement a stateful command processor to register data centers, update their operational health, and route incoming requests to the nearest healthy data center using the Haversine distance formula.

Problem Statement

You are tasked with building a routing engine for a globally distributed cloud platform. The engine maintains data center registrations and their operational health states, and routes incoming traffic to the nearest healthy data center using great-circle distance (the Haversine formula).

You must implement a function process_commands(commands: list[str]) -> list[str] that processes commands sequentially. Each command produces exactly one line of output. If a command fails validation, the state must not be modified, and it must output ERROR.

Commands

  1. REGISTER

    • Registers a new data center with identifier `` at coordinates (, ).
    • Newly registered data centers default to health status UP.
    • Validation: must be non-empty, contain only alphanumeric characters, and not already be registered. must be a valid float in [-90.0, 90.0]. `` must be a valid float in [-180.0, 180.0].
    • Output: OK on success, ERROR on failure.
  2. SET_STATUS

    • Updates the health status of data center ``.
    • `` must be either UP or DOWN.
    • Validation: must exist and must be valid.
    • Output: OK on success, ERROR on failure.
  3. ROUTE

    • Finds the nearest data center with status UP from the query coordinates (, ) using Haversine distance.
    • Validation: must be in `[-90.0, 90.0]`, must be in [-180.0, 180.0], and there must be at least one data center with status UP.
    • Output: of the closest healthy data center. In case of a distance tie, pick the one with the lexicographically smallest. If validation fails, output ERROR.

Distance Formula

Given two coordinates (lat1, lon1) and (lat2, lon2) in degrees, convert angles to radians:

dlat = (lat2 - lat1) * pi / 180
dlon = (lon2 - lon1) * pi / 180
a = sin^2(dlat / 2) + cos(lat1 * pi / 180) * cos(lat2 * pi / 180) * sin^2(dlon / 2)
distance = 2 * R * asin(sqrt(a))

Use Earth's radius R = 6371.0 km.

Example

commands = [
    "REGISTER us-east 39.0438 -77.4874",
    "REGISTER eu-west 53.3498 -6.2603",
    "REGISTER us-east 40.0 -74.0",      # Duplicate ID
    "ROUTE 40.7128 -74.0060",          # NYC -> us-east is closer
    "SET_STATUS us-east DOWN",
    "ROUTE 40.7128 -74.0060",          # us-east is DOWN -> routes to eu-west
    "SET_STATUS eu-west DOWN",
    "ROUTE 40.7128 -74.0060"           # No UP data centers -> ERROR
]

# Output:
# ["OK", "OK", "ERROR", "us-east", "OK", "eu-west", "OK", "ERROR"]

Constraints

  • Total number of commands: 1 <= len(commands) <= 5000.
  • Number of unique data centers: <= 1000.
  • Any malformed command or unknown command name must output ERROR without throwing an unhandled exception.

Hints

Hint 1

Validate all parameters (token counts, float conversion, boundary checks) before mutating any internal state so failed commands leave no side effects.

Hint 2

Remember to convert degrees to radians before applying trigonometric functions for the Haversine formula.

Hint 3

When selecting the nearest data center during a tie, include the data center ID in your sorting or min-comparison key as a secondary criterion.

Follow-up questions

  • How would you optimize the `ROUTE` query if the number of data centers grew to millions and latency needed to be sub-millisecond?
  • How would you extend this design to support atomic batch transactions where multiple commands must all succeed or all roll back?
  • How would you incorporate capacity and current load so that traffic does not overwhelm the single closest healthy data center?