A Queue Built From Two Stacks
Updated
Problem statement
A queue gives values back in the order they came in: first in, first out. Run a list of operations on a queue that starts empty. The operations come in order as the strings operations:
push xadds the valuexat the back of the queue. It outputs nothing.popremoves the value at the front and outputs it.peekoutputs the value at the front and leaves it in the queue.emptyoutputs1if the queue holds no value, or0if it holds at least one.
Return an array with the outputs of all pop, peek and empty operations, in the order they run. If none of them occurs, return an empty array.
To practise, build the queue from two stacks (a stack gives back its newest value first) instead of using your language's built-in queue. The judge only compares the answers, so it cannot check this.
Function
queueOperations(operations: String[]) → int[]Examples
Example 1
operations = ["push 5","push 8","peek","pop","empty","pop","empty"]return = [5,5,0,8,1]The queue gives back the oldest value first. After push 5 and push 8, peek outputs 5 without removing it, and pop outputs 5 and removes it. empty outputs 0 because 8 is still inside. The next pop outputs 8, and the last empty outputs 1.
Example 2
operations = ["push 1","push 2","pop","push 3","push 4","pop","pop","peek","pop","empty"]return = [1,2,3,4,4,1]Values leave in the order they arrived. pop outputs 1. Then 3 and 4 arrive while 2 is still waiting, but 2 is older, so the next pop outputs 2, and the one after that outputs 3. peek outputs 4 and the following pop outputs 4, so the queue is empty and empty outputs 1.
Example 3
operations = ["empty","push 0","empty","peek","push 1000000000","pop","pop","empty"]return = [1,0,0,0,1000000000,1]A new queue is empty, so the first empty outputs 1. The value 0 is a real value: after push 0, empty outputs 0 and peek outputs 0. Then 1000000000 is added behind it. The first pop outputs 0, the second outputs 1000000000, and the last empty outputs 1.
Constraints
1 <= operations.length <= 2000- Each operation is
push x,pop,peekorempty, with a single space betweenpushandx. xis a whole number with0 <= x <= 109, written without leading zeros.popandpeekare only called when the queue holds at least one value.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Implement a Queue Using Two StacksAmazon · ONSITE INTERVIEW
- Implement Queue Using StacksMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Command Undo Data StructureNetflix · ONSITE INTERVIEWNetflix interview questions
- Stack from Queues and Queue from StacksBank of America · ONSITE INTERVIEWBank of America interview questions
- Stack Built on a Linked ListCapital One · ONSITE INTERVIEWCapital One interview questions
- Implement Queue Using StacksBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Implement a Basic StackAMD · ONSITE INTERVIEWAMD interview questions
- KV Store with Nested TransactionsApplied Intuition · PHONE SCREEN · ONSITE INTERVIEWApplied Intuition interview questions
- Min StackDP World · ONSITE INTERVIEW
- Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)