Batch a Usage Record Stream Into Count, Size and Age Bounded Flushes

Last updated: November 29, 2026

Quick Overview

Write a generator that consumes a time-ordered stream of metered usage records `(timestamp, payload)` and yields batches, flushing whenever a batch reaches `max_count` records, would exceed `max_bytes`, or a new record arrives `max_age` seconds or more after the batch started.

Stripe
Coding & Algorithms
Software Engineer
Stripe
November 29, 2026
Software Engineer
Take-home Project
Coding & Algorithms
Medium

0

0

0 solved


Write a generator that consumes a time-ordered stream of metered usage records `(timestamp, payload)` and yields batches, flushing whenever a batch reaches `max_count` records, would exceed `max_bytes`, or a new record arrives `max_age` seconds or more after the batch started.

Usage-based billing on Stripe ingests very high volumes of meter events, and clients batch them before sending to keep request counts and latency in check. The interviewer checks that you write a clean lazy generator, apply all three limits in the right order and think about what happens to an oversized single record and to the final partial batch.

What the Interviewer Expects
  • Implement as a generator so the input can be unbounded
  • Check the age and size limits before adding the new record, and the count limit after
  • Always flush the final partial batch when the stream ends
  • Handle a single record larger than `max_bytes` by sending it alone
  • Explain that age is only checked when a record arrives, and what a timer would add
Key Topics to Cover
iterators
generators
streams
batching
metered billing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you add a background timer so a quiet stream still flushes on time?
  • How would you retry a failed batch without reordering records?
  • How would you make batching idempotent so retried batches are not double-billed?
  • What metrics would you emit from this component?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Records arrive in non-decreasing timestamp order; payload size is len(payload) bytes. Output batches preserve input order. A batch is never empty. Age is measured from the first record of the curren...

High-Level Design

Keep batch, its byte size and its start timestamp. For each record: if the batch is non-empty and either the record is too old relative to start or adding it would exceed max_bytes, yield th...


Submit Your Answer
Markdown supported