Implement Queue using array | Practice | GeeksforGeeks

Implement a Queue using an Array. Queries in the Queue are of the following type:
(i) 1 x   (a query of this type means  pushing 'x' into the queue)
(ii) 2     (a query of this type means to pop element from queue and print the poped element)
Example 1:
Input:
Q = 5
Queries = 1 2 1 3 2 1 4 2
Output: 2 3
Explanation:
In the first test case for query 
1 2 the queue will be {2}
1 3 the queue will be {2 3}
2   poped element will be 2 the 
    queue will be {3}
1 4 the queue will be {3 4}
2   poped element will be 3 

Example 2:
Input:
Q = 4
Queries = 1 3 2 2 1 4   
Output: 3 -1
Explanation:
In the second testcase for query 
1 3 the queue will be {3}
2   poped element will be 3 the
    queue will be empty
2   there is no element in the
    queue and hence -1
1 4 the queue will be {4}. 
Your Task :
You are required to complete the two methods push() which take one argument an integer 'x' to be pushed into the queue and pop() which returns a integer poped out from othe queue. If the queue is empty, it should return -1 on a pop operation. 
Expected Time Complexity: O(1) for both push() and pop().
Expected Auxiliary Space: O(1) for both push() and pop().
Constraints:
1 ≤ Q ≤ 105
1 ≤ x ≤ 105
0
amanahirwar1515 days ago
C++ EASY SOLUTION
//Function to push an element x in a queue.void MyQueue :: push(int x){   arr[rear++]=x;   }
//Function to pop an element from queue and return that element.int MyQueue :: pop(){   if(rear==front){       return -1;   }         int d = arr[front];   front++;   return d;}
+2
harshscode2 weeks ago
       arr[rear]=x;       rear=rear+1;}
//Function to pop an element from queue and return that element.int MyQueue :: pop(){    if(rear==front)    return -1;       int x=arr[front];       front=front+1;       return x;
+1
vbn20012 weeks ago
Java Solution: 

class MyQueue {

    int front, rear;
	int arr[] = new int[100005];

    MyQueue()
	{
		front=0;
		rear=0;
	}
	
	//Function to push an element x in a queue.
	void push(int x)
	{
	   arr[rear] = x;
	   rear++;
	} 

    //Function to pop an element from queue and return that element.
	int pop()
	{
		if(rear==front) return -1;
		
		int temp = arr[front];
		front++;
		return temp;
	} 
}

0
zerefkhan2 weeks ago
C++ solution
Total Time Taken : 0.79/2.36
 
//Function to push an element x in a queue.
void MyQueue :: push(int x)
{
    arr[rear++] = x;
}

//Function to pop an element from queue and return that element.
int MyQueue :: pop()
{
    if(front == rear) return -1;
    return arr[front++];
}
-1
atif836141 month ago
java solution:-
void push(){ 
arr[rear]=x;
rear++;
}
 
int  pop(){
if(rear ==front){
return -1;
}
int temp=arr[front];
front++;
return arr[temp];
}
0
swastikp17111 month ago
Simple Java Code
class MyQueue {

    int front, rear;
	int arr[] = new int[100005];

    MyQueue()
	{
		front=0;
		rear=-1;
	}
	
	//Function to push an element x in a queue.
	void push(int x)
	{
	    arr[++rear]=x;      
	} 

    //Function to pop an element from queue and return that element.
	int pop()
	{   // when Queue is Empty
		if(front>rear) return -1;
		
		// When Queue is not Empty
		int ans=arr[front++];
		return ans;
	} 
}
0
hasnainraza1998hr1 month ago
C++, 0.8
//Function to push an element x in a queue.void MyQueue :: push(int x){       arr[rear++] = x;}
//Function to pop an element from queue and return that element.int MyQueue :: pop(){       if(front==rear)           return -1;       int temp;       if(rear==1){           temp = arr[front];           front = rear = 0;       }       else{           temp = arr[front++];           if(front>rear)               front = rear = 0;       }       return temp;}
0
harshitgoel0031 month ago
class MyQueue {
   int front, rear;   int arr[] = new int[100005];
   MyQueue(){ front = -1; rear = -1;}//Function to push an element x in a queue.void push(int x){    // Your code here    if((front == -1) && (rear == -1)) {        arr[(front + 1)] = x;        front += 1;        rear += 1;    }    else {        arr[(rear + 1)] = x;        rear += 1;    }}
   //Function to pop an element from queue and return that element.int pop(){ // Your code here if((front == -1) && (rear == -1))     return -1;      int result = arr[front];     front += 1;    if(front > rear) {        front = -1;     rear = -1;    }
       return result; } }
 
 
0
detroix072 months ago
void MyQueue :: push(int x){  if(rear==100005)  {    cout << "queue is full" << endl;  }   else   {    arr[rear]=x;    rear++;  }}
//Function to pop an element from queue and return that element.int MyQueue :: pop(){   if(rear==front)     {     return -1;   }   else    {     int Element = arr[front];     front++;     if(rear==front)     {       front=0;       rear=0;     }     return Element;   }} 
0
akshitbansal7192 months ago
Javascript solution:-
 
class MyQueue {
    
    constructor(){
		this.front = 0;
		this.rear = 0;
		this.arr = new Array(100005);
	}

	push(x) {
	    // Your code here
	    this.arr[this.rear++] = x
	} 

	pop() {
		// Your code here
		if (this.front >= this.rear) return -1
		return this.arr[this.front++]
	} 
}

We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.