Build a datacenter region registry that validates REGISTER and SET_HEALTHZ commands.
Store all datacenters and their details, and calculate the routing distance.
Part 1: Commands are REGISTER <region> <latitude> <longitude> <capacity> and SET_HEALTHZ <region> <state>. Output OK if the command succeeds or ERROR if it fails the rules.
Sample input: REGISTER us-east-1 38 120 100 REGISTER us-west-2 50 112 30 SET_HEALTHZ us-west-2 false REGISTER eu-east-1 -10 15 0
Rules:
- Latitude must be in [-90, 90].
- Longitude must be in [-180, 180].
- Capacity must be > 0.
- A region must not already be registered for a REGISTER call, but must be registered for a SET_HEALTHZ call.
- Latitude, longitude and capacity are integers; healthz is always a boolean.
- New regions are healthy by default.
Part 2: Calculate the Haversine distance between two sets of coordinates (the Haversine algorithm is given). Input: DISTANCE <lat1> <long1> <lat2> <long2>. Sample input: DISTANCE 0 0 100 100. Output: 10200. Inputs are integers and the output must be rounded to an integer.
Part 3: Input: ROUTE <latitude> <longitude>. Route the request to a registered region that is healthy and can handle the load, and return the chosen region, its distance from the request (Haversine from part 2), and the possible candidates for the routing.
Sample input: REGISTER us-east-1 0 0 1 REGISTER ap-south-1 0 0 1 ROUTE 0 0 SET_HEALTHZ ap-south-1 false ROUTE 0 0
Output: OK OK us-east-1 0 us-east-1 ap-south-1 NONE 0 us-east-1
Rules:
- An unhealthy region is not included in the candidates and cannot be routed to.
- The initial load of every registered region is 0.
- If no region can take the request, output NONE with a distance of 0.
- Latitude and longitude constraints are the same as part 1.
- Choose the region by distance, breaking ties alphabetically.
Expect follow-ups like
- Calculate the Haversine distance between two sets of integer coordinates for a DISTANCE command, rounded to an integer.
- Route a ROUTE request to the nearest healthy region that can handle the load, returning its distance and the candidate regions, with ties broken alphabetically.
Format · 1 hour on HackerRank; input and output processed from scratch; the Haversine formula was given.
Community answers
Related courses


