Implement a Queue Using Two Stacks
Updated
Problem statement
Process a sequence of queue operations while using exactly two stacks as the queue's storage. Do not use a queue data structure.
The arrays operations and values have the same length. At position i:
pushaddsvalues[i]to the back of the queue and produces no output.popremoves the front value and appends its decimal string to the result.peekreads the front value without removing it and appends its decimal string to the result.emptyappendstrueorfalseto the result.
Every pop or peek operation occurs while the queue is non-empty. Return the recorded outputs in operation order.
Function
processQueueOperations(operations: String[], values: int[]) → String[]Examples
Example 1
operations = ["push","push","peek","pop","empty"]values = [1,2,0,0,0]return = ["1","1","false"]After pushing 1 and 2, both peek and pop observe the front value 1. Value 2 remains, so the queue is not empty.
Example 2
operations = ["empty","push","empty","peek","pop","empty"]values = [0,7,0,0,0,0]return = ["true","false","7","7","true"]The queue starts empty. After pushing 7, it is non-empty; peek and pop both return 7, after which it is empty again.
Example 3
operations = ["push","push","pop","push","peek","pop","pop","empty"]values = [-1,-2,0,3,0,0,0,0]return = ["-1","-2","-2","3","true"]The first pop removes -1. Pushing 3 does not move it ahead of -2, so the next peek and pop return -2. The final pop returns 3.
More Oracle Phone Screen questions
- Remove Parentheses from a Plus-Minus ExpressionPHONE SCREEN · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · Seen Oct 2026
- Shortest Window by Distinct-Value SumPHONE SCREEN · Seen Oct 2026
- Detect a Cycle in a Directed GraphPHONE SCREEN · Seen Oct 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Remove Adjacent Duplicates in String IIPHONE SCREEN · Seen Oct 2026
- Sum Root-to-Leaf Binary NumbersPHONE SCREEN · Seen Oct 2026
- Longest All-Present Attendance StreakPHONE SCREEN · Seen Oct 2026
- Next PermutationPHONE SCREEN · Seen Sep 2026
- Valid ParenthesesPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Implement a Queue Using Two StacksAmazon · ONSITE INTERVIEW
- Build a Queue From Two StacksAdobe · PHONE SCREENAdobe interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- VIP and Regular Service SchedulerIntuit · PHONE SCREENIntuit interview questions
- Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Multiline Text Editor with Cursor MovementDatologyAI · PHONE SCREEN
- Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions