Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Priority Queue Implementation

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

Your question is Priority Queue Implementation. 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 priority queue in a coding environment.

Implement a min-priority queue using a binary heap. Each operation is represented as a list: ['push', priority, value], ['peek'], or ['pop']. Lower priorities are removed first, and values with equal priorities must be returned in insertion order. Return the values produced by peek and pop; return None when either operation is performed on an empty queue.

Input: a list of valid operations. Output: a list containing the results of all peek and pop operations.

Example: [['push', 2, 'a'], ['push', 1, 'b'], ['pop'], ['peek']] returns ['b', 'a'].

Constraints

  • 1 <= operations.length <= 5000
  • Each operation is valid and is either ['push', priority, value], ['peek'], or ['pop']
  • -10^9 <= priority <= 10^9
  • Values are strings
  • Values do not need to be unique

Function Signature

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