Stack using Queue
Implement a Stack Data Structure specifically to store integer data using two Queues.
There should be two data members, both being Queues to store the data internally. You may use the inbuilt Queue.
Implement the following public functions :
1. Constructor:
It initializes the data members(queues) as required.
2. push(data) :
This function should take one argument of type integer. It pushes the element into the stack and returns nothing.
3. pop() :
It pops the element from the top of the stack and, in turn, returns the element being popped or deleted. In case the stack is empty, it returns -1.
4. top :
It returns the element being kept at the top of the stack. In case the stack is empty, it returns -1.
5. size() :
It returns the size of the stack at any given instance of time.
6. isEmpty() :
It returns a boolean value indicating whether the stack is empty or not.
Operations Performed on the Stack:
Query-1(Denoted by an integer 1): Pushes an integer data to the stack. (push function)
Query-2(Denoted by an integer 2): Pops the data kept at the top of the stack and returns it to the caller. (pop function)
Query-3(Denoted by an integer 3): Fetches and returns the data being kept at the top of the stack but doesn't remove it, unlike the pop function. (top function)
Query-4(Denoted by an integer 4): Returns the current size of the stack. (size function)
Query-5(Denoted by an integer 5): Returns a boolean value denoting whether the stack is empty or not. (isEmpty function)
Input Format:
The first line contains an integer 'Q’, which denotes the number of queries to be run against each operation in the stack.
The next 'Q' lines represent an operation that needs to be performed.
For the push operation, the input line will contain two integers separated by a single space, representing the type of the operation in integer and the integer data being pushed into the stack.
For the rest of the operations on the stack, the input line will contain only one integer value, representing the query being performed on the stack.
Output Format:
For Query-1, you do not need to return anything.
For Query-2, prints the data being popped from the stack.
For Query-3, print the data kept on the top of the stack.
For Query-4, print the current size of the stack.
For Query-5, print 'true' or 'false'(without quotes).
Output for every query will be printed in a separate line.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
Constraints:
1 <= Q <= 1000
1 <= query type <= 5
-10^9 <= data <= 10^9 and data != -1
Where 'Q' is the total number of queries being performed on the stack and data represents the integer pushed into the stack.
Time Limit: 1 second
CodingNinjas
author
2y
Approach 1
- This method ensures that every new element entered in the queue ‘q1’ is always at the front.
- Hence, during pop operation, we just dequeue from ‘q1’.
- For this, we need another queue ‘q2., which...read more
CodingNinjas
author
2y
Approach 2
- We will be using two queues, ‘q1’ and ‘q2’.
- In a push operation, the new element is always enqueued to ‘q1’.
- During push operation :
- Enqueue new element ‘x’ to ‘q1’.
- During pop operation :
- One by...read more
CodingNinjas
author
2y
Approach 3
- We will be using a single queue ‘q1’.
- In a push operation, we can calculate the size of the queue ‘q1’.Hence we enqueue new data to the queue.
- Now suppose before inserting new data size of the...read more
Add answer anonymously...
Top Adobe Software Quality Engineer interview questions & answers
Popular interview questions of Software Quality Engineer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app