Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Thread-Safe Producer-Consumer Queue
00:00
5 left

Thread-Safe Producer-Consumer Queue

HardPython

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):
Interviewer

Your question is Thread-Safe Producer-Consumer Queue. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.