Implement a Resizable Deque
Company: Tml
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in data structures and array-based implementations, specifically building a resizable double-ended queue (deque) backed by a circular array and understanding amortized O(1) operation costs.
Constraints
- 1 <= number of operations <= 10^5
- Values fit in a 32-bit signed integer (negatives allowed).
- popFront/popBack/peekFront/peekBack on an empty deque must return None (no exception thrown by the driver).
- All operations must run in O(1) amortized time.
Examples
Input: (['pushBack', 'pushBack', 'pushFront', 'peekFront', 'peekBack', 'size', 'popFront', 'popBack', 'popFront', 'isEmpty'], [[1], [2], [0], [], [], [], [], [], [], []])
Expected Output: [None, None, None, 0, 2, 3, 0, 2, 1, True]
Explanation: After pushBack(1), pushBack(2), pushFront(0) the deque is [0, 1, 2]. peekFront=0, peekBack=2, size=3. popFront removes 0, popBack removes 2, popFront removes 1, leaving it empty so isEmpty=True.
Input: (['isEmpty', 'size', 'popFront', 'popBack', 'peekFront', 'peekBack'], [[], [], [], [], [], []])
Expected Output: [True, 0, None, None, None, None]
Explanation: Edge case: every operation on a brand-new empty deque. isEmpty=True, size=0, and all pop/peek operations return None instead of erroring.
Input: (['pushBack', 'pushBack', 'pushBack', 'pushBack', 'pushBack', 'pushBack', 'size', 'peekFront', 'peekBack'], [[10], [20], [30], [40], [50], [60], [], [], []])
Expected Output: [None, None, None, None, None, None, 6, 10, 60]
Explanation: Six pushBack calls exceed the initial capacity (4), forcing at least one resize. After resizing, logical order is preserved: front=10, back=60, size=6.
Input: (['pushFront', 'pushFront', 'pushFront', 'pushFront', 'pushFront', 'popBack', 'popBack', 'peekFront', 'size'], [[1], [2], [3], [4], [5], [], [], [], []])
Expected Output: [None, None, None, None, None, 1, 2, 5, 3]
Explanation: Five pushFront calls build [5, 4, 3, 2, 1] and trigger a resize. popBack twice removes 1 then 2; the new front (most recent pushFront) is 5, and size is 3.
Input: (['pushFront', 'popFront', 'isEmpty', 'pushBack', 'peekBack', 'popBack', 'isEmpty'], [[-7], [], [], [-9], [], [], []])
Expected Output: [None, -7, True, None, -9, -9, True]
Explanation: Negative values and alternating ends: pushFront(-7) then popFront returns -7, deque empty. pushBack(-9), peekBack=-9, popBack returns -9, deque empty again.
Hints
- Track three things: the backing array, the index of the front element (head), and the current element count. The back index is always (head + count - 1) modulo capacity.
- pushFront moves head one step counter-clockwise: head = (head - 1) mod cap. pushBack writes at (head + count) mod cap. Use modulo so the indices wrap around the circular array.
- Resize only when count == capacity: allocate a 2x array, copy the count elements in logical order starting from head, then reset head to 0. This keeps the amortized cost O(1).
- For the empty case, check count == 0 before any pop/peek and return None instead of indexing into the array.