NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

A request router maintains a collection of datacenters. Each datacenter has a unique, case-sensitive name, integer latitude and longitude, a capacity, a health status, and a current load. Different datacenters may share coordinates. A newly registered datacenter is healthy and has a load of 0.

Implement the RequestRouter class:

  • RequestRouter() initializes an empty router.
  • String processCommand(String command) accepts one space-separated command and returns its response:
    • "<REGISTER> <name> <latitude> <longitude> <capacity>" (space-separated) registers a datacenter if the name is not already registered, the latitude is within [-90, 90], the longitude is within [-180, 180], and the capacity is positive. Return "OK" on success or "ERROR" without changing the router otherwise.
    • "<SET_HEALTHY> <name> <true|false>" (space-separated) changes the health status of an existing datacenter and returns "OK". If the name does not exist, return "ERROR" without changing the router.

Changing a datacenter's health status does not change its capacity or load. Commands take effect in the order received.

Constraints:

  • At most 500 calls to processCommand are made on one router.
  • 1 ≤ name.length ≤ 20. Names contain only ASCII letters, digits, or hyphens.
  • All commands follow the specified syntax, and all numeric fields contain parseable integers.

Example:

Input: ["RequestRouter", "processCommand", "processCommand", "processCommand", "processCommand", "processCommand", "processCommand", "processCommand"] [[], ["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: [null, "OK", "OK", "ERROR", "ERROR", "ERROR", "OK", "ERROR"]

Explanation:

  • RequestRouter router = new RequestRouter();
  • router.processCommand("REGISTER us-west 38 -122 100"); // Returns "OK" and registers us-west.
  • router.processCommand("REGISTER us-east 41 -74 150"); // Returns "OK" and registers us-east.
  • router.processCommand("REGISTER us-west 50 -100 50"); // Returns "ERROR" because the name already exists.
  • router.processCommand("REGISTER invalid-node 91 0 100"); // Returns "ERROR" because latitude 91 is invalid.
  • router.processCommand("REGISTER invalid-cap 0 0 0"); // Returns "ERROR" because capacity must be positive.
  • router.processCommand("SET_HEALTHY us-east false"); // Returns "OK" and marks us-east unhealthy.
  • router.processCommand("SET_HEALTHY fake-node true"); // Returns "ERROR" because the name is not registered.

Accepted
156/506
Acceptance Rate30.8%
Test Cases
Input
Output
Expected