Implement a Fixed-Capacity Deque
Applied IntuitionMachine Learning Engineer · Technical ScreenLast updated
This question evaluates proficiency with double-ended queue semantics, fixed-capacity data structure design, constant-time (O(1)) operations, and correct handling of boundary conditions and wraparound.
Design and implement a fixed-capacity double-ended queue.
The data structure is initialized with an integer capacity k and must support the following operations in O(1) time:
insertFront(value): Insertvalueat the front. Returntrueif successful, orfalseif the deque is full.insertLast(value): Insertvalueat the back. Returntrueif successful, orfalseif the deque is full.deleteFront(): Delete the front element. Returntrueif successful, orfalseif the deque is empty.deleteLast(): Delete the back element. Returntrueif successful, orfalseif the deque is empty.getFront(): Return the front element, or-1if the deque is empty.getRear(): Return the back element, or-1if the deque is empty.isEmpty(): Return whether the deque is empty.isFull(): Return whether the deque is full.
You may assume 1 <= k <= 1000 and values are integers. Explain your representation and handle wraparound correctly.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.