Register Data Centers and Route to the Nearest Healthy Region

Quick Overview

Process a deterministic command language for registering data centers, changing health, measuring Haversine distance, and routing to healthy regions, with one output and no partial mutation per command.

Register Data Centers and Route to the Nearest Healthy Region

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

## Problem Process commands for an in-memory registry of data centers. Each registered region has integer latitude, integer longitude, positive integer capacity, and a health flag that initially equals `true`. Supported commands are represented as arrays: ```text ["REGISTER", region, latitude, longitude, capacity] ["SET_HEALTHZ", region, healthy] ["DISTANCE", lat1, lon1, lat2, lon2] ["ROUTE", latitude, longitude] ``` Process every array independently and return exactly one output string for every input array, including malformed arrays and unsupported command names. A command has the exact arity shown above; otherwise its output is `ERROR`. - `REGISTER` returns `OK` only when `region` is valid and unregistered, both coordinates are valid integers in range, and `capacity` is a positive integer. Otherwise it returns `ERROR`. - `SET_HEALTHZ` returns `OK` only when `region` is valid and registered and `healthy` is exactly a Boolean. Otherwise it returns `ERROR`. `true` means healthy and `false` means unhealthy. - `DISTANCE` returns `ERROR` unless all four values are valid integer coordinates in range. For valid input, it returns the Haversine distance in kilometers, rounded with `floor(distance + 0.5)`. - `ROUTE` returns `ERROR` unless its latitude and longitude are valid integers in range. For valid input, it considers healthy regions only. Rank them by unrounded Haversine distance, then region name. Return `region roundedDistance candidates`, where `candidates` is every healthy region name in ranking order joined by commas. If none are healthy, return `NONE 0`. An `ERROR` command leaves the registry exactly unchanged. Validate the complete command before inserting a region or changing health, then continue with the next array. Use Earth radius `6371` kilometers and convert degrees to radians before applying: ```text a = sin²((lat2-lat1)/2) + cos(lat1) * cos(lat2) * sin²((lon2-lon1)/2) c = 2 * atan2(sqrt(a), sqrt(1-a)) distance = 6371 * c ``` ### Function Contract Implement `processDataCenterCommands(commands)` and return an array of output strings. ### Constraints & Assumptions - Latitude must be in `[-90, 90]`; longitude must be in `[-180, 180]`. - Capacity must be greater than zero. - An integer field must have an integer type; a Boolean is not accepted as an integer even in languages where Boolean values are integer-like. - Every region argument must be a nonempty ASCII string. `REGISTER` rejects a name that is already present. - `1 <= len(commands) <= 100,000`. - Capacity is stored but does not alter routing in this version. ### Clarifying Questions to Ask - Does an unhealthy region appear in the candidate list? No. - Does distance ranking use rounded or full precision? Full precision; only displayed distance is rounded. - How are exact distance ties resolved? Lexicographically by region name. - Does `DISTANCE` require registered regions? No; it operates directly on coordinates. - What happens for an unknown command, wrong arity, wrong type, or out-of-range query coordinate? That command returns `ERROR`, produces no mutation, and does not stop later commands from being processed. ```hint Centralize validation and distance math One coordinate validator and one Haversine helper keep all command paths consistent. ``` ```hint Sort the same records you display For `ROUTE`, compute each healthy region's full-precision distance once, sort `(distance, region)`, then use the first record and the complete sorted name list. ``` ### Example ```text commands = [ ["REGISTER", "east", 40, -74, 10], ["REGISTER", "west", 34, -118, 20], ["SET_HEALTHZ", "east", false], ["ROUTE", 41, -73] ] outputs = [ "OK", "OK", "OK", "west 4000 west" ] ``` ### Evaluation Focus - Enforces exact command shapes, strict field types, coordinate ranges, registration and lookup rules without partially mutating state on errors. - Implements the Haversine formula in radians and deterministic rounding. - Excludes unhealthy regions from both selection and candidates. - Applies full-precision distance ordering and alphabetical tie-breaking. - Handles state changes across the complete command sequence. ### Extensions to Discuss 1. How would capacity and current load affect routing while preserving deterministic ties? 2. What data structure would help when millions of regions make every route scan too expensive? 3. How would you make registry updates durable and concurrent?

