Aggregate Out-of-Order Sensor Readings With a Watermark
Last updated: November 29, 2026
Quick Overview
Build a per-device, per-minute sum and count aggregator for sensor readings that arrive out of order, emitting each minute bucket once the watermark (max event time seen minus allowed lateness) passes its end, and counting readings that arrive after their bucket closed.
Samsara
November 29, 20260
0
0 solved
Build a per-device, per-minute sum and count aggregator for sensor readings that arrive out of order, emitting each minute bucket once the watermark (max event time seen minus allowed lateness) passes its end, and counting readings that arrive after their bucket closed.
Samsara gateways buffer data when they lose cellular coverage and upload it later, so the ingestion pipeline constantly sees late and out-of-order points. The interviewer checks event-time versus arrival-time reasoning and a sane policy for data that comes too late.
What the Interviewer Expects
- Bucket by event time, not arrival time
- Define and advance a watermark from max event time
- Emit buckets in end-time order with a heap
- Never emit a bucket twice; route too-late data to a counter or correction path
- Discuss memory bounds and a final flush
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- A truck in a tunnel uploads two hours of data at once; how does a global watermark hurt other devices?
- Would you prefer a per-device watermark? What are the trade-offs?
- How would you issue corrections for already emitted buckets downstream?
- How would you make this fault tolerant across restarts?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
add(device, event_time, value) returns any buckets that are now final as (device, bucket_start, sum, count). Buckets are 60 s. Watermark = max event time seen minus lateness. A bucket is final w...
High-Level Design
Keep a dict of open buckets keyed by (device, bucket_start) and a min-heap keyed by bucket end, so advancing the watermark pops only what is ready. Track closed_upto, the highest watermark reached...