Bounded Blocking Queue
Design the state machine behind a bounded blocking queue — a fixed-capacity FIFO where enqueue fails when full and dequeue fails when empty — modelled single-threaded so the logic is testable in isolation.
Requirements
- The queue is FIFO and holds at most
capacityitems. offer(x)enqueues at the tail and returnstrue; if the queue is full it must not enqueue and returnsfalse.pollremoves and returns the head item; if the queue is empty it returns-1(all enqueued values are non-negative, so-1is an unambiguous "empty" sentinel).sizereturns the current item count, always in0 .. capacity.- This is the single-threaded model of a blocking queue: instead of blocking on full/empty we surface it as a
false/-1return, so the ordering and capacity logic can be tested without threads.
API to implement
Implement the class BoundedQueue. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.
| Command | Returns | Behavior |
|---|---|---|
| offer <x> | boolean | Enqueue x at the tail and return true, unless the queue is already at capacity, in which case do nothing and return false. |
| poll | int | Remove and return the item at the head (FIFO), or return -1 if the queue is empty. |
| size | int | Return the number of items currently in the queue. |
Input format
The first line contains Q, the number of operations. The second line contains the constructor argument: capacity — the maximum number of items the queue can hold at once. Each of the next Q lines is one command as listed above. String arguments contain no spaces.
Output format
For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.
This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.
Sample cases
8
2
offer 10
offer 20
offer 30
size
poll
poll
poll
sizetrue
true
false
2
10
20
-1
09
3
offer 5
poll
poll
offer 7
offer 8
size
poll
offer 9
sizetrue
5
-1
true
true
2
7
true
2