FastPrepImplement a Heap Priority Queue

Implement a Heap Priority Queue

ByteDance logoByteDance● MediumNEW GRADPHONE SCREEN
Learn

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:

  • PUSH inserts values[i].
  • PEEK returns the current minimum without removing it.
  • POP removes and returns the current minimum.

Collect and return every value produced by PEEK or POP, in operation order. Every observation occurs while the queue is nonempty. Equal values are allowed; values[i] is ignored for observation operations.

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]

The minimum after both insertions is 2. POP returns and removes 2, leaving 5.

Example 2

operations = ["PUSH","PUSH","PUSH","POP","POP","POP"]values = [3,3,1,0,0,0]return = [1,3,3]

Repeated values remain separate queue entries and are removed in nondecreasing priority order.

Example 3

operations = ["PUSH","PEEK","PUSH","PEEK"]values = [-4,0,-7,0]return = [-4,-7]

The second insertion becomes the new minimum.

Constraints

  • 1 <= operations.length == values.length <= 200000.
  • Each operation is PUSH, PEEK, or POP.
  • Every PEEK and POP occurs when the queue is nonempty.
  • Each inserted value is a signed 32-bit integer.

More ByteDance problems

See ByteDance hiring insights
public int[] runPriorityQueue(String[] operations, int[] values) {
  // write your code here
}
operations["PUSH","PUSH","PEEK","POP","PEEK"]
values[5,2,0,0,0]
expected[2,2,5]
Checking account…