Skip to content
Backend logic · Senior · about 20 minutes

Write batcher

Implement createBatcher(flush, { maxSize, maxWaitMs }), which groups single writes into batches the way a logging or analytics client does. It returns an object whose add(item) puts the item in the current batch. When the batch reaches maxSize items, call flush(items) at once. Otherwise flush it maxWaitMs after its first item was added, even if more items arrive. Each batch is a new array, and a batch flushed by size leaves no timer behind. In the tests flush records [time, items], and the test waits long enough for every timer.

Start from

function createBatcher(flush, { maxSize, maxWaitMs }) {
  // Return { add(item) } that flushes by size or by time.
}

Hints and solution

The app has this lab in JavaScript, TypeScript and Python. This page shows the JavaScript code.

Solve it in the app: its tests run without a year.

The hints, the solution and what an interviewer looks for opens with a year in the app. What a year includes