Home
← All questions

Analytics module: batch events with flush on size or time in JavaScript

HardPremiumJavaScript

JavaScript interview practice: Send analytics blobs in batches or after an idle duration, with optional retries and storage persistence.

Problem

Build an Analytics module that batches JSON blobs and sends them via an injected send(batch) API when the batch reaches batchSize OR idleDuration ms pass since the last add. Follow-ups: maxRetries, and persisting the queue to localStorage/IndexedDB until a successful send.

const sampleSend = async (batch) =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log("Processing batch", batch.map((b) => b.a));
      Math.random() < 0.2 ? reject("Error in sending batch") : resolve("Success");
    }, 2000);
  });
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium