FastPrepImplement a FIFO Queue

Implement a FIFO Queue

ByteDance logoByteDance● MediumINTERNPHONE SCREEN
Learn

Problem statement

Implement the behavior of a first-in, first-out queue without using a built-in queue or deque type.

Process the paired arrays operations and values from left to right:

  • push: append values[i] to the back of the queue.
  • pop: remove the value at the front and append that value to the result.
  • peek: append the value at the front to the result without removing it.

Return all values produced by pop and peek, in operation order. A push operation produces no result.

Function

queueOperations(operations: String[], values: int[]) → int[]

Examples

Example 1

operations = ["push","push","peek","pop","peek"]values = [4,7,0,0,0]return = [4,4,7]

The first peek reads 4. The following pop removes and returns 4, so the final peek reads 7.

Example 2

operations = ["push","push","pop","push","peek","pop","pop"]values = [10,20,0,30,0,0,0]return = [10,20,20,30]

After removing 10, pushing 30 leaves the queue as [20,30]. The remaining operations read and remove those values in FIFO order.

Example 3

operations = ["push","push","peek","pop","peek","pop"]values = [-2,-2,0,0,0,0]return = [-2,-2,-2,-2]

Equal and negative values retain their insertion order. A peek does not remove the front value.

Constraints

  • 1 <= operations.length == values.length <= 100000.
  • Each operation is push, pop, or peek.
  • For push, -10^9 <= values[i] <= 10^9; for other operations, values[i] is ignored.
  • Every pop or peek occurs while the queue is nonempty.
  • Do not use a built-in queue or deque type.

More ByteDance problems

See ByteDance hiring insights
public int[] queueOperations(String[] operations, int[] values) {
    // Write your code here.
}
operations["push","push","peek","pop","peek"]
values[4,7,0,0,0]
expected[4,4,7]
Checking account…