Design async batched key-value fetcher
Company: Anrok
Role: Software Engineer
Category: System Design
Difficulty: hard
Interview Round: Technical Screen
You are given a backend endpoint: HTTP GET /read?keys=k1,k2,... which returns a JSON object mapping each key to a numeric value (e.g., { "foo": 12, "bar": 100 }). You also have two primitives:
(
1) httpGetAsync(urlPath, callback) -> void, and
(
2) parseKvs(json) -> a dictionary mapping keys to values. Implement a single-threaded (event-loop) interface getKeyAsync(key, callback) that supports concurrent I/O and batches multiple incoming requests. Requirements: - Batch all requests that arrive within a 100 ms window into a single HTTP call to /read?keys=...; - Deduplicate keys within a batch; if multiple requests ask for the same key, invoke each request’s callback exactly once; - Enforce consistency: a batch response may only satisfy callbacks for requests that arrived before that batch was dispatched; requests arriving afterward must be served by a subsequent batch; - Allow multiple in-flight batches without mixing responses; - Define the data structures (queues, maps, sets), control flow (timers, scheduling), and how responses are correlated to waiting callbacks; - Specify behavior for missing keys, partial failures, timeouts/retries, and cancellation/backpressure; - Discuss complexity (time/space), single-threaded ordering pitfalls, and a brief test plan.
Quick Answer: This question evaluates understanding of designing asynchronous, batched request handling in a single-threaded event-loop, covering batching windows, key deduplication, response-to-callback correlation, consistency boundaries, error handling, timeouts/retries, cancellation, and backpressure.