Problemhealth aware resolver pool

Session: Sign in to solve

Solution.txt

Health-Aware Resolver Pool

Problem

Design HealthAwareResolverPool, an in-memory DNS resolver that stores one current address answer per hostname, tracks the latest health observation for each address, and applies a fixed time to live.

Requirements

  • Replace an answer. publishAnswer replaces the hostname's cached answer with the supplied distinct addresses and observedAt.
  • Track latest health. recordHealth replaces the address's previous health observation with the supplied value.
  • Expire by TTL. An answer is valid through observedAt + ttl and expires when now is greater; expired addresses are not eligible.
  • Return eligible addresses. resolve returns exactly the healthy addresses in the hostname's current valid answer. The returned array's order is not judged.
  • Keep reads hostname-local. resolve runs in O(k)O(k) time, where kk is the size of the requested hostname's current answer, independent of addresses stored for other hostnames.

API

SignatureReturnsBehavior
HealthAwareResolverPool(ttl: integer)Not applicableCreates an empty pool with one fixed positive TTL.
publishAnswer(hostname: string, addresses: string[], observedAt: integer)voidReplaces one hostname's cached answer.
recordHealth(address: string, healthy: boolean)voidStores the address's latest health observation.
resolve(hostname: string, now: integer)string[]Returns the requested hostname's eligible address set as an array in O(k)O(k) time.

Examples

Given HealthAwareResolverPool(10):

StepOperationResult
1publishAnswer("api.test", ["10.0.0.2", "10.0.0.1"], 5)void
2recordHealth("10.0.0.2", true)void
3recordHealth("10.0.0.1", false)void
4resolve("api.test", 15)["10.0.0.2"]
5resolve("api.test", 16)[]

Constraints

  • ttl is between 1 and 1,000,000,000.
  • Every string is non-empty ASCII text with at most 253 characters.
  • Each published answer contains 1 to 1,000 distinct addresses.
  • Time values are between 0 and 1,000,000,000.
  • observedAt + ttl fits in a signed 64-bit integer.
  • Every hostname supplied to resolve has a current published answer.
  • Before resolving an unexpired answer, every address in that current answer has a recorded health observation.
  • At most 50,000 public method calls occur per testcase.
  • Inputs satisfy these constraints.

Notes

Calls are sequential. Result-array order has no meaning.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.