oneby7

Stacks and Queues Building stacks and queues

190. Implement Stack using Queues

Easy

Build a stack of integers, last in, first out, using only queues. Write a class MyStack with four methods:

  • push(x) puts x on top of the stack.
  • pop() removes the value on top and returns it.
  • top() returns the value on top without removing it.
  • empty() returns true if the stack holds no values, and false otherwise.

The values must be kept in one or more queues, and each queue may only be used as a queue: add a value at the back, look at or remove the value at the front, and ask for its size or whether it is empty. Anything that touches the back of a queue, such as deque.pop() or rotate, is off limits. The judge cannot check this rule, so keeping it is up to you.

Use queue in C++, a Queue such as ArrayDeque in Java (offer, peek, poll), Queue in C#, a deque in Python with only append, popleft and [0], and a VecDeque in Rust with only push_back, pop_front and front. JavaScript has no queue type, so use an array with only push, shift and [0]. In Go, use a slice with only append, [0] and [1:].

Your class is tested by replaying a list of calls on a new, empty MyStack. ops[i] names the i-th call: "push", "pop", "top" or "empty". The pushes take their values from values in order, so the first push pushes values[0], the second pushes values[1], and so on. The answer is the list of every value that pop, top and empty return, in the order they are called.

In C# and Go the methods are Push, Pop, Top and Empty. Go creates the stack with Constructor(), and Rust with MyStack::new(). The names in ops are the same in every language.

Example 1

Input: ops = ["push", "push", "push", "top", "pop", "pop", "empty", "top"] values = [6, 3, 8]

Output: [8,8,3,false,6]

Explanation: After three pushes the stack holds 6, 3 and 8 from the bottom up. top and the first pop both give 8, the value pushed last, and the second pop gives 3. Only 6 is left, so empty is false and top is 6.

Example 2

Input: ops = ["empty", "push", "empty", "pop", "empty", "push", "top"] values = [0, -5]

Output: [true,false,0,true,-5]

Explanation: A new stack is empty. Pushing 0 makes it non-empty, since 0 is a value like any other. Popping 0 empties the stack again, and -5 then becomes the only value.

Example 3

Input: ops = ["push", "push", "pop", "push", "top", "pop", "pop", "empty"] values = [1000000000, -1000000000, 7]

Output: [-1000000000,7,7,1000000000,true]

Explanation: After the first pop, 7 goes on top of 1000000000, the one value left. So 7 comes out before it.

Check your understanding

Work out the answer for this input before you write any code.

ops = ["push", "push", "push", "pop", "top"] values = [1, 2, 3]

Constraints

  • 1 ≤ ops.length ≤ 5000
  • Each ops[i] is "push", "pop", "top" or "empty".
  • values.length equals the number of "push" calls in ops.
  • -109 ≤ values[i] ≤ 109
  • pop and top are only called when the stack is not empty.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.