Implement a fixed-capacity queue using a circular array
Quick Overview
Implement a fixed-capacity FIFO queue on a pre-allocated circular array, with enqueue, dequeue, peek, isEmpty, isFull and size each running in constant time. It tests ring buffer index arithmetic, telling a full queue from an empty one, and handling calls on an empty or full queue.
Implement a fixed-capacity queue using a circular array
Company: NVIDIA
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design and implement a queue data structure using a fixed-size array. The goal is to minimize memory fragmentation by pre-allocating a contiguous block of memory.
The queue should follow FIFO (first in, first out) ordering. Use a circular array (ring buffer) approach so that space freed by dequeue operations can be reused without shifting elements.
The queue is initialized with a given `capacity` representing the maximum number of elements it can hold at any time. Implement a class `MyQueue` with these methods:
- `MyQueue(capacity: int)`: create an empty queue that holds at most `capacity` elements.
- `enqueue(value: int) -> bool`: add an element to the back of the queue. Return `True` if the operation is successful, or `False` if the queue is full.
- `dequeue() -> int`: remove and return the front element of the queue. Return `-1` if the queue is empty.
- `peek() -> int`: return the front element without removing it. Return `-1` if the queue is empty.
- `isEmpty() -> bool`: return `True` if the queue contains no elements, `False` otherwise.
- `isFull() -> bool`: return `True` if the queue has reached its maximum capacity, `False` otherwise.
- `size() -> int`: return the current number of elements in the queue.
In the examples, `operations` lists the calls in order, `arguments` holds the arguments of each call, and the output holds each call's return value, with `null` for the constructor.
Example 1:
```text
operations = ["MyQueue","enqueue","enqueue","enqueue","peek","dequeue","enqueue","peek","isFull","size","dequeue","dequeue","dequeue","isEmpty"]
arguments = [[3],[10],[20],[30],[],[],[40],[],[],[],[],[],[],[]]
Output: [null,true,true,true,10,10,true,20,true,3,20,30,40,true]
```
Create a queue with capacity 3. Enqueue 10, 20 and 30 (all return true). Peek returns 10. Dequeue returns 10. Enqueue 40 succeeds by wrapping around and returns true. Peek returns 20. isFull returns true and size returns 3. The next three dequeues return 20, 30 and 40 in order, and isEmpty returns true.
Example 2:
```text
operations = ["MyQueue","isEmpty","dequeue","peek","enqueue","enqueue","isFull","enqueue","isEmpty","size"]
arguments = [[2],[],[],[],[5],[10],[],[15],[],[]]
Output: [null,true,-1,-1,true,true,true,false,false,2]
```
Create a queue with capacity 2. isEmpty returns true on the empty queue, and dequeue and peek both return -1. Enqueue 5 and 10 both succeed, and isFull returns true. Enqueue 15 fails and returns false because the queue is full. isEmpty returns false and size returns 2.
```hint Telling full from empty
Suppose you track only a front index and a rear index. Look at what they hold when the queue is empty and when it is full, then decide what extra state lets you tell those two cases apart.
```
### Constraints & Assumptions
- `1 <= capacity <= 10⁴`
- `-10⁹ <= value <= 10⁹`
- At most `10⁵` total calls will be made across all methods.
- `dequeue` and `peek` return `-1` when the queue is empty.
- `enqueue` returns `False` when the queue is full.
### Clarifying Questions to Ask
- Values can be `-1`, which is also the empty signal from `dequeue` and `peek`. Is that ambiguity acceptable, or should the API change?
- Will more than one thread call the queue at the same time?
- Does a dequeued slot need to be cleared, for example to release object references, or can the old value stay until it is overwritten?
### What a Strong Answer Covers
- How the front and rear positions advance and wrap at the end of the array.
- How the implementation tells a full queue from an empty one.
- The boundary calls: `dequeue` and `peek` on an empty queue, `enqueue` on a full queue, and a capacity of 1.
- The time cost of each operation and the total memory used.
- A trace of an example through the wrap-around that confirms the indices.
### Follow-up Questions
- How would you change the API so a caller can tell an empty queue apart from a stored `-1`?
- If the capacity is always a power of two, how can you avoid the modulo operation?
- How would you make the queue safe for one producer thread and one consumer thread, and what changes with several producers?
- How would you let the queue grow when it is full while keeping FIFO order?
Overview: Implement a fixed-capacity FIFO queue on a pre-allocated circular array, with enqueue, dequeue, peek, isEmpty, isFull and size each running in constant time. It tests ring buffer index arithmetic, telling a full queue from an empty one, and handling calls on an empty or full queue.
Design and implement a queue data structure using a fixed-size array. The goal is to minimize memory fragmentation by pre-allocating a contiguous block of memory.
The queue should follow FIFO (first in, first out) ordering. Use a circular array (ring buffer) approach so that space freed by dequeue operations can be reused without shifting elements.
The queue is initialized with a given capacity representing the maximum number of elements it can hold at any time. Implement a class MyQueue with these methods:
MyQueue(capacity: int)
: create an empty queue that holds at most
capacity
elements.
enqueue(value: int) -> bool
: add an element to the back of the queue. Return
True
if the operation is successful, or
False
if the queue is full.
dequeue() -> int
: remove and return the front element of the queue. Return
-1
if the queue is empty.
peek() -> int
: return the front element without removing it. Return
-1
if the queue is empty.
isEmpty() -> bool
: return
True
if the queue contains no elements,
False
otherwise.
isFull() -> bool
: return
True
if the queue has reached its maximum capacity,
False
otherwise.
size() -> int
: return the current number of elements in the queue.
In the examples, operations lists the calls in order, arguments holds the arguments of each call, and the output holds each call's return value, with null for the constructor.
Create a queue with capacity 3. Enqueue 10, 20 and 30 (all return true). Peek returns 10. Dequeue returns 10. Enqueue 40 succeeds by wrapping around and returns true. Peek returns 20. isFull returns true and size returns 3. The next three dequeues return 20, 30 and 40 in order, and isEmpty returns true.
Create a queue with capacity 2. isEmpty returns true on the empty queue, and dequeue and peek both return -1. Enqueue 5 and 10 both succeed, and isFull returns true. Enqueue 15 fails and returns false because the queue is full. isEmpty returns false and size returns 2.
Constraints & Assumptions
1 <= capacity <= 10⁴
-10⁹ <= value <= 10⁹
At most
10⁵
total calls will be made across all methods.
dequeue
and
peek
return
-1
when the queue is empty.
enqueue
returns
False
when the queue is full.
Clarifying Questions to Ask Guidance
Values can be
-1
, which is also the empty signal from
dequeue
and
peek
. Is that ambiguity acceptable, or should the API change?
Will more than one thread call the queue at the same time?
Does a dequeued slot need to be cleared, for example to release object references, or can the old value stay until it is overwritten?
What a Strong Answer Covers Guidance
How the front and rear positions advance and wrap at the end of the array.
How the implementation tells a full queue from an empty one.
The boundary calls:
dequeue
and
peek
on an empty queue,
enqueue
on a full queue, and a capacity of 1.
The time cost of each operation and the total memory used.
A trace of an example through the wrap-around that confirms the indices.
Follow-up Questions Guidance
How would you change the API so a caller can tell an empty queue apart from a stored
-1
?
If the capacity is always a power of two, how can you avoid the modulo operation?
How would you make the queue safe for one producer thread and one consumer thread, and what changes with several producers?
How would you let the queue grow when it is full while keeping FIFO order?