Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5725 |

@blackshadows | 5685 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4908 |

@saiujwal13083 | 4156 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3511 |

@sushant_a | 3453 |

@KshamaGupta | 3318 |

@the_coder95 | 3302 |

Complete Leaderboard | |

Handle | Score |

@shashi1iitk | 398 |

@Suniti Jain | 114 |

@ankish2000nayak | 104 |

@gopalsethlnct | 94 |

@senthil1185 | 90 |

@esmadhan | 90 |

@gyanadipta07 | 89 |

@satwikdash2000 | 85 |

@varunmishra3040 | 83 |

@gamec1634 | 82 |

@pj_1721 | 80 |

Complete Leaderboard |

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 ≤ 10^{5}

1 ≤ x** **≤ 10^{5}

Login to report an issue on this page.

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

Yes
Implement Queue using array

...

Please choose 'ReadOnlyMode' if you needn't to 'Edit' the problem e.g. viewing OJ's solution, TestCase Files (TCFs), TimeLimit etc.