Memory-Efficient Circular Frame Buffer

Quick Overview

Design a fixed-capacity ring buffer for video frames on a memory-constrained device, where pushing into a full buffer overwrites the oldest frame. It tests index arithmetic with wraparound, telling a full buffer from an empty one, and keeping push, pop and peek operations constant time.

Memory-Efficient Circular Frame Buffer

Company: Anduril

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a fixed-capacity **circular (ring) buffer** for storing video frames on a device with highly constrained RAM. The buffer pre-allocates exactly `capacity` slots and reuses them, so memory usage is bounded regardless of how many frames are pushed over its lifetime. When a new frame is pushed into a full buffer, the oldest frame is overwritten by the newest. Frames are integer identifiers. The buffer supports pushing frames, popping the oldest frame (FIFO order), peeking at the oldest and newest frames, querying size, and reading the current contents in oldest-to-newest order. All operations must run in `O(1)` time, except `snapshot`, which is `O(n)` in the current size. Implement `CircularFrameBuffer(capacity)` with the following methods: - `push(frame: int)`: Append a new frame to the buffer. If the buffer is full, overwrite the oldest frame. - `pop() -> int`: Remove and return the oldest frame in FIFO order. Returns -1 if the buffer is empty. - `peekOldest() -> int`: Return the oldest frame without removing it. Returns -1 if the buffer is empty. - `peekNewest() -> int`: Return the most recently pushed frame without removing it. Returns -1 if the buffer is empty. - `size() -> int`: Return the current number of frames stored in the buffer. - `snapshot() -> List[int]`: Return a list of all frames currently in the buffer in oldest-to-newest order. ```hint State to keep Look for the smallest set of integers that tells you where the oldest frame is and where the next write goes, without ever moving frames that are already stored. ``` ```hint Full versus empty If you keep only a read position and a write position, a full buffer and an empty buffer can look the same. Decide how your design tells them apart, and check the order of steps when a push overwrites. ``` ### Examples Example 1: ```text Input: ["CircularFrameBuffer", "push", "push", "push", "snapshot", "push", "snapshot", "peekOldest", "peekNewest", "pop", "size"] [[3], [10], [20], [30], [], [40], [], [], [], [], []] Output: [null, null, null, null, [10, 20, 30], null, [20, 30, 40], 20, 40, 20, 2] Explanation: buf = CircularFrameBuffer(3) # capacity = 3 buf.push(10) # buf: [10] buf.push(20) # buf: [10, 20] buf.push(30) # buf: [10, 20, 30] (full) buf.snapshot() # -> [10, 20, 30] buf.push(40) # full: overwrites 10, buf: [20, 30, 40] buf.snapshot() # -> [20, 30, 40] buf.peekOldest() # -> 20 buf.peekNewest() # -> 40 buf.pop() # -> 20 (buf: [30, 40]) buf.size() # -> 2 ``` Example 2: ```text Input: ["CircularFrameBuffer", "pop", "peekOldest", "peekNewest", "push", "push", "pop", "push", "push", "snapshot", "size"] [[2], [], [], [], [1], [2], [], [3], [4], [], []] Output: [null, -1, -1, -1, null, null, 1, null, null, [3, 4], 2] Explanation: buf = CircularFrameBuffer(2) # capacity = 2 buf.pop() # -> -1 (empty) buf.peekOldest() # -> -1 (empty) buf.peekNewest() # -> -1 (empty) buf.push(1) # buf: [1] buf.push(2) # buf: [1, 2] (full) buf.pop() # -> 1 (buf: [2]) buf.push(3) # buf: [2, 3] (full) buf.push(4) # overwrites 2, buf: [3, 4] buf.snapshot() # -> [3, 4] buf.size() # -> 2 ``` ### Constraints & Assumptions - `1 <= capacity <= 10⁵` - At most `10⁵` calls will be made across all operations. - `-2³¹ <= frame <= 2³¹ - 1` - `pop`, `peekOldest`, and `peekNewest` return `-1` when the buffer is empty. ### What a Strong Answer Covers - The state kept (an index for the oldest frame plus a count, or an equivalent pair) and how it locates both ends of the buffer. - How a full buffer is told apart from an empty one. - The overwrite path in `push`: which slot receives the new frame, how the oldest position moves, and the order of those steps. - Index wraparound with the capacity as modulus, including capacity 1 and the empty-buffer return values. - Why memory stays at O(capacity): one allocation up front and no growth or copying per push. ### Follow-up Questions - The frame range includes -1, so `pop()` returning -1 is ambiguous. How would you change the API so an empty buffer cannot be confused with a real frame? - A camera thread pushes frames while an encoder thread pops them. How would you make the buffer safe for that, and what does the overwrite policy do to a lock-free single-producer, single-consumer design? - If each frame were a large byte payload instead of an integer id, how would you store frames so that `push` never allocates? - What changes if the capacity is restricted to a power of two?

Overview: Design a fixed-capacity ring buffer for video frames on a memory-constrained device, where pushing into a full buffer overwrites the oldest frame. It tests index arithmetic with wraparound, telling a full buffer from an empty one, and keeping push, pop and peek operations constant time.

Anduril logo
Anduril
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a fixed-capacity circular (ring) buffer for storing video frames on a device with highly constrained RAM. The buffer pre-allocates exactly capacity slots and reuses them, so memory usage is bounded regardless of how many frames are pushed over its lifetime.

When a new frame is pushed into a full buffer, the oldest frame is overwritten by the newest. Frames are integer identifiers. The buffer supports pushing frames, popping the oldest frame (FIFO order), peeking at the oldest and newest frames, querying size, and reading the current contents in oldest-to-newest order.

All operations must run in O(1) time, except snapshot, which is O(n) in the current size.

Implement CircularFrameBuffer(capacity) with the following methods:

  • push(frame: int) : Append a new frame to the buffer. If the buffer is full, overwrite the oldest frame.
  • pop() -> int : Remove and return the oldest frame in FIFO order. Returns -1 if the buffer is empty.
  • peekOldest() -> int : Return the oldest frame without removing it. Returns -1 if the buffer is empty.
  • peekNewest() -> int : Return the most recently pushed frame without removing it. Returns -1 if the buffer is empty.
  • size() -> int : Return the current number of frames stored in the buffer.
  • snapshot() -> List[int] : Return a list of all frames currently in the buffer in oldest-to-newest order.

Examples

Example 1:

Input:
["CircularFrameBuffer", "push", "push", "push", "snapshot", "push", "snapshot", "peekOldest", "peekNewest", "pop", "size"]
[[3],                   [10],   [20],   [30],   [],         [40],   [],         [],           [],           [],    []]

Output: [null, null, null, null, [10, 20, 30], null, [20, 30, 40], 20, 40, 20, 2]

Explanation:
buf = CircularFrameBuffer(3)         # capacity = 3

buf.push(10)                         # buf: [10]
buf.push(20)                         # buf: [10, 20]
buf.push(30)                         # buf: [10, 20, 30] (full)
buf.snapshot()                       # -> [10, 20, 30]
buf.push(40)                         # full: overwrites 10, buf: [20, 30, 40]
buf.snapshot()                       # -> [20, 30, 40]
buf.peekOldest()                     # -> 20
buf.peekNewest()                     # -> 40
buf.pop()                            # -> 20   (buf: [30, 40])
buf.size()                           # -> 2

Example 2:

Input:
["CircularFrameBuffer", "pop", "peekOldest", "peekNewest", "push", "push", "pop", "push", "push", "snapshot", "size"]
[[2],                   [],    [],           [],           [1],    [2],    [],    [3],    [4],    [],         []]

Output: [null, -1, -1, -1, null, null, 1, null, null, [3, 4], 2]

Explanation:
buf = CircularFrameBuffer(2)         # capacity = 2

buf.pop()                            # -> -1   (empty)
buf.peekOldest()                     # -> -1   (empty)
buf.peekNewest()                     # -> -1   (empty)
buf.push(1)                          # buf: [1]
buf.push(2)                          # buf: [1, 2] (full)
buf.pop()                            # -> 1    (buf: [2])
buf.push(3)                          # buf: [2, 3] (full)
buf.push(4)                          # overwrites 2, buf: [3, 4]
buf.snapshot()                       # -> [3, 4]
buf.size()                           # -> 2

Constraints & Assumptions

  • 1 <= capacity <= 10⁵
  • At most 10⁵ calls will be made across all operations.
  • -2³¹ <= frame <= 2³¹ - 1
  • pop , peekOldest , and peekNewest return -1 when the buffer is empty.

What a Strong Answer Covers Guidance

  • The state kept (an index for the oldest frame plus a count, or an equivalent pair) and how it locates both ends of the buffer.
  • How a full buffer is told apart from an empty one.
  • The overwrite path in push : which slot receives the new frame, how the oldest position moves, and the order of those steps.
  • Index wraparound with the capacity as modulus, including capacity 1 and the empty-buffer return values.
  • Why memory stays at O(capacity): one allocation up front and no growth or copying per push.

Follow-up Questions Guidance

  • The frame range includes -1, so pop() returning -1 is ambiguous. How would you change the API so an empty buffer cannot be confused with a real frame?
  • A camera thread pushes frames while an encoder thread pops them. How would you make the buffer safe for that, and what does the overwrite policy do to a lock-free single-producer, single-consumer design?
  • If each frame were a large byte payload instead of an integer id, how would you store frames so that push never allocates?
  • What changes if the capacity is restricted to a power of two?