KatInterview

Circular Buffer Deque with Auto-Resize

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designdequecircular-arrayamortized-analysis

Last updated

Tests ability to build a double-ended queue on a ring-indexed array that doubles in capacity when full, keeping all operations amortized O(1).

Circular Buffer Deque with Auto-Resize

Build a double-ended queue (deque) using a circular array as the backing store. The array must grow automatically when it runs out of room.

A driver function replays a sequence of named operations on a single deque instance and collects one output value per operation. You implement that driver.

Operations

NameArgument(s)What it doesOutput
pushFrontvalueInsert value at the frontNone
pushBackvalueInsert value at the backNone
popFront—Remove and return the front elementremoved value, or None if empty
popBack—Remove and return the back elementremoved value, or None if empty
peekFront—Return the front element without removing itfront value, or None if empty
peekBack—Return the back element without removing itback value, or None if empty
isEmpty—Check whether the deque holds any elementsTrue or False
size—Count of elements currently storedinteger

pop and peek on an empty deque must return None without raising an exception. An unrecognised operation name must raise ValueError.

Implementation requirements

  • Use a plain array (Python list) as internal storage.
  • Index into the array using circular (modulo) arithmetic so that both ends can be reached in O(1).
  • Start with an initial capacity of 4. When the array is full and a push arrives, double the capacity: allocate a new array of twice the size, copy all elements in logical front-to-back order into the new array starting at index 0, and reset the front pointer to 0.
  • Every operation must run in O(1) amortized time.

Function signature

def circular_deque(operations: List[str], arguments: List[List[int]]) -> List[object]:
  • operations: a list of operation name strings (see table above).
  • arguments: a parallel list; each entry is a list. For operations that take a value argument the list contains one integer; for operations with no argument the list is empty.
  • Returns a list with one entry per operation, following the output rules in the table.

Constraints

  • 1 ≤ len(operations) ≤ 10,000
  • len(operations) == len(arguments)
  • Each value passed to a push operation is a 32-bit signed integer.
  • The only valid operation names are the eight listed above; any other name must raise ValueError.
  • Initial backing-array capacity is exactly 4; capacity doubles on every resize.
  • pop and peek on an empty deque return None (no exception).

Examples

Example 1

Input: circular_deque(['pushBack', 'pushBack', 'peekFront', 'peekBack', 'size', 'popFront', 'popBack', 'isEmpty'], [[1], [2], [], [], [], [], [], []])

Output: [None, None, 1, 2, 2, 1, 2, True]

Example 2

Input: circular_deque(['pushFront', 'pushFront', 'pushBack', 'popBack', 'peekFront', 'size'], [[10], [20], [30], [], [], []])

Output: [None, None, None, 30, 20, 2]

Example 3

Input: circular_deque(['popFront', 'peekBack', 'isEmpty', 'pushBack', 'popBack', 'popBack'], [[], [], [], [-5], [], []])

Output: [None, None, True, None, -5, None]

Hints

Hint 1

Track the deque's state with just two variables — the physical index of the front element and the current count of stored elements. The back index is always derivable from those two, which sidesteps the empty-vs-full ambiguity you get when storing both front and back pointers.

Hint 2

For pushFront, move the front pointer one slot backward before writing: `front = (front - 1) % capacity`. Python's modulo always returns a non-negative result, so this wraps correctly even when front is 0.

Hint 3

When the array is full and needs to grow, don't copy the raw physical layout into the new array — copy in *logical* order (element 0 through element size-1 using the circular formula) and set front back to 0. If the contents have wrapped around, a straight copy will corrupt every subsequent index calculation.