Producer-Consumer Queue
Updated
Problem statement
Given capacity, operations and values, simulate a producer-consumer queue and return the final result of each call. The queue holds at most capacity values and removes them in the order they were added.
A PUT call is a producer and a TAKE call is a consumer. The queue starts empty, and the calls are submitted one at a time in order. A call that cannot finish yet waits, and later calls are still submitted.
- The
PUTcall at indexiaddsvalues[i]. It waits while the queue is full. TAKEremoves the oldest value in the queue. It waits while the queue is empty. Its input value is zero and is ignored.
Waiting producers finish in the order they were submitted, and so do waiting consumers. After each submission, keep finishing waiting calls until none of them can run.
After the last call, return one result per call: PUT for a finished producer, TAKE x for a consumer that finished with integer x, or WAIT for a call that is still waiting. A call that waited at first can still finish later. The queue has no close, cancellation or timeout operation.
Function
runProducerConsumerQueue(capacity: int, operations: String[], values: int[]) → String[]Examples
Example 1
capacity = 1operations = ["PUT","PUT","TAKE"]values = [7,8,0]return = ["PUT","PUT","TAKE 7"]The queue holds one value, so the second producer waits. The consumer removes 7, which frees the slot, and the second producer finishes.
Example 2
capacity = 2operations = ["TAKE","TAKE","PUT"]values = [0,0,5]return = ["TAKE 5","WAIT","PUT"]Both consumers wait on the empty queue. The producer's value goes only to the older waiting consumer, so the second consumer still waits.
Example 3
capacity = 1operations = ["PUT","PUT"]values = [1,2]return = ["PUT","WAIT"]The first producer fills the queue. No consumer ever frees the slot, so the second producer still waits.
Constraints
1 <= capacity <= 1000.0 <= operations.length = values.length <= 2000.- Each operation is
PUTorTAKE. - Producer values are integers in
[-10^9,10^9]; each consumer input value is zero.
More Apple Phone Screen questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Rate Limiter with a Sliding Window per ClientStackAdapt · PHONE SCREENStackAdapt interview questions
- Rolling Hit CounterDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- VIP and Regular Service SchedulerIntuit · PHONE SCREENIntuit interview questions
- Per-Client Sliding-Window Rate LimiterCVS Health · PHONE SCREEN
- Rolling CSAT TrackerDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Sliding-Window Rate LimiterUber · PHONE SCREENUber interview questions