Data Structures | Queue | Question 11

Last Updated :
Discuss
Comments

An implementation of a queue Q, using two stacks S1 and S2, is given below: 

C++
Function Insert(Q, x):
    Push x onto stack S1

Function Delete(Q):
    If stack S2 is empty:
        If stack S1 is also empty:
            Print "Q is empty"
            Return
        Else:
            While stack S1 is not empty:
                Pop element from stack S1 and store it in x
                Push x onto stack S2
    Pop element from stack S2 and store it in x
    Return x  // Or process the popped element (if needed)

Let n insert and m (<=n) delete operations be performed in an arbitrary order on an empty queue Q. Let x and y be the number of push and pop operations performed respectively in the process. Which one of the following is true for all m and n?

n+m <= x < 2n and 2m <= y <= n+m

n+m <= x < 2n and 2m<= y <= 2n

2m <= x < 2n and 2m <= y <= n+m

2m <= x <2n and 2m <= y <= 2n

Share your thoughts in the comments