KatInterview

Ring Buffer Double-Ended Queue Simulator

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designdequecircular-buffersimulation

Last 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:

CommandEffectResult
insertFrontPut the given value at the front of the deque.True if stored, False if the deque was already full.
insertLastPut the given value at the back of the deque.True if stored, False if the deque was already full.
deleteFrontRemove the element at the front.True if an element was removed, False if the deque was empty.
deleteLastRemove the element at the back.True if an element was removed, False if the deque was empty.
getFrontLook at the front element without removing it.The element, or -1 if the deque is empty.
getRearLook at the back element without removing it.The element, or -1 if the deque is empty.
isEmptyCheck whether the deque holds no elements.True or False.
isFullCheck 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 as operations. The entry at index i holds the arguments for operations[i]. It is [value] for insertFront and insertLast, 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.