FastPrepA Queue Built From Two Stacks
Problems/A Queue Built From Two Stacks

A Queue Built From Two Stacks

Expedia logoExpedia● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: One post, published in September 2018 about a campus visit for interns, ends with a list of extra questions that the writer or friends were asked. This task is on it as a link, and the post does not say who got it or in which round.

The link's address is all the post gives. We did not read the page behind it for this problem, and no operations, input form, examples or limits are stated. Here, you get a list of push, pop, peek and empty operations on whole-number values from 0 to 10^9, up to 2000 of them. You return what each pop, peek and empty gives, with empty written as 1 or 0. A pop or peek never comes while the queue is empty. The tests see only the answers, so they cannot check that two stacks were used.

Our version is about 70% the same as the reported question.

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 x adds the value x at the back of the queue. It outputs nothing.
  • pop removes the value at the front and outputs it.
  • peek outputs the value at the front and leaves it in the queue.
  • empty outputs 1 if the queue holds no value, or 0 if 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, peek or empty, with a single space between push and x.
  • x is a whole number with 0 <= x <= 109, written without leading zeros.
  • pop and peek are 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)
All Expedia interview questionsSee Expedia hiring insights
Saved
class Solution {
    public int[] queueOperations(String[] operations) {
        // Write your code here.
    }
}
Checking account…