FastPrepProducer-Consumer Queue
Problems/Producer-Consumer Queue

Producer-Consumer Queue

Apple logoApple● MediumFULLTIMENEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple phone screen for full-time and new grad roles. The report asks for a producer-consumer queue that works with many threads. Our version uses an ordered, finite trace of atomic calls. It keeps the capacity, first-in-first-out storage, and blocking and resuming. We chose the capacity, who goes first among waiting calls, how the final answer is written, and the input limits, because the original interface and scheduling rules were not kept. We do not claim any real thread schedule.

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 PUT call at index i adds values[i]. It waits while the queue is full.
  • TAKE removes 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 PUT or TAKE.
  • 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

Interview reports that mention this question

  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE Interviews: 37 Experiences (Part 1 of 3)

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public String[] runProducerConsumerQueue(int capacity, String[] operations, int[] values) {
        // Write your code here.
    }

}
Checking account…