Ring Buffer Double-Ended Queue Simulator
Data Structure DesignAuto-graded · 17 test casesLast updated
Simulate a bounded double-ended queue over a batch of commands using a circular buffer with constant-time operations and correct index wraparound.
Bounded Deque Simulator
You are asked to model a double-ended queue that can hold at most k integers. Rather than building a class, you write a single function that is given the capacity and a list of commands, runs them in order on a freshly created, empty deque, and reports what each command returned.
Commands
Each command is identified by its name:
| Command | Effect | Result |
|---|---|---|
insertFront | Put the given value at the front of the deque. | True if stored, False if the deque was already full. |
insertLast | Put the given value at the back of the deque. | True if stored, False if the deque was already full. |
deleteFront | Remove the element at the front. | True if an element was removed, False if the deque was empty. |
deleteLast | Remove the element at the back. | True if an element was removed, False if the deque was empty. |
getFront | Look at the front element without removing it. | The element, or -1 if the deque is empty. |
getRear | Look at the back element without removing it. | The element, or -1 if the deque is empty. |
isEmpty | Check whether the deque holds no elements. | True or False. |
isFull | Check whether the deque holds exactly k elements. | True or False. |
A command that fails (insert into a full deque, delete from an empty one) must leave the deque unchanged.
Input format
k: the capacity of the deque.operations: a list of command names.arguments: a list of the same length asoperations. The entry at indexiholds the arguments foroperations[i]. It is[value]forinsertFrontandinsertLast, and[]for every other command.
Output format
Return a list with exactly one entry per command, in the same order as operations. Do not add an entry for creating the deque. Entries are booleans for inserts, deletes, isEmpty and isFull, and integers for getFront and getRear.
Requirements
Each command should run in O(1) time. A plain Python list with insert(0, x) or pop(0) does not meet this, so you should manage the storage yourself and make sure indices wrap around correctly at both ends of the buffer.
Constraints
1 <= k <= 1000- All stored values are integers.
len(arguments) == len(operations)- Every command name is one of the eight listed above.
- Each command must take
O(1)time.
Examples
Example 1
Input: simulate_bounded_deque(3, ['insertLast', 'insertLast', 'insertFront', 'getRear', 'isFull', 'deleteFront', 'getFront'], [[1], [2], [3], [], [], [], []])
Output: [True, True, True, 2, True, True, 1]
Example 2
Input: simulate_bounded_deque(2, ['getFront', 'insertFront', 'insertLast', 'insertLast', 'getFront', 'getRear', 'deleteLast', 'isFull'], [[], [10], [20], [30], [], [], [], []])
Output: [-1, True, True, False, 10, 20, True, False]
Example 3
Input: simulate_bounded_deque(4, ['insertFront', 'insertFront', 'deleteLast', 'insertLast', 'insertLast', 'getFront', 'getRear', 'isEmpty'], [[1], [2], [], [3], [4], [], [], []])
Output: [True, True, True, True, True, 2, 4, False]
Hints
Hint 1
Allocate a list of length k and treat it as a ring: every index you compute should be taken modulo k.
Hint 2
Track the index of the front element and the current element count. Using a count instead of a separate rear pointer avoids confusing an empty ring with a full one.
Hint 3
The rear index is (front + size - 1) % k, and the next free slot at the back is (front + size) % k. Inserting at the front moves front to (front - 1) % k, so make sure going below 0 wraps to k - 1.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Store the elements in a fixed array of length k that is treated as a ring. Keep just two integers:
front: the index of the first element.size: how many elements are currently stored.
The rear is not stored; it is computed as (front + size - 1) % k. The slot where a new back element goes is (front + size) % k.
| Command | Action |
|---|---|
insertFront(v) | if not full: front = (front - 1) % k, write v at front, size += 1 |
insertLast(v) | if not full: write v at (front + size) % k, size += 1 |
deleteFront() | if not empty: front = (front + 1) % k, size -= 1 |
deleteLast() | if not empty: size -= 1 |
getFront() | data[front], or -1 if empty |
getRear() | data[(front + size - 1) % k], or -1 if empty |
isEmpty() / isFull() | size == 0 / size == k |
Deleted slots are not cleared. They lie outside the live window and are overwritten by later inserts.
Why it is correct
The invariant is that the deque contents from front to back are exactly data[front], data[(front+1) % k], ..., data[(front+size-1) % k], with 0 <= size <= k.
- Inserts happen only when
size < k, so the slot they write into (just before the window, or just after it) is not part of the window. After the write, the window grows by one slot on the correct side. - Deletes shrink the window by exactly one element from the chosen side.
- Reads index directly into the window and are guarded by the empty check.
- Because emptiness and fullness are decided by
size, there is no ambiguity between the two states, which is the usual trap with separate front and rear pointers. - In Python,
(0 - 1) % k == k - 1, so inserting at the front from index 0 wraps correctly. In languages where%can return negatives, use(front - 1 + k) % k.
Complexity
- Time:
O(1)per command, soO(n)forncommands. - Space:
O(k)for the buffer plusO(n)for the result list.
Reference solution
from typing import List
def simulate_bounded_deque(k: int, operations: List[str], arguments: List[List[int]]) -> List[object]:
data = [0] * k
front = 0
size = 0
results = []
for operation, args in zip(operations, arguments):
if operation == "insertFront":
if size == k:
results.append(False)
else:
front = (front - 1) % k
data[front] = args[0]
size += 1
results.append(True)
elif operation == "insertLast":
if size == k:
results.append(False)
else:
data[(front + size) % k] = args[0]
size += 1
results.append(True)
elif operation == "deleteFront":
if size == 0:
results.append(False)
else:
front = (front + 1) % k
size -= 1
results.append(True)
elif operation == "deleteLast":
if size == 0:
results.append(False)
else:
size -= 1
results.append(True)
elif operation == "getFront":
results.append(-1 if size == 0 else data[front])
elif operation == "getRear":
results.append(-1 if size == 0 else data[(front + size - 1) % k])
elif operation == "isEmpty":
results.append(size == 0)
elif operation == "isFull":
results.append(size == k)
return results