Skip to the solution
MediumLow-Level DesignAsked in 20247 min read

Build a Dataloader-Style Batching Utility That Coalesces Individual Async Calls

2.2k views

The problem

Problem, as an interviewer would state it: "Build a batching utility (like Facebook's DataLoader): individual load(key) calls made within the SAME tick should coalesce into ONE real bulk request, with each caller correctly getting back only their own result."

Examples:

Code
const [u1, u2, u3] = await Promise.all([loader.load(1), loader.load(2), loader.load(3)]);
// only ONE real batchFn([1,2,3]) call happens, despite 3 separate load() calls

Clarifying questions expected:

  • What defines "the same tick" for batching purposes — the current synchronous execution, a microtask boundary, or a real timer-based window?
  • Does the batch function receive keys in the exact order they were requested, and must results map back correctly by that same order?
  • Should a load() call made in a LATER tick (after the current batch has already started) join the same batch, or start a new one?

Code / implementation expected: Yes — real, direct proof that 3 concurrent load() calls trigger exactly 1 real batch call, with each result correctly routed to its own original caller.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: 3 concurrent load() calls coalesce into exactly 1 real batchFn call, with results correctly routed to their own callers

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 68 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Find missing number

Continue