Implement Queue Using Stacks
Updated
Problem statement
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 xadds the integerxto the back of the queue. It produces no output.popremoves the value at the front of the queue and outputs it.peekoutputs the value at the front of the queue without removing it.emptyoutputs"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, orempty. -2^31 <= x <= 2^31 - 1popandpeekare only called when the queue has at least one value.- At least one operation is
pop,peek, orempty.
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