FastPrepImplement Queue Using Stacks
Problems/Implement Queue Using Stacks

Implement Queue Using Stacks

Microsoft logoMicrosoft● EasyNEW GRADINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Two Microsoft reports name this classic task. In a 2025 Microsoft AI intern first round it came with a puzzle question, and the candidate handled this part well but missed the puzzle. An older Microsoft new grad report also lists it as a round 2 question, and we have no other details from that one.

The 2025 report gives no input format or sizes. Here the calls arrive as one list of text operations and the results come back as text. There are up to 10^4 operations, values are 32-bit integers, pop and peek only happen when the queue has a value, and at least one operation gives an output. These are our choices.

Our version is about 90% the same as the reported questions.

Build a first-in, first-out queue using only two stacks. The queue starts empty.

You get a list of operations and run them in order:

  • push x adds the integer x to the back of the queue. It produces no output.
  • pop removes the value at the front of the queue and outputs it.
  • peek outputs the value at the front of the queue without removing it.
  • empty outputs "true" if the queue has no values, and "false" otherwise.

Return the outputs of pop, peek, and empty as strings, in the order they happen. Write numbers in normal decimal form, such as "-7".

Inside your queue, use only normal stack actions: push on top, look at or remove the top, check the size, and check if it is empty. Try to make every operation take constant time on average.

Function

processQueue(operations: String[]) → String[]

Examples

Example 1

operations = ["push 4","push 9","peek","pop","push 2","pop","pop","empty"]return = ["4","4","9","2","true"]

The queue holds 4 then 9, so peek shows 4 and pop removes 4. After 2 is pushed the queue is 9, 2. The next two pops give 9 and then 2, and the queue is now empty, so empty gives "true".

Example 2

operations = ["push -7","empty","push 0","push -7","pop","peek","push 3","pop","pop","peek"]return = ["false","-7","0","0","-7","3"]

The queue is not empty, so empty gives "false". After two more pushes the queue is -7, 0, -7. pop removes the front value, the first -7, and peek shows 0. After 3 is pushed the queue is 0, -7, 3, so the next two pops give 0 and -7, and peek shows 3. A plain stack would have popped 3 instead of 0, because 3 was pushed last.

Constraints

  • 1 <= operations.length <= 10^4
  • Each operation is push x, pop, peek, or empty.
  • -2^31 <= x <= 2^31 - 1
  • pop and peek are only called when the queue has at least one value.
  • At least one operation is pop, peek, or empty.

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Implement a Queue Using Two StacksAmazon · ONSITE INTERVIEW
  • Stack from Queues and Queue from StacksBank of America · ONSITE INTERVIEWBank of America interview questions
  • Implement Queue Using StacksBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Run a Queue Built From StacksCapital One · ONSITE INTERVIEWCapital One interview questions
  • Implement a Queue Using Two StacksOracle · PHONE SCREENOracle interview questions
  • Build a Queue From Two StacksAdobe · PHONE SCREENAdobe interview questions
  • Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • Command Undo Data StructureNetflix · ONSITE INTERVIEWNetflix interview questions
  • A Queue Built From Two StacksExpedia · ONSITE INTERVIEWExpedia interview questions
  • Implement a Basic StackAMD · ONSITE INTERVIEWAMD interview questions

Interview reports that mention this question

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public String[] processQueue(String[] operations) {
        // Write your code here.
    }
}
Checking account…