Build a simplified request routing system for a globally distributed cloud service provider (e.g., a global payment processing platform). The system processes a sequence of commands from stdin and returns responses to stdout.
Part 1 – Datacenter Registry & Health Management
REGISTER <name> <latitude> <longitude> <capacity>: Register a new datacenter. Validation rules: latitude must be in [-90, 90], longitude must be in [-180, 180], capacity must be strictly > 0, and name must be globally unique. On success, the datacenter is initialized with load = 0 and healthy = true. Returns "OK" or "ERROR".
SET_HEALTHY <name> <true|false>: Update the health status of an existing datacenter. Returns "OK" if the datacenter exists, "ERROR" otherwise.
Part 2 – Distance Calculation
DISTANCE <lat1> <lon1> <lat2> <lon2>: Calculate the great-circle distance between two geographic points using the Haversine formula with Earth radius R = 6371 km. Both coordinate pairs must be valid. Return the distance rounded to the nearest whole integer, or "ERROR" if any coordinate is invalid.
Haversine pseudocode (provided in the problem):
Part 3 – Proximity-Based Routing
ROUTE <user_lat> <user_lon>: Route a user request to the optimal datacenter. User coordinates must be valid (else ERROR). Routing logic:
<AllocatedName> <DistanceToUser> <comma-separated list of all healthy datacenter names in sorted order>None <comma-separated list of all healthy datacenter names>NONE (or NONE 0 per one variant)All coordinates (latitude, longitude, capacity) are integers. There are 20 test cases total distributed across the three parts. The OA is timed (~1 hour).
Edge cases probed: Registering a datacenter with a duplicate name (should return ERROR); Registering with latitude exactly at boundary values: -90, 90 (valid) vs 91 (invalid); Registering with capacity = 0 (invalid; must be strictly > 0); SET_HEALTHY on a datacenter name that does not exist (ERROR); DISTANCE with an invalid coordinate, e.g., lat=91 (ERROR); DISTANCE between identical points (output: 0); ROUTE when all healthy datacenters are at full capacity (output: None + list of healthy nodes); ROUTE tie-breaking: two datacenters at equal distance sorted alphabetically by name; Load tracking: once a datacenter reaches capacity, subsequent ROUTE calls must skip it
What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 70 Stripe questions, refreshed monthly from new candidate reports.