Implement a String Deque
Goldman SachsSoftware Engineer · Technical ScreenLast updated
This question evaluates competence with fundamental data structures and algorithms by requiring an implementation of a string double-ended queue (deque), checking understanding of constant-time (O(1)) operations, manipulation of node links or references, and correct handling of edge cases; category: Coding & Algorithms; level: practical implementation rather than purely conceptual. It is commonly asked to assess the ability to implement low-level data structure mechanics, guarantee performance bounds, and expose attention to correctness and boundary conditions in algorithmic coding tasks.
Implement a double-ended queue (Deque) that stores strings.
Your implementation must support the following operations:
addFirst(data): Insertdataat the front of the deque.addLast(data): Insertdataat the back of the deque.removeFirst(): Remove and return the front element. Returnnullif the deque is empty.removeLast(): Remove and return the back element. Returnnullif the deque is empty.peekFirst(): Return the front element without removing it. Returnnullif the deque is empty.peekLast(): Return the back element without removing it. Returnnullif the deque is empty.getSize(): Return the number of elements currently in the deque.
Requirements:
- Implement the data structure from scratch; do not use a built-in deque or linked-list library.
- All operations should run in
O(1)time. - The deque should correctly handle edge cases such as inserting into an empty deque, removing the only element, and alternating front/back operations.
- Add a small test method with several test cases covering normal operations and edge cases.
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.