Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Thread-Safe Producer-Consumer Queue

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Thread-Safe Producer-Consumer Queue. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

KPMG AI solutions may process work items through concurrent producer and consumer threads. Implement a bounded, thread-safe FIFO queue without using Python's queue.Queue.

Your implementation must expose put(item, block=True, timeout=None) and get(block=True, timeout=None) operations. put waits while the queue is full, and get waits while it is empty. Both operations must wake efficiently when their required condition changes, preserve FIFO order, and prevent lost updates or corrupted state.

For automated evaluation, implement process_queue(operations, capacity). Each operation is either ["put", value] or ["get"]. The harness executes operations sequentially with non-blocking behavior: a successful put returns true, a put attempted on a full queue returns false, a successful get returns its value, and a get attempted on an empty queue returns null. The queue class itself must support blocking calls for concurrent use.

Constraints

  • 1 <= capacity <= 10^4
  • 1 <= len(operations) <= 10^5
  • Each value is an integer
  • Each operation is either ["put", value] or ["get"]
  • Do not use queue.Queue

Function Signature

def process_queue(operations, capacity):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output