Query Batching - Problem

Design and implement a QueryBatcher class that efficiently batches multiple small queries into larger ones using a throttling mechanism.

The class constructor accepts:

  • queryMultiple: An asynchronous function that takes an array of string keys and returns a Promise resolving to an array of corresponding values
  • t: Throttle time in milliseconds

Implement the method:

  • async getValue(key): Returns a Promise that resolves to the value for the given key

Throttling Rules:

  • First call to getValue immediately triggers queryMultiple
  • Subsequent calls within t milliseconds are batched together
  • After t milliseconds, all pending keys are sent to queryMultiple
  • Each key is guaranteed to be unique

Input & Output

Example 1 — Basic Throttling
$ Input: queryMultiple = async (keys) => keys.map(k => `value_${k}`), t = 100
Output: First getValue('key1') returns 'value_key1' immediately, subsequent calls batched
💡 Note: First call executes immediately. Calls within 100ms window get batched together and executed after timeout.
Example 2 — Multiple Batches
$ Input: Multiple getValue calls over time with 200ms throttle
Output: Separate batches for calls outside throttle window
💡 Note: Calls separated by more than throttle time create new batches, maintaining efficient grouping.
Example 3 — Single Key Immediate
$ Input: Single getValue call when no pending queries
Output: Immediate execution without waiting
💡 Note: When no queries are pending and enough time has passed, execute immediately for best performance.

Constraints

  • 1 ≤ t ≤ 104 (throttle time in milliseconds)
  • queryMultiple never rejects
  • All keys passed to getValue are unique
  • queryMultiple returns array same length as input

Visualization

Tap to expand
Query Batching - Throttled Batching INPUT QueryBatcher( queryMultiple, t=100ms ) Call Timeline 0ms 50ms 100ms 1 2 3 getValue() Calls: getValue('key1') @ 0ms getValue('key2') @ 20ms getValue('key3') @ 50ms queryMultiple Function: async (keys) => keys.map(k => `value_${k}`) ALGORITHM STEPS 1 First Call Triggers getValue('key1') executes queryMultiple immediately 2 Start Throttle Timer Set t=100ms timer Collect pending keys 3 Batch Pending Keys key2, key3 added to batch while timer active 4 Timer Expires Execute queryMultiple with batched keys Batch Queue State key2 key3 ... Timer: 50ms / 100ms FINAL RESULT Immediate (0ms): queryMultiple(['key1']) --> 'value_key1' Batched (100ms): queryMultiple(['key2','key3']) --> ['value_key2', 'value_key3'] Promise Resolution: key1 --> 'value_key1' key2 --> 'value_key2' key3 --> 'value_key3' OK - All Resolved Key Insight: The QueryBatcher optimizes network calls by collecting multiple getValue() requests within a throttle window (t ms). The first call triggers immediately, while subsequent calls within t milliseconds are batched together. This reduces N individual queries to fewer batched queries, improving efficiency and reducing latency. TutorialsPoint - Query Batching | Throttled Batching Approach Time: O(batch_size) per batch | Space: O(pending_keys) for queue
Asked in
Netflix 15 Uber 12 Meta 10 Google 8
25.4K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen