Implement a String Deque
Company: Goldman Sachs
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: 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.
Constraints
- 0 <= len(operations) <= 100000
- 0 <= len(data) <= 100 for each inserted string
- All operation names are valid
- All deque operations must run in O(1) time
- Do not use a built-in deque or linked-list library
Examples
Input: ([('addFirst', 'apple'), ('addLast', 'banana'), ('peekFirst',), ('peekLast',), ('getSize',), ('removeFirst',), ('removeLast',), ('removeLast',)],)
Expected Output: ['apple', 'banana', 2, 'apple', 'banana', None]
Explanation: After adding apple to the front and banana to the back, the deque is [apple, banana]. Peeks return apple and banana, size is 2, then removals return apple, banana, and finally None because the deque is empty.
Input: ([('addLast', 'a'), ('removeFirst',), ('getSize',), ('addFirst', 'b'), ('addLast', 'c'), ('removeLast',), ('removeLast',), ('peekFirst',)],)
Expected Output: ['a', 0, 'c', 'b', None]
Explanation: Removing the only element a makes the deque empty. Later b and c are added, removed from the back as c then b, and the final peek returns None.
Input: ([('peekFirst',), ('peekLast',), ('removeFirst',), ('removeLast',), ('getSize',)],)
Expected Output: [None, None, None, None, 0]
Explanation: All peek and remove operations on an empty deque return None, and the size remains 0.
Input: ([('addFirst', ''), ('addLast', 'x'), ('addFirst', 'x'), ('getSize',), ('removeFirst',), ('removeLast',), ('removeFirst',), ('removeFirst',)],)
Expected Output: [3, 'x', 'x', '', None]
Explanation: This checks duplicates and the empty string. The deque becomes [x, '', x]. Size is 3, then removals return x, x, the empty string, and finally None.
Input: ([('addLast', 'one'), ('addFirst', 'zero'), ('addLast', 'two'), ('removeLast',), ('addLast', 'three'), ('removeFirst',), ('peekFirst',), ('peekLast',), ('getSize',)],)
Expected Output: ['two', 'zero', 'one', 'three', 2]
Explanation: This alternates front and back operations. After removing two from the back and zero from the front, the deque contains [one, three].
Input: ([],)
Expected Output: []
Explanation: No operations are executed, so there are no results.
Hints
- A singly linked list can remove efficiently from the front, but what extra pointer information is needed to remove from the back in O(1)?
- Be careful when the deque transitions between empty, one element, and multiple elements; both front and back pointers may need updates.