Pooled-Capacity Dual Queue
Problem
Design PooledCapacityDualQueue to maintain two independent FIFO queues, FIRST and SECOND, that share one fixed total capacity.
Requirements
- Independent FIFO order. Each queue returns its successfully enqueued values in enqueue order, and operations on the other queue do not change that order.
- Pooled capacity. An enqueue to either queue succeeds whenever the shared pool has free capacity, including capacity released by the other queue.
- Atomic full failure. When all slots are occupied,
enqueuereturnsfalsewithout changing stored values, queue order, endpoints, or slot ownership. - Removal and reuse.
dequeuereturns and removes the selected non-empty queue's front value, then makes that capacity available to either queue.
API
| Signature | Returns | Behavior |
|---|---|---|
PooledCapacityDualQueue(capacity: integer) | Not applicable | Creates two empty queues sharing total capacity capacity. |
enqueue(queueName: string, value: integer) | boolean | Attempts to append value to the selected queue and reports whether it was accepted. |
dequeue(queueName: string) | integer | Removes and returns the selected queue's front value. |
Examples
For PooledCapacityDualQueue(3):
| Step | Operation | Result |
|---|---|---|
| 1 | enqueue("FIRST", 10) | true |
| 2 | enqueue("SECOND", 20) | true |
| 3 | enqueue("FIRST", 30) | true |
| 4 | enqueue("SECOND", 40) | false |
| 5 | dequeue("FIRST") | 10 |
| 6 | enqueue("SECOND", 40) | true |
| 7 | dequeue("SECOND") | 20 |
Constraints
1 <= capacity <= 100000.- Values are signed 32-bit integers.
- At most
50000method calls occur in one testcase. queueNameis exactly"FIRST"or"SECOND".- Every
dequeuecall targets a non-empty queue. - The total pooled capacity never changes after construction.
Notes
Calls are sequential. Invalid queue names, empty-queue removal, resizing, and concurrent access are outside this contract.