KatInterview

Simulate a Ring Buffer Queue from a Command Log

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

Last updated

Replay a list of queue commands against a bounded ring buffer, returning each command's result while handling wrap-around, full and empty states correctly.

Simulate a Ring Buffer Queue from a Command Log

You are given the capacity of a bounded first-in-first-out queue and a log of commands to run against it. The queue starts empty and can never hold more than k items. Process the commands in order and report the outcome of each one.

Your queue should be backed by a fixed-size array whose positions are reused cyclically, so each command takes constant time.

Function

Implement ring_queue_replay(capacity, commands, params), where:

  • capacity is the maximum number of items the queue may hold (k).
  • commands is a list of command names.
  • params[i] is the argument list for commands[i]: it is [x] (one integer) for "enQueue" and [] for every other command.

Return a list with exactly one result per command, in the same order.

Commands

CommandBehaviorResult
"enQueue"Append x at the back if there is room. Never overwrite existing items.True if added, False if the queue was full
"deQueue"Remove the oldest item if there is one.True if an item was removed, False if the queue was empty
"Front"Look at the oldest item without removing it.The item, or -1 if the queue is empty
"Rear"Look at the newest item without removing it.The item, or -1 if the queue is empty
"isEmpty"Check whether the queue holds no items.True or False
"isFull"Check whether the queue holds exactly capacity items.True or False

A command that fails (an enQueue on a full queue or a deQueue on an empty queue) must leave the queue unchanged.

If a command name is not one of the six above, raise a ValueError.

Notes on edge cases

  • When capacity is 0, every enQueue fails, Front and Rear return -1, and both isEmpty and isFull return True.
  • Returned -1 values from Front and Rear are just a sentinel for an empty queue.
  • Slots freed by deQueue must be reusable by later enQueue calls, including after the write position wraps past the end of the array.

Constraints

  • 0 <= capacity
  • len(commands) == len(params)
  • Each command name is a string; the valid names are enQueue, deQueue, Front, Rear, isEmpty, isFull.
  • For enQueue, params[i] holds exactly one integer; for all other commands it is empty.
  • Each command must run in O(1) time, so the whole log is processed in O(n) for n commands.

Examples

Example 1

Input: ring_queue_replay(3, ['enQueue', 'enQueue', 'enQueue', 'enQueue', 'Rear', 'isFull', 'deQueue', 'enQueue', 'Rear'], [[1], [2], [3], [4], [], [], [], [4], []])

Output: [True, True, True, False, 3, True, True, True, 4]

Example 2

Input: ring_queue_replay(1, ['enQueue', 'deQueue', 'Front', 'Rear', 'isEmpty', 'isFull'], [[9], [], [], [], [], []])

Output: [True, True, -1, -1, True, False]

Hints

Hint 1

A plain pair of head and tail indices cannot tell an empty buffer from a full one, because both look like head == tail. Think about what extra piece of state removes the ambiguity.

Hint 2

Store only the index of the oldest item and a count of how many items are held. The slot for the next write is (head + count) % capacity, and the newest item sits at (head + count - 1) % capacity.

Hint 3

Check for empty or full before doing any index arithmetic. This avoids stale reads for Rear on an empty queue and a modulo by zero when capacity is 0.