oneby7

Stacks and Queues Building stacks and queues

191. Implement Queue using Stacks

Easy

Build a queue of integers, first in, first out, using only stacks. Write a class MyQueue with four methods:

  • push(x) adds x at the back of the queue.
  • pop() removes the value at the front and returns it.
  • peek() returns the value at the front without removing it.
  • empty() returns true if the queue holds no values, and false otherwise.

The values must be kept in one or more stacks, and each stack may only be used as a stack: push a value onto the top, look at or remove the top value, and ask for its size or whether it is empty. Anything that reaches below the top, such as reading [0], shift, popleft or pop_front, is off limits. The judge cannot check this rule, so keeping it is up to you.

Use stack in C++, an ArrayDeque in Java with only push, pop and peek, and Stack in C#. In Python, use a list with only append, pop() and [-1], and in Rust a Vec with only push, pop and last. In JavaScript, use an array with only push, pop and [length - 1]. In Go, use a slice with only append, [len(s)-1] and [:len(s)-1].

Your class is tested by replaying a list of calls on a new, empty MyQueue. ops[i] names the i-th call: "push", "pop", "peek" 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, peek and empty return, in the order they are called.

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

Example 1

Input: ops = ["push", "push", "push", "peek", "pop", "pop", "empty", "peek"] values = [5, 1, 7]

Output: [5,5,1,false,7]

Explanation: The queue holds 5, 1 and 7 from front to back. peek and the first pop both give 5, the value pushed first, and the second pop gives 1. Only 7 is left, so empty is false and peek is 7.

Example 2

Input: ops = ["empty", "push", "push", "pop", "empty", "pop", "empty", "push", "peek"] values = [0, -4, -9]

Output: [true,0,false,-4,true,-9]

Explanation: A new queue is empty. The pops give 0 and then -4, and 0 counts as a value like any other. After them the queue is empty again, and -9 becomes the only value.

Example 3

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

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

Explanation: 7 joins the queue behind -1000000000, the one value left after the first pop. So 7 comes out last.

Check your understanding

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

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

Constraints

  • 1 ≤ ops.length ≤ 105
  • Each ops[i] is "push", "pop", "peek" or "empty".
  • values.length equals the number of "push" calls in ops.
  • -109 ≤ values[i] ≤ 109
  • pop and peek are only called when the queue 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.