Build a Fixed-Size Ring Buffer for Sensor Samples That Overwrites Oldest
Last updated: November 29, 2026
Quick Overview
Implement a fixed-capacity circular buffer for sensor readings with push (overwriting the oldest sample when full and counting drops), pop of the oldest sample, and latest(n) returning the newest n samples in time order.
Samsung
November 29, 20260
0
0 solved
Implement a fixed-capacity circular buffer for sensor readings with push (overwriting the oldest sample when full and counting drops), pop of the oldest sample, and latest(n) returning the newest n samples in time order.
Sensor hubs and audio paths in Galaxy phones and wearables buffer data in fixed memory before the application processor wakes up. Interviewers use this to check index arithmetic, a full versus empty distinction and awareness of what changes in a real producer and consumer setting.
What the Interviewer Expects
- Uses head plus size (or head plus tail plus flag) to distinguish full from empty
- All operations O(1) with no reallocation
- Overwrite policy advances head and counts dropped samples
- latest(n) returns oldest-to-newest order and clamps n
- Discusses single-producer single-consumer concurrency in C
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you make this lock-free for one interrupt handler producer and one thread consumer?
- Why do firmware engineers often pick a power-of-two capacity?
- What changes if the consumer should block instead of losing data?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Capacity fixed at construction. push never fails: when full it replaces the oldest sample. pop returns None when empty. latest(n) returns up to n newest samples, oldest first....
High-Level Design
Store head (oldest index) and size. The write slot is (head + size) % cap. When full, writing there lands on the oldest sample, so advance head instead of size. This avoids the classic ambiguity...