Implement a Min-Priority Queue
Updated
Problem statement
Implement a min-priority queue of integers without using a built-in priority-queue or heap data structure. Process operations from left to right. For the operation at index i:
PUSHinsertsvalues[i].PEEKreturns the current minimum without removing it.POPremoves and returns the current minimum.
Return every value that PEEK or POP returns, in the order the operations run. Every PEEK and POP happens while the queue is nonempty. Equal values are allowed. For PEEK and POP, values[i] is ignored.
Function
runPriorityQueue(operations: String[], values: int[]) → int[]Examples
Example 1
operations = ["PUSH","PUSH","PEEK","POP","PEEK"]values = [5,2,0,0,0]return = [2,2,5]After both insertions, the minimum is 2, so PEEK returns 2 without removing it. POP then removes and returns that same value. Only 5 is left, so the last PEEK returns it. The answer lists the returned values in that order.
Example 2
operations = ["PUSH","PUSH","PUSH","POP","POP","POP"]values = [3,3,1,0,0,0]return = [1,3,3]The pushes insert the values three, three, and one. The two threes stay separate entries in the queue. The first POP removes one, the minimum. The next two POP operations each remove a three. The answer is one, then three, then three.
Example 3
operations = ["PUSH","PEEK","PUSH","PEEK"]values = [-4,0,-7,0]return = [-4,-7]The first push inserts minus four. It is the only value, so the first PEEK returns minus four. The second push inserts minus seven, which is smaller and becomes the new minimum. The last PEEK returns minus seven. The answer is minus four, then minus seven.
Constraints
1 <= operations.length == values.length <= 200000.- Each operation is
PUSH,PEEK, orPOP. - Every
PEEKandPOPoccurs when the queue is nonempty. - Each inserted value is a signed 32-bit integer.
More Amazon Onsite Interview questions
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Flood FillONSITE INTERVIEW · Seen Nov 2026
- Merge k Sorted ListsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions
- Delayed Job SchedulerWalmart Labs · ONSITE INTERVIEW
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Notification Deduplication WindowGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Helpdesk Ticket ProcessingThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Execute Tasks by PrioritySnowflake · ONSITE INTERVIEWSnowflake interview questions
- Priority Job Scheduler with CooldownsGoogle · ONSITE INTERVIEWGoogle interview questions
- Order Matching EngineJane Street · ONSITE INTERVIEWJane Street interview questions
- Trip Booking with the Earliest Free DriverRippling · ONSITE INTERVIEWRippling interview questions
- Reusable Number AllocatorDropbox · ONSITE INTERVIEWDropbox interview questions