Design a circular queue data structure
Company: Optiver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of circular buffer data structures and the ability to implement fixed-capacity queue operations with O(1) time and O(k) space guarantees, testing skills in index management and state representation within the Coding & Algorithms domain.
Constraints
- 1 <= k <= 100000
- 1 <= len(operations) <= 200000
- For ('enq', x), x is an integer in the 32-bit signed range
- operations contain only: 'enq', 'deq', 'front', 'rear', 'isEmpty', 'isFull'
Examples
Input: (3, [('enq', 1), ('enq', 2), ('enq', 3), ('enq', 4), ('rear',), ('isFull',), ('deq',), ('enq', 4), ('rear',), ('front',)])
Expected Output: [True, True, True, False, 3, True, True, True, 4, 2]
Explanation: Queue fills to capacity 3; enq(4) fails. After one deq, enq(4) succeeds and wraps around.
Input: (1, [('deq',), ('front',), ('enq', 5), ('isFull',), ('enq', 6), ('rear',), ('deq',), ('isEmpty',)])
Expected Output: [False, -1, True, True, False, 5, True, True]
Explanation: Capacity 1: cannot dequeue or read front when empty; cannot enqueue when full.
Input: (2, [('enq', 1), ('enq', 2), ('deq',), ('enq', 3), ('front',), ('rear',)])
Expected Output: [True, True, True, True, 2, 3]
Explanation: After removing 1, enqueue 3 goes into the freed slot via wrap-around.
Input: (4, [('isEmpty',), ('isFull',), ('front',), ('rear',)])
Expected Output: [True, False, -1, -1]
Explanation: Fresh queue is empty, not full, and has no front/rear values.