Quick Answer: Process a deterministic command language for registering data centers, changing health, measuring Haversine distance, and routing to healthy regions, with one output and no partial mutation per command.

|Home/Coding & Algorithms/Stripe
Stripe logo
Stripe
Aug 13, 2026, 12:00 AM
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
5
0

Problem

Process commands for an in-memory registry of data centers. Each registered region has integer latitude, integer longitude, positive integer capacity, and a health flag that initially equals true.

Supported commands are represented as arrays:

["REGISTER", region, latitude, longitude, capacity]
["SET_HEALTHZ", region, healthy]
["DISTANCE", lat1, lon1, lat2, lon2]
["ROUTE", latitude, longitude]

Process every array independently and return exactly one output string for every input array, including malformed arrays and unsupported command names. A command has the exact arity shown above; otherwise its output is ERROR.

  • REGISTER returns OK only when region is valid and unregistered, both coordinates are valid integers in range, and capacity is a positive integer. Otherwise it returns ERROR .
  • SET_HEALTHZ returns OK only when region is valid and registered and healthy is exactly a Boolean. Otherwise it returns ERROR . true means healthy and false means unhealthy.
  • DISTANCE returns ERROR unless all four values are valid integer coordinates in range. For valid input, it returns the Haversine distance in kilometers, rounded with floor(distance + 0.5) .
  • ROUTE returns ERROR unless its latitude and longitude are valid integers in range. For valid input, it considers healthy regions only. Rank them by unrounded Haversine distance, then region name. Return region roundedDistance candidates , where candidates is every healthy region name in ranking order joined by commas. If none are healthy, return NONE 0 .

An ERROR command leaves the registry exactly unchanged. Validate the complete command before inserting a region or changing health, then continue with the next array.

Use Earth radius 6371 kilometers and convert degrees to radians before applying:

a = sin²((lat2-lat1)/2) + cos(lat1) * cos(lat2) * sin²((lon2-lon1)/2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = 6371 * c

Function Contract

Implement processDataCenterCommands(commands) and return an array of output strings.

Constraints & Assumptions

  • Latitude must be in [-90, 90] ; longitude must be in [-180, 180] .
  • Capacity must be greater than zero.
  • An integer field must have an integer type; a Boolean is not accepted as an integer even in languages where Boolean values are integer-like.
  • Every region argument must be a nonempty ASCII string. REGISTER rejects a name that is already present.
  • 1 <= len(commands) <= 100,000 .
  • Capacity is stored but does not alter routing in this version.

Clarifying Questions to Ask Guidance

  • Does an unhealthy region appear in the candidate list? No.
  • Does distance ranking use rounded or full precision? Full precision; only displayed distance is rounded.
  • How are exact distance ties resolved? Lexicographically by region name.
  • Does DISTANCE require registered regions? No; it operates directly on coordinates.
  • What happens for an unknown command, wrong arity, wrong type, or out-of-range query coordinate? That command returns ERROR , produces no mutation, and does not stop later commands from being processed.

Example

commands = [
  ["REGISTER", "east", 40, -74, 10],
  ["REGISTER", "west", 34, -118, 20],
  ["SET_HEALTHZ", "east", false],
  ["ROUTE", 41, -73]
]

outputs = [
  "OK",
  "OK",
  "OK",
  "west 4000 west"
]

Evaluation Focus

  • Enforces exact command shapes, strict field types, coordinate ranges, registration and lookup rules without partially mutating state on errors.
  • Implements the Haversine formula in radians and deterministic rounding.
  • Excludes unhealthy regions from both selection and candidates.
  • Applies full-precision distance ordering and alphabetical tie-breaking.
  • Handles state changes across the complete command sequence.

Extensions to Discuss

  1. How would capacity and current load affect routing while preserving deterministic ties?
  2. What data structure would help when millions of regions make every route scan too expensive?
  3. How would you make registry updates durable and concurrent?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...