Implement a batching utility that dispatches on batch size or timeout
Quick Overview
Implement a batching utility on a simulated clock that dispatches a batch when it reaches a size limit or when a timeout passes since its first item. It tests state management, keeping the buffer and timer consistent across all three dispatch triggers, the boundary at the exact timeout, and how the design extends to real timers and threads.
Implement a batching utility that dispatches on batch size or timeout
Company: Uber
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a batching utility that collects items and dispatches them in batches. A batch is dispatched either when the batch size limit is reached or when a configurable timeout expires since the first item was added to the current batch, whichever comes first.
The utility uses a simulated clock that starts at 0. Time is advanced explicitly with a `tick` method. After advancing time, if the elapsed time since the first item in the current pending batch meets or exceeds the timeout, the batch is dispatched automatically. A `flush` method dispatches any pending items manually at any time.
All dispatched batches are recorded in order and can be retrieved. The utility also supports querying the current pending buffer and the current simulated time.
Implement the class `BatchingUtility` with these methods:
- `BatchingUtility(batch_size: int, timeout_ms: int)` creates the utility with an empty buffer at time 0.
- `add_item(item: int) -> int` adds an item to the current batch. Returns 1 if a batch was dispatched (batch size reached), 0 otherwise.
- `tick(ms: int) -> int` advances the simulated clock by `ms` milliseconds. Returns 1 if a batch was dispatched due to timeout, 0 otherwise.
- `flush() -> int` manually dispatches any pending items in the current batch. Returns 1 if a batch was dispatched, 0 if the buffer was empty.
- `get_dispatched_batches() -> List[List[int]]` returns all dispatched batches in the order they were dispatched.
- `get_current_batch() -> List[int]` returns the items currently buffered but not yet dispatched.
- `get_current_time() -> int` returns the current simulated time in milliseconds.
Example 1, a dispatch caused by the timeout:
```text
Input:
["BatchingUtility","add_item","add_item","tick","get_dispatched_batches","get_current_batch","get_current_time"]
[[5,500],[1],[2],[500],[],[],[]]
Output:
[null,0,0,1,[[1,2]],[],500]
```
With batch size 5 and a 500 ms timeout, items 1 and 2 arrive at t=0, so the batch starts at t=0. `tick(500)` moves the clock to 500, the elapsed time of 500 ms meets the timeout, and `[1, 2]` is dispatched. Afterward the buffer is empty and the time is 500.
Example 2, a size dispatch followed by a timeout dispatch:
```text
Input:
["BatchingUtility","add_item","tick","add_item","add_item","tick","tick","flush","get_dispatched_batches"]
[[2,100],[7],[60],[8],[9],[50],[50],[],[]]
Output:
[null,0,0,1,0,0,1,0,[[7,8],[9]]]
```
Item 8 fills the batch at t=60, so `[7, 8]` is dispatched right away. Item 9 starts a new batch at t=60, so its timeout expires at t=160, on the second `tick(50)`. The final `flush` finds an empty buffer and returns 0.
```hint Timer state
Think about which single timestamp decides whether the pending batch has expired, and at which moments it has to be set and cleared.
```
```hint Three triggers
Three different events can send a batch. Check that each of them leaves the buffer and the timer in the same state afterward.
```
### Constraints & Assumptions
- `1 <= batch_size <= 100`
- `1 <= timeout_ms <= 10000`
- Items are non-negative integers.
- At most 1000 calls in total across all methods.
- `tick` values are non-negative integers giving the milliseconds to advance.
- The timeout is measured from the first item of the current batch. Later items in the same batch do not restart it.
### Clarifying Questions to Ask
- Should the getters return copies, so a caller cannot change the utility's internal state?
- Is the dispatched history allowed to grow without limit, or will a consumer take batches off it later?
- When the utility is shut down with items still pending, is the caller expected to call `flush`, or should the utility send them itself?
- Will this later run on a real clock or be called from several threads, so the design should leave room for timers and locking?
### What a Strong Answer Covers
- The state the class keeps, and the exact moments the batch start time is set and cleared.
- Whether all three triggers (size limit, timeout, `flush`) leave the buffer and the timer in a consistent state.
- The boundary at exactly the timeout, and the empty-buffer cases for `tick` and `flush`.
- The cost of each method, and whether the getters expose internal lists to the caller.
### Follow-up Questions
- With a real clock nobody calls `tick`. How would you make a pending batch dispatch on time?
- How would you make `add_item` and `flush` safe when several threads call them at once?
- The consumer of each batch can be slow or can fail. Where would you put retries and back-pressure?
- How would you add a second limit, such as total bytes per batch, next to the item count?
Overview: Implement a batching utility on a simulated clock that dispatches a batch when it reaches a size limit or when a timeout passes since its first item. It tests state management, keeping the buffer and timer consistent across all three dispatch triggers, the boundary at the exact timeout, and how the design extends to real timers and threads.
Design a batching utility that collects items and dispatches them in batches. A batch is dispatched either when the batch size limit is reached or when a configurable timeout expires since the first item was added to the current batch, whichever comes first.
The utility uses a simulated clock that starts at 0. Time is advanced explicitly with a tick method. After advancing time, if the elapsed time since the first item in the current pending batch meets or exceeds the timeout, the batch is dispatched automatically. A flush method dispatches any pending items manually at any time.
All dispatched batches are recorded in order and can be retrieved. The utility also supports querying the current pending buffer and the current simulated time.
Implement the class BatchingUtility with these methods:
BatchingUtility(batch_size: int, timeout_ms: int)
creates the utility with an empty buffer at time 0.
add_item(item: int) -> int
adds an item to the current batch. Returns 1 if a batch was dispatched (batch size reached), 0 otherwise.
tick(ms: int) -> int
advances the simulated clock by
ms
milliseconds. Returns 1 if a batch was dispatched due to timeout, 0 otherwise.
flush() -> int
manually dispatches any pending items in the current batch. Returns 1 if a batch was dispatched, 0 if the buffer was empty.
get_dispatched_batches() -> List[List[int]]
returns all dispatched batches in the order they were dispatched.
get_current_batch() -> List[int]
returns the items currently buffered but not yet dispatched.
get_current_time() -> int
returns the current simulated time in milliseconds.
With batch size 5 and a 500 ms timeout, items 1 and 2 arrive at t=0, so the batch starts at t=0. tick(500) moves the clock to 500, the elapsed time of 500 ms meets the timeout, and [1, 2] is dispatched. Afterward the buffer is empty and the time is 500.
Example 2, a size dispatch followed by a timeout dispatch:
Item 8 fills the batch at t=60, so [7, 8] is dispatched right away. Item 9 starts a new batch at t=60, so its timeout expires at t=160, on the second tick(50). The final flush finds an empty buffer and returns 0.
Constraints & Assumptions
1 <= batch_size <= 100
1 <= timeout_ms <= 10000
Items are non-negative integers.
At most 1000 calls in total across all methods.
tick
values are non-negative integers giving the milliseconds to advance.
The timeout is measured from the first item of the current batch. Later items in the same batch do not restart it.
Clarifying Questions to Ask Guidance
Should the getters return copies, so a caller cannot change the utility's internal state?
Is the dispatched history allowed to grow without limit, or will a consumer take batches off it later?
When the utility is shut down with items still pending, is the caller expected to call
flush
, or should the utility send them itself?
Will this later run on a real clock or be called from several threads, so the design should leave room for timers and locking?
What a Strong Answer Covers Guidance
The state the class keeps, and the exact moments the batch start time is set and cleared.
Whether all three triggers (size limit, timeout,
flush
) leave the buffer and the timer in a consistent state.
The boundary at exactly the timeout, and the empty-buffer cases for
tick
and
flush
.
The cost of each method, and whether the getters expose internal lists to the caller.
Follow-up Questions Guidance
With a real clock nobody calls
tick
. How would you make a pending batch dispatch on time?
How would you make
add_item
and
flush
safe when several threads call them at once?
The consumer of each batch can be slow or can fail. Where would you put retries and back-pressure?
How would you add a second limit, such as total bytes per batch, next to the item count?