KatInterview

Replay Operations on a Fixed-Size Ring Buffer Queue

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designcircular-queuering-buffersimulation

Last updated

Simulate a fixed-capacity circular FIFO queue from an operation log, handling wraparound, full and empty states, and logical index lookups in constant time per operation.

Ring Buffer Queue Replay

A ring buffer is a fixed-size array used as a FIFO queue. New items are written after the last live item, wrapping to the start of the array when the end is reached. Because the array is allocated once and never resized, it avoids per-element allocation and keeps its data contiguous in memory, which makes it a popular choice for high-throughput message passing.

You are given a log of operations to apply, in order, to one such queue. Return the result of every operation.

Operations

Each operation is a string in operations, and the entry at the same position in arguments is its argument list.

OperationArgumentsBehaviorResult
"init"[capacity]Create an empty queue that can hold at most capacity items.None
"enqueue"[val]Add val at the back. If the queue already holds capacity items, do nothing.True if added, False if the queue was full
"dequeue"[]Remove the front (oldest) item.The removed value, or -1 if the queue is empty
"get"[index]Read, without removing, the item index positions behind the front (0 is the front).The value, or -1 if index is not smaller than the current number of items
"is_empty"[]Check whether the queue holds no items.True or False
"is_full"[]Check whether the queue holds exactly capacity items.True or False

Details

  • The first operation is always "init".
  • A rejected enqueue on a full queue must not change the queue in any way. Existing items are never overwritten.
  • get is judged against the number of items currently stored, not against the capacity. Slots that held items removed earlier are not valid positions.
  • Values stored in the queue are never negative, so -1 is unambiguous as a "nothing here" marker.
  • Aim for constant time per operation, using one buffer allocated up front.

Output

Return a list with one entry per operation, in the same order as the input. The entry for "init" is None.

Constraints

  • 1 <= capacity <= 10^5
  • 0 <= val <= 10^9
  • 0 <= index < capacity
  • operations and arguments have the same length.
  • Each operation should run in O(1) time and the buffer must not be reallocated after init.

Examples

Example 1

Input: replay_ring_queue(['init', 'enqueue', 'enqueue', 'enqueue', 'enqueue', 'get', 'dequeue', 'enqueue', 'get'], [[3], [10], [20], [30], [40], [1], [], [40], [2]])

Output: [None, True, True, True, False, 20, 10, True, 40]

Example 2

Input: replay_ring_queue(['init', 'is_empty', 'dequeue', 'enqueue', 'is_full', 'get', 'get', 'dequeue', 'dequeue', 'is_empty'], [[1], [], [], [7], [], [0], [1], [], [], []])

Output: [None, True, -1, True, True, 7, -1, 7, -1, True]

Hints

Hint 1

Keep three pieces of state: the buffer, the physical index of the front item, and the count of stored items.

Hint 2

The next free slot is (front + size) % capacity, and the item at logical offset i lives at (front + i) % capacity. Tracking size explicitly avoids confusing a full queue with an empty one.

Hint 3

For get, validate the index against the current size rather than the capacity, since stale slots may still contain old values.