FastPrepImplement Queue Using Stacks

Implement Queue Using Stacks

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Implement a first-in-first-out queue using only two stacks. The queue supports push, pop, peek, and empty.

The function receives parallel arrays operations and arguments. Process them in order. MyQueue creates the queue, push uses the first integer in the corresponding argument array, and the remaining operations take no arguments.

Return one string for every operation: null for MyQueue and push, the returned integer for pop and peek, and true or false for empty.

Use only standard stack operations: push to the top, read or remove the top, size, and emptiness checks.

Function

MyQueue(operations: String[], arguments: int[][]) → String[]

Examples

Example 1

operations = ["MyQueue","push","push","peek","pop","empty"]arguments = [[],[1],[2],[],[],[]]return = ["null","null","null","1","1","false"]

Create the queue, push 1 and 2, then peek and pop the front value 1. One value remains, so empty returns false.

Constraints

  • 1 <= x <= 9
  • At most 100 calls will be made to push, pop, peek, and empty.
  • All the calls to pop and peek are valid.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] MyQueue(String[] operations, int[][] arguments) {
  // write your code here
}
operations["MyQueue","push","push","peek","pop","empty"]
arguments[[],[1],[2],[],[],[]]
expected["null", "null", "null", "1", "1", "false"]
Checking account…