KatInterview

Coalescing Single-Key Reads into Timed Batch Requests

Data Structure DesignModel answer
MediumModel answerData Structure Designevent-loopbatchingcallbacks

Last updated

Build a callback-driven client that gathers individual key requests into one batched GET every 100 ms, using only a timer and an async HTTP primitive, while keeping overlapping batches isolated.

You are writing client code for a single-threaded, callback-driven runtime, comparable to a JavaScript event loop. A backend offers a bulk read endpoint:

GET /get?keys=key1,key2,...
Response: {"key1": 123, "key2": "abs"}

The runtime gives you exactly two primitives:

http_get_request(url, callback)  # issues a GET; when the reply arrives, invokes callback(response: dict)
set_timeout(callback, ms)        # invokes callback() once, ms milliseconds after this call

Implement start_fetch(key, callback). Application code calls it for one key at a time, but you must not issue one HTTP request per call. Instead, gather the requested keys and send them to the endpoint as a single batch request roughly every 100 ms. When a batch's response arrives, every callback that was waiting on a key from that batch must be invoked.

def start_fetch(key, callback):
    # TODO: implement this
    ...

Example driver:

def main():
    start_fetch("foo", cb1)   # t = 0 ms
    start_fetch("bar", cb2)   # t = 30 ms
    start_fetch("foo", cb3)   # t = 70 ms
    start_fetch("foo", cb4)   # t = 120 ms

Here http_get_request must be called exactly twice. The first response triggers cb1, cb2 and cb3; the second response triggers cb4.

You may introduce global state and helper functions.

Rules

  • The runtime is single-threaded: start_fetch, timer callbacks and response callbacks each run to completion, one at a time.
  • A key may be requested multiple times before its batch goes out, and all waiting callbacks must fire (cb1 and cb3 both wait on "foo").
  • A key requested after its batch has been sent belongs to a later batch, even if the earlier request is still unanswered. This is why cb4 causes a second request.
  • A response can arrive at any later moment, including after further start_fetch calls or after the next batch has already been sent.

What to deliver

Write the implementation and explain (1) how you schedule the 100 ms flush, and (2) how you store pending keys together with the callbacks waiting on each. State your assumptions about the open design points: how the window is anchored, whether duplicate keys appear once in the URL, what the callback receives, and how failures or missing keys are handled.

Hints

Hint 1

Think about what has to happen the first time a key arrives while nothing is pending, and what should happen on every later arrival before the flush.

Hint 2

A dictionary from key to a list of callbacks handles duplicates naturally, and its keys give you the de-duplicated URL list.

Hint 3

At flush time, hand the current dictionary to the request's response handler and replace the global with a fresh one, so a late response can only touch its own batch.

Hint 4

Decide up front what a callback receives and what you do when the response lacks a key or the request fails; make sure each callback still runs exactly once.