KatInterview

Batch Simulator for a TTL-Aware Name Lookup Service with Aliases

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationdesign

Last updated

Simulate a batch of record insertions and lookups with normalized names, TTL expiry, alias-chain following, cycle and hop-limit detection, and A/alias exclusivity.

You are building a small in-memory name-lookup service, similar to DNS, and you must replay a batch of commands against it. Implement one function that processes the commands in order and returns one result per command.

Inputs

  • operations: a list of command names. Each is either "add_record" or "resolve".
  • arguments: a list of the same length. arguments[i] holds the parameters for operations[i]:
    • for "add_record": [name, record_type, value, ttl, current_time]
    • for "resolve": [name, record_type, current_time]

Output

A list with one entry per command, in the same order:

  • None for every "add_record" command.
  • A list of strings for every "resolve" command.

Record types

  • "A" maps a name to an address string such as "192.0.2.1". A name may hold several different A values at once.
  • "CNAME" maps a name (the alias) to another name (the target). A name holds at most one CNAME.
  • Record types are case-insensitive, so "a" and "cname" are accepted. Any other record type in an add_record command is ignored and stores nothing.

Rules

Name normalization. Names are case-insensitive and may end with one trailing dot. To normalize a name, convert it to lowercase and then remove a single trailing . if present. Do not do anything else to it, such as trimming whitespace or removing further dots. Normalize the name argument of both commands and also the value of a CNAME record, because it is a name. The value of an A record is stored exactly as given.

Expiry. An add_record with ttl and current_time creates a record that expires at current_time + ttl. The record is valid for a query at time t only if t < current_time + ttl. At t equal to the expiry time, or later, the record is expired and must be ignored. A record with ttl <= 0 is therefore never valid. Time values never decrease from one command to the next.

Adding an A record.

  • If the name currently holds a valid CNAME, that CNAME is removed. This happens even if the new A record has ttl <= 0.
  • The A value is then added to the name's set of A values.
  • Adding the same A value again does not remove earlier insertions. A value counts as present if at least one of its insertions is still valid.

Adding a CNAME record.

  • All A values stored for that name are removed.
  • Any existing CNAME for the name is replaced by the new one.

Resolving a CNAME query. Return a one-element list containing the normalized target if the name has a valid CNAME, otherwise []. The chain is not followed.

Resolving an A query. Start at the normalized name and repeat the following:

  1. If the current name has already been visited during this query, there is a cycle. Return [].
  2. Mark the current name as visited.
  3. If the current name has a valid CNAME:
    • If 10 CNAME links have already been followed in this query, return [].
    • Otherwise follow the link: the target becomes the current name, count one more hop, and go back to step 1.
  4. Otherwise, return the sorted list of distinct valid A values stored for the current name. This is [] if there are none, if the name is unknown, or if all of its A values have expired.

A valid CNAME always takes priority over A values at the same name. An expired CNAME is treated as if it does not exist.

Resolving any other record type. Return [].

Result lists of A values are sorted in ascending string order and contain no duplicates.

Constraints

  • operations and arguments have the same length, and there is at least one command.
  • Every command name is "add_record" or "resolve".
  • name, value and record types are strings. ttl and current_time are integers, and ttl may be zero or negative.
  • current_time does not decrease across consecutive commands.
  • At most 10 CNAME links may be followed in a single A query. Needing an 11th link makes the query return [].

Examples

Example 1

Input: simulate_name_service(['add_record', 'add_record', 'resolve', 'resolve', 'resolve'], [['example.com', 'A', '93.184.216.34', 60, 100], ['www.example.com.', 'CNAME', 'example.com', 60, 100], ['EXAMPLE.COM.', 'A', 120], ['www.example.com', 'A', 150], ['www.example.com', 'A', 170]])

Output: [None, None, ['93.184.216.34'], ['93.184.216.34'], []]

Hints

Hint 1

Normalize names once at the entry of every command, including the target value of a CNAME, so that all later lookups are exact dictionary matches.

Hint 2

Store absolute expiry times (current_time + ttl) rather than TTLs. For repeated A values keep the latest expiry, so a value is valid if any of its insertions is still valid.

Hint 3

Resolve an A query with a loop: keep a visited set for cycles and a hop counter that fails when an 11th link would be needed. A valid CNAME is checked before the A values at each name.