FastPrepBuild a Queue From Two Stacks
Problems/Build a Queue From Two Stacks

Build a Queue From Two Stacks

Adobe logoAdobe● EasyFULLTIMEINTERNPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem simulates a first-in, first-out queue made only of stacks, driven by a list of push, pop, peek and empty calls. An Adobe Computer Scientist 1 report names it, by link, as the second of two first-round questions; per the report, the interviewer pushed for a better-optimized version after the author's first correct answer. It gives no limits or examples.

Our choices: the calls come as two lists and the answer lists each pop, peek and empty result (empty as 1 or 0); pop and peek never run on an empty queue; at most 2000 calls with values up to 10^9; new examples. The judge checks outputs only, so using stacks is on trust.

The judged task matches the report at about 85%.

Build a first-in, first-out queue using only stacks: the only actions allowed on each stack are push on top, pop from the top, look at the top, and check its size. The queue supports:

  • push: add values[i] at the back of the queue.
  • pop: remove the value at the front and output it.
  • peek: output the value at the front without removing it.
  • empty: output 1 if the queue holds no values, otherwise 0.

Run operations in order, starting from an empty queue, and return the outputs in order (push outputs nothing). Try to make every operation take O(1) time on average. The judge only checks the outputs.

Function

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

Examples

Example 1

operations = ["push","push","pop","push","peek","pop","pop","empty"]values = [8,3,0,5,0,0,0,0]return = [8,3,3,5,1]

8 and 3 join; pop removes 8, the oldest. 5 joins behind 3. peek sees 3, then two pops remove 3 and 5. The queue is now empty, so empty gives 1. Outputs: 8, 3, 3, 5, 1.

Example 2

operations = ["empty","push","empty","peek","peek"]values = [0,42,0,0,0]return = [1,0,42,42]

The queue starts empty (1). After 42 joins it is not empty (0). peek does not remove anything, so both peeks see 42.

Example 3

operations = ["push","push","push","pop","push","push","pop","pop","pop","pop"]values = [1,2,3,0,4,5,0,0,0,0]return = [1,2,3,4,5]

Values leave in the order they joined, even when new values join between removals: 1, then 2, 3, 4, 5.

Constraints

  • 1 <= operations.length <= 2000 and values.length == operations.length
  • each operations[i] is one of push, pop, peek, empty
  • 1 <= values[i] <= 10^9 when operations[i] is push, and values[i] == 0 otherwise
  • pop and peek are only called when the queue is not empty

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int[] runQueue(String[] operations, int[] values) {
    // Write your code here.
}
Checking account…