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.
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.