FastPrepImplement Stack Using Two Queues

Implement Stack Using Two Queues

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Implement a last-in-first-out stack using exactly two FIFO queues.

Process commands:

  • push x: push x and append "null".
  • pop: remove and append the top integer.
  • top: append the top integer without removing it.
  • empty: append "true" or "false".

Design push and top to run in O(1).

Function

processStackUsingQueues(operations: String[]) → String[]

Examples

Example 1

operations = ["push 1","push 2","top","pop","empty"]return = ["null","null","2","2","false"]

Two is the most recently pushed value.

Constraints

  • 1 <= operations.length <= 10^4.
  • Pop and top are called only on a nonempty stack.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] processStackUsingQueues(String[] operations) {
  // Write your code here.
}
operations["push 1","push 2","top","pop","empty"]
expected["null", "null", "2", "2", "false"]
Checking account…