Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Thread-Safe Bounded Priority Queue

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

Your question is Thread-Safe Bounded Priority 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

Implement a thread-safe bounded priority queue for tasks. The queue stores tasks as (priority, task_id, payload) and supports concurrent producers and consumers. Write a class with methods push(priority, task_id, payload), pop(), and size(). If the queue is full, push must block until space is available; if it is empty, pop must block until an item is available. Higher priority values must be returned first, and tasks with the same priority must be returned in FIFO insertion order.

Constraints

  • 1 <= capacity <= 10^5
  • At most 10^5 total push and pop operations
  • -10^9 <= priority <= 10^9
  • task_id values are unique per successful push
  • push blocks when the queue is full and pop blocks when the queue is empty

Function Signature

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