FastPrepImplement a Queue Using Two Stacks
Problems/Implement a Queue Using Two Stacks

Implement a Queue Using Two Stacks

Amazon logoAmazon● EasyNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamples

Problem statement

Source note: Reported in an Amazon new grad onsite interview. The report describes a question about a queue built from two stacks. Our function signature, examples, and limits are the same as in an equivalent version of this problem that we already reviewed. Our version is about 95% the same as the reported question.

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:

  • push adds values[i] to the back of the queue and produces no output.
  • pop removes the front value and appends its decimal string to the result.
  • peek reads the front value without removing it and appends its decimal string to the result.
  • empty appends true or false to 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 Amazon Onsite Interview questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Implement a Queue Using Two StacksOracle · PHONE SCREENOracle interview questions
  • Recent Hit CounterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • Implement Queue Using StacksMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • 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
  • Durable Work Queue OperationsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions
  • Five-Minute Hit CounterGoogle · ONSITE INTERVIEWGoogle interview questions
  • Weighted Round-Robin Seller Task SchedulerByteDance · ONSITE INTERVIEWByteDance interview questions

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public String[] processQueueOperations(String[] operations, int[] values) {
        // write your code here
    }
}
Checking account…