Design a queue and analyze tradeoffs
Company: Optiver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of FIFO data structures, algorithmic complexity analysis, memory and cache behavior, and concurrent access concerns.
Constraints
- 0 <= capacity <= 10^5
- 0 <= len(operations) <= 2 * 10^5
- Queue values are integers in the range [-10^9, 10^9]
- Each operation is one of 'enqueue', 'dequeue', 'peek', or 'isEmpty'
- Assume single-threaded execution; concurrency discussion is a follow-up, not part of the implementation
Examples
Input: ([('enqueue', 1), ('enqueue', 2), ('peek',), ('dequeue',), ('enqueue', 3), ('dequeue',), ('dequeue',), ('isEmpty',)], 2)
Expected Output: [True, True, 1, 1, True, 2, 3, True]
Explanation: After removing 1, the enqueue of 3 wraps around into the freed slot. The remaining dequeues return 2 and then 3.
Input: ([('enqueue', 5), ('enqueue', 6), ('enqueue', 7), ('isEmpty',), ('peek',), ('dequeue',), ('dequeue',), ('dequeue',)], 2)
Expected Output: [True, True, False, False, 5, 5, 6, 'underflow']
Explanation: The third enqueue fails because the queue is full. After removing 5 and 6, one more dequeue underflows.
Input: ([('isEmpty',), ('enqueue', 10), ('peek',), ('dequeue',)], 0)
Expected Output: [True, False, 'underflow', 'underflow']