Priority Queue Class
Updated
Problem statement
Implement a class PriorityQueue in which every value waits with a priority:
void enqueue(int value, int priority)addsvaluewith the givenpriority.int dequeue()removes and returns the waiting value with the highest priority. If several waiting values share that priority, the one that was added earliest leaves first. If the queue is empty, return-1.
To test your class, you get operations, the calls made in order on one new queue, and operands, the arguments of each call. Return one string per call: "null" for enqueue, and the returned number in normal decimal form for dequeue.
Function
runPriorityQueue(operations: String[], operands: int[][]) → String[]Examples
Example 1
operations = ["enqueue","enqueue","enqueue","dequeue","dequeue","dequeue"]operands = [[10,2],[20,7],[30,5],[],[],[]]return = ["null","null","null","20","30","10"]The queue holds 10 (priority 2), 20 (priority 7) and 30 (priority 5). The highest priority is 7, so the first dequeue returns "20". Then 30 (priority 5) leaves, and finally 10 (priority 2).
Example 2
operations = ["enqueue","enqueue","enqueue","dequeue","dequeue","dequeue"]operands = [[4,3],[8,3],[6,9],[],[],[]]return = ["null","null","null","6","4","8"]6 has the highest priority, 9, so it leaves first. 4 and 8 both have priority 3; 4 was added earlier, so it leaves before 8.
Example 3
operations = ["dequeue","enqueue","dequeue","dequeue"]operands = [[],[5,0],[],[]]return = ["-1","null","5","-1"]The first dequeue finds the queue empty and returns "-1". After 5 is added, dequeue returns "5", and the last dequeue again finds the queue empty.
Constraints
1 <= operations.length <= 2000andoperands.length == operations.length- Each operation is
"enqueue"or"dequeue". operands[i]is[value, priority]forenqueueand[]fordequeue.0 <= value, priority <= 10^6
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Streaming Median of Trade PricesXantium · PHONE SCREEN
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Rolling Hit CounterDatabricks · PHONE SCREENDatabricks interview questions
- Alert Engine with a Repeat IntervalChronosphere · PHONE SCREEN
- VIP and Regular Service SchedulerIntuit · PHONE SCREENIntuit interview questions