Problemshared array dual queue allocator

Session: Sign in to solve

Solution.txt

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, enqueue returns false without changing stored values, queue order, endpoints, or slot ownership.
  • Removal and reuse. dequeue returns and removes the selected non-empty queue's front value, then makes that capacity available to either queue.

API

SignatureReturnsBehavior
PooledCapacityDualQueue(capacity: integer)Not applicableCreates two empty queues sharing total capacity capacity.
enqueue(queueName: string, value: integer)booleanAttempts to append value to the selected queue and reports whether it was accepted.
dequeue(queueName: string)integerRemoves and returns the selected queue's front value.

Examples

For PooledCapacityDualQueue(3):

StepOperationResult
1enqueue("FIRST", 10)true
2enqueue("SECOND", 20)true
3enqueue("FIRST", 30)true
4enqueue("SECOND", 40)false
5dequeue("FIRST")10
6enqueue("SECOND", 40)true
7dequeue("SECOND")20

Constraints

  • 1 <= capacity <= 100000.
  • Values are signed 32-bit integers.
  • At most 50000 method calls occur in one testcase.
  • queueName is exactly "FIRST" or "SECOND".
  • Every dequeue call 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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.