KatInterview

Healthy Region Registry and Nearest-Site Router

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designsimulationstring-parsinghash-map

Last updated

Simulate a command interpreter that registers sites, toggles their health, and answers nearest-healthy-site queries by great-circle distance, with strict validation and tie-breaking.

Healthy Region Registry and Nearest-Site Router

You are building the core of a traffic router for a worldwide hosting service. It keeps a registry of sites (regions) with their geographic positions and a health flag. It also answers queries asking which healthy site is closest to a given point on Earth.

Write a function that takes a list of text commands and returns a list of text results. Commands are processed in order, and each command produces exactly one output string. If a command is invalid for any reason, the registry must stay exactly as it was and the output for that command is ERROR.

Parsing rules

  • Split each command on runs of whitespace (as Python's str.split() does). The first token is the command name, and names are case-sensitive.
  • The number of tokens must match the command exactly, as listed below. A wrong token count, an unknown command name, or an empty or blank command gives ERROR.
  • A "valid number" is any text that Python's float() accepts, except that nan, inf and -inf (in any casing) are not valid. Range limits are inclusive.

Commands

REGISTER <id> <lat> <lon> (4 tokens)

Adds a new site whose health starts as UP.

  • <id> must be non-empty, consist only of letters and digits (the check Python's str.isalnum() performs), and not match any site already registered. Characters such as - or _ make an id invalid.
  • <lat> must be a valid number in [-90.0, 90.0].
  • <lon> must be a valid number in [-180.0, 180.0].
  • Output OK on success, otherwise ERROR.

SET_STATUS <id> <state> (3 tokens)

Changes the health of an existing site.

  • <id> must already be registered.
  • <state> must be exactly UP or DOWN (case-sensitive).
  • Setting a site to the state it already has is allowed and still gives OK.
  • Output OK on success, otherwise ERROR.

ROUTE <lat> <lon> (3 tokens)

Finds the closest site whose health is UP, measured from the query point.

  • <lat> must be a valid number in [-90.0, 90.0] and <lon> a valid number in [-180.0, 180.0].
  • At least one registered site must currently be UP.
  • Output the <id> of the closest UP site. If several sites are at exactly the same computed distance, output the lexicographically smallest id.
  • Otherwise output ERROR.

Distance

For points (lat1, lon1) and (lat2, lon2) given in degrees, with R = 6371.0:

dlat = radians(lat2) - radians(lat1)
dlon = radians(lon2 - lon1)
a    = sin(dlat / 2)^2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon / 2)^2
a    = clamp(a, 0, 1)
dist = 2 * R * asin(sqrt(a))

Compare distances as the floating-point values this formula produces. Distances are tied only when these values are exactly equal.

Constraints

  • 1 <= len(commands) <= 5000
  • At most 1000 distinct sites are ever registered.
  • No input may cause an unhandled exception. Anything malformed must simply yield ERROR.

Examples

Example 1

Input: run_region_commands(['REGISTER us-east 39.0438 -77.4874', 'REGISTER eu-west 53.3498 -6.2603', 'REGISTER us-east 40.0 -74.0', 'ROUTE 40.7128 -74.0060', 'SET_STATUS us-east DOWN', 'ROUTE 40.7128 -74.0060', 'SET_STATUS eu-west DOWN', 'ROUTE 40.7128 -74.0060'])

Output: ['ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR']

Hints

Hint 1

Keep a dictionary from site id to its latitude, longitude and an up/down flag. Handle each command by checking its name and token count together, and fall through to ERROR otherwise.

Hint 2

Validate everything first (id rule, number parsing, ranges, state text) and only then write to the dictionary, so a failing command can never leave partial changes. Remember that float() accepts 'nan' and 'inf', which must be rejected.

Hint 3

For ROUTE, scan every UP site, compute the Haversine distance, and keep the best by the pair (distance, id). Convert degrees to radians before using trig functions, and clamp 'a' into [0, 1] before taking the square root and asin.