Ring Buffer for High-Rate Sensor Data With Window Queries

Last updated: November 29, 2026

Quick Overview

Implement a fixed-capacity ring buffer that stores timestamped sensor samples, overwrites the oldest sample when full, and supports fetching the latest k samples and all samples in a time range [t0, t1] efficiently.

Figure AI
Coding & Algorithms
Software Engineer
Figure AI
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement a fixed-capacity ring buffer that stores timestamped sensor samples, overwrites the oldest sample when full, and supports fetching the latest k samples and all samples in a time range [t0, t1] efficiently.

On a humanoid, joint encoders, force sensors and IMUs publish at hundreds of hertz, and both the control stack and the 'what just happened' fault logger need recent history without allocating memory in the hot path. A Figure AI onsite for middleware or embedded software could use this to check index arithmetic, invariants, and awareness of real-time constraints.

What the Interviewer Expects
  • Preallocates storage and never grows it
  • Keeps head and size invariants correct across wraparound
  • Implements window lookup in O(log n + k) using the monotone timestamps
  • Rejects or handles out-of-order timestamps explicitly
  • Discusses thread safety for one producer and one consumer
Key Topics to Cover
Circular buffers
Binary search on rotated storage
Real-time memory discipline
Lock-free SPSC
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 make this safe for one writer thread and one reader thread without a mutex?
  • How would you dump the last 10 seconds to disk when a fault is detected, without blocking the control loop?
  • How would you change it if samples could arrive slightly out of order from a CAN bus?
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
  • push(t, v) in O(1), overwriting the oldest sample once full.
  • latest(k) returns the newest k samples, oldest first.
  • window(t0, t1) returns samples with t0 <= t <= t1.
  • Timestamps are non...
High-Level Design

Store parallel preallocated arrays for timestamps and values. Keep head (index of the oldest element) and size. The logical index k maps to the physical slot (head + k) % cap. Because timestam...


Submit Your Answer
Markdown supported

Related Questions