Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Implement Queue using Arrays

Basic Acceptance 47.24% Points 10.00

Implement a Queue using an array to handle the following types of queries:

  1. Push operation (1 x): This query adds the element x to the queue.
  2. Pop operation (2): This query removes and returns the front element from the queue. If the queue is empty, return -1.

You need to implement the push and pop functions. The rest of the program will handle the input and output operations.

Examples
Example 1

Example 1

Input: Queries = 2 1 5 2 2

Output: -1 5 -1

Explanation: 2: Pop from an empty queue ?

Output: -1 ? Queue: . 1 5: Push 5 into the queue ? Queue: {5}. 2: Pop and print the front element (5) ? Queue: . 2: Pop from an empty queue ?

Output: -1

Example 2

Example 2

Input: Queries = 1 10 1 20 2 1 30 2 2

Output: 10 20 30

Explanation: 1 10: Push 10 into the queue ? Queue: {10}. 1 20: Push 20 into the queue ? Queue: {10, 20}. 2: Pop and print the front element (10) ? Queue: {20}. 1 30: Push 30 into the queue ? Queue: {20, 30}. 2: Pop and print the front element (20) ? Queue: {30}. 2: Pop and print the front element (30) ? Queue:

Hints
Hint 1
Expected Time Complexity: O(1)
Hint 2
Expected Auxiliary Space: O(1)
Constraints
  • 1 <= number of query<= 10^5
  • 0 <= x <= 10^5
Companies
Amazon Goldman Sachs
Topics
Queue
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions