Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Queues

Problem Description

The Queue interface of the Java collections framework provides the functionality of the queue data structure. It extends the Collection interface.

Classes that Implement Queue:

Since the Queue is an interface, we cannot provide the direct implementation of it. In order to use the functionalities of Queue, we need to use classes that implement it:

  • ArrayDeque
  • LinkedList
  • PriorityQueue

In queues, elements are stored and accessed in First In, First Out manner. That is, elements are added from the behind and removed from the front.

In Java, we must import java.util.Queue package in order to use Queue.

Syntax:

 Queue<String> names= new ArrayDeque<>();

The above statement creates a Queue of Strings, we can now push and remove Strings from this queue easily.

Methods of Queue:

  • add() - Inserts the specified element into the queue. If the task is successful, add() returns true, if not it throws an exception.
  • offer() - Inserts the specified element into the queue. If the task is successful, offer() returns true, if not it returns false.
  • element() - Returns the head of the queue. Throws an exception if the queue is empty.
  • peek() - Returns the head of the queue. Returns null if the queue is empty.
  • remove() - Returns and removes the head of the queue. Throws an exception if the queue is empty.
  • poll() - Returns and removes the head of the queue. Returns null if the queue is empty.
  • size() - Return an integer denoting the total number of elements in the queue at present.

Task:

Complete the code given below.

You need to answer Q queries, in each query you are given two integers and y:

  • if x = 1 then push the integer  to the back of the queue.
  • if x = 2 then return the front element of the queue if the queue is not empty else return -1
  • if x = 3  then remove the front element from the queue if the queue is not empty else do nothing. No need to return anything in this query.


Problem Constraints

1 <= Q <= 100

1 <= x <= 3

1 <= y <= 100


Input Format

First line of input contains a single integer Q.

Next Q lines each contain two integers x and y for that query.


Output Format

For each query in which x = 2 print its answer in separate lines.


Example Input

Input 1:

 5
 2 -1
 1 5
 3 -1
 1 5
 2 -1


Example Output

Output 1:

 -1
 5


Example Explanation

Explanation 1:

 Query 1: x = 2 so we need to print the front element of the queue , since the queue is empty we will print -1.
 Query 5: x = 2 Queue contains only element 5 so we will print 5.

 

Start solving Queues on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test