Problem, as an interviewer would state it: "Implement a double-ended queue — pushFront, pushBack, popFront, popBack, all genuinely O(1). An array with unshift/push/shift/pop looks tempting, but does it actually give you O(1) on every operation?"
Examples:
const d = new Deque();
d.pushBack(1); d.pushBack(2); d.pushFront(0);
d.popFront(); // 0
d.popBack(); // 2
Clarifying questions expected:
- Does every operation genuinely need to be O(1), or is O(n) acceptable for some of them?
- Should popping from an empty deque throw, or return a sentinel like
undefined? - Is random-access indexing into the deque ever needed, or purely front/back operations?
Code / implementation expected: Yes — real, direct proof that a naive array-based deque's front operations are genuinely O(n), measurably slower than a linked-list-based version's genuinely O(1) operations.