PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates command parsing, stateful data structure management, input validation, geospatial computation using the Haversine formula, deterministic sorting/tie-breaking, and capacity-based routing logic.

  • medium
  • Stripe
  • Coding & Algorithms
  • Software Engineer

Implement a Datacenter Request Router

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

Implement a command processor for a request-routing system that manages datacenters. Each datacenter stores: - `name` - `latitude` in `[-90, 90]` - `longitude` in `[-180, 180]` - `capacity`, which must be greater than `0` - `healthy` status, initially `true` - `current_load`, initially `0` Process commands sequentially and return one output per command. ## Commands ### 1. `REGISTER <name> <lat> <lon> <capacity>` Create a new datacenter. - Reject the command if: - latitude is outside `[-90, 90]` - longitude is outside `[-180, 180]` - capacity is `<= 0` - the datacenter name already exists - Output `OK` on success, otherwise output `ERROR`. ### 2. `SET_HEALTHY <name> <true|false>` Update the health status of an existing datacenter. - Output `OK` on success. - Output `ERROR` if the datacenter does not exist. ### 3. `DISTANCE <lat1> <lon1> <lat2> <lon2>` Compute the great-circle distance between two coordinates. - Validate both points using the same latitude/longitude bounds as above. - Use the Haversine formula with Earth radius `6371 km`. - Output the distance rounded to the nearest whole kilometer. - If any coordinate is invalid, output `ERROR`. ### 4. `ROUTE <userLat> <userLon>` Route one user request to a datacenter. - Consider only healthy datacenters. - Compute the distance from the user to every healthy datacenter. - Sort candidates by distance ascending. - If two datacenters have the same distance, sort by name in lexicographic order. - Scan the sorted list and choose the first datacenter whose `current_load < capacity`. - If a datacenter is selected: - increment its load by `1` - output: `<selected_name> <selected_distance> <comma-separated healthy datacenter names in sorted order>` - If all healthy datacenters are full, output: `None <comma-separated healthy datacenter names in sorted order>` - If there are no healthy datacenters, output `None`. ## Example 1 Input commands: - `REGISTER us-west 38 -122 100` - `REGISTER us-east 41 -74 150` - `REGISTER us-west 50 -100 50` - `REGISTER invalid-node 91 0 100` - `REGISTER invalid-cap 0 0 0` - `SET_HEALTHY us-east false` - `SET_HEALTHY fake-node true` Output: - `OK` - `OK` - `ERROR` - `ERROR` - `ERROR` - `OK` - `ERROR` ## Example 2 Input commands: - `DISTANCE 38 -122 41 -74` - `DISTANCE 0 0 0 0` - `DISTANCE 91 0 0 0` Output: - `4080` - `0` - `ERROR` ## Example 3 Input commands: - `REGISTER node-A 0 0 1` - `REGISTER node-B 0 0 1` - `REGISTER node-C 10 10 100` - `SET_HEALTHY node-C false` - `ROUTE 0 0` - `ROUTE 0 0` - `ROUTE 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`

Quick Answer: This question evaluates command parsing, stateful data structure management, input validation, geospatial computation using the Haversine formula, deterministic sorting/tie-breaking, and capacity-based routing logic.

Implement a command processor for a request-routing system that manages datacenters. Write a function `solution(commands)` that processes the commands in order and returns a list of output strings, one output per command. Each datacenter stores: - `name` - `latitude` in `[-90, 90]` - `longitude` in `[-180, 180]` - `capacity` as a positive integer - `healthy` status, initially `true` - `current_load`, initially `0` Supported commands: 1. `REGISTER <name> <lat> <lon> <capacity>` - Create a new datacenter. - Reject the command and output `ERROR` if: - latitude is outside `[-90, 90]` - longitude is outside `[-180, 180]` - capacity is `<= 0` - the datacenter name already exists - Otherwise output `OK`. 2. `SET_HEALTHY <name> <true|false>` - Update the health status of an existing datacenter. - Output `OK` on success. - Output `ERROR` if the datacenter does not exist. 3. `DISTANCE <lat1> <lon1> <lat2> <lon2>` - Compute the great-circle distance between two coordinates using the Haversine formula. - Use Earth radius `6371 km`. - Validate both points using the same latitude/longitude bounds as above. - Output the distance rounded to the nearest whole kilometer. - If any coordinate is invalid, output `ERROR`. 4. `ROUTE <userLat> <userLon>` - Validate the user coordinates using the same latitude/longitude bounds. If invalid, output `ERROR`. - Consider only healthy datacenters. - Compute the distance from the user to every healthy datacenter. - Sort healthy datacenters by exact computed distance ascending. If two distances are equal, sort by datacenter name in lexicographic order. - Scan that sorted list and choose the first datacenter whose `current_load < capacity`. - If a datacenter is selected: - increment its `current_load` by `1` - output `<selected_name> <selected_distance> <comma-separated healthy datacenter names in sorted order>` - `selected_distance` must be rounded to the nearest whole kilometer - If all healthy datacenters are full, output `None <comma-separated healthy datacenter names in sorted order>`. - If there are no healthy datacenters, output `None`. Notes: - The comma-separated list in `ROUTE` must follow the same sorted order used for routing. - Datacenter loads persist across future commands.

Constraints

  • 0 <= len(commands) <= 2000
  • Datacenter names are non-empty strings without spaces
  • Latitude and longitude values may be integers or decimals
  • Capacity is intended to be an integer and must be greater than 0
  • At most 2000 datacenters will be registered

Examples

Input: []

Expected Output: []

Explanation: No commands means no outputs.

Input: ["REGISTER us-west 38 -122 2", "REGISTER us-east 41 -74 3", "REGISTER us-west 50 -100 1", "REGISTER invalid-node 91 0 100", "REGISTER invalid-cap 0 0 0", "SET_HEALTHY us-east false", "SET_HEALTHY fake-node true"]

Expected Output: ["OK", "OK", "ERROR", "ERROR", "ERROR", "OK", "ERROR"]

Explanation: This checks successful registration, duplicate names, invalid coordinates, invalid capacity, updating an existing datacenter, and failing to update a missing one.

Input: ["DISTANCE 0 0 0 0", "DISTANCE 0 0 90 0", "DISTANCE 0 0 0 180", "DISTANCE 91 0 0 0"]

Expected Output: ["0", "10008", "20015", "ERROR"]

Explanation: Same point is 0 km, equator to pole is a quarter of Earth's circumference, opposite points on the equator are half the circumference, and invalid coordinates produce ERROR.

Input: ["REGISTER node-B 0 0 1", "REGISTER node-A 0 0 1", "REGISTER node-C 10 10 100", "SET_HEALTHY node-C false", "ROUTE 0 0", "ROUTE 0 0", "ROUTE 0 0"]

Expected 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: node-A and node-B are tied at distance 0, so lexicographic order breaks the tie. After both reach capacity 1, routing returns None along with the healthy sorted list.

Input: ["REGISTER gamma 0 0 1", "REGISTER alpha 0 180 1", "REGISTER beta 90 0 1", "ROUTE 0 0", "ROUTE 0 0", "ROUTE 95 0", "SET_HEALTHY gamma false", "ROUTE 0 0"]

Expected Output: ["OK", "OK", "OK", "gamma 0 gamma,beta,alpha", "beta 10008 gamma,beta,alpha", "ERROR", "OK", "alpha 20015 beta,alpha"]

Explanation: The first route chooses the closest node, the second skips a full but still healthy node, invalid user coordinates return ERROR, and after gamma is marked unhealthy only beta and alpha remain in sorted order.

Input: ["REGISTER solo 0 0 1", "SET_HEALTHY solo false", "ROUTE 0 0"]

Expected Output: ["OK", "OK", "None"]

Explanation: When there are no healthy datacenters, ROUTE returns exactly None.

Hints

  1. Use a hash map keyed by datacenter name so REGISTER, duplicate checks, and SET_HEALTHY updates are O(1).
  2. For each ROUTE command, build a list of tuples like (exact_distance, name), sort it once, and then scan for the first datacenter that still has remaining capacity.
Last updated: May 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Assign Reviewers from Changed Files - Stripe (medium)
  • Generate Account Email Notifications - Stripe (medium)
  • Calculate Transaction Fees - Stripe (medium)
  • Build an Account Transfer Ledger - Stripe (medium)
  • Implement Validation and String Compression - Stripe (hard)