GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 49

Push and pop counts for a two-stack queue

Data Structures · Stacks & Queues · Full GATE CSE 2006 paper

GATE 2006, Q49 2 marks · MCQ
Report issue

Stacks & Queues

Question 49, GATE 2006: An initially empty queue is implemented using stacks S1,S2:
insert(Q,x): push(S1,x).
delete(Q): if S2 is empty, then if S1 is empty report empty and return; otherwise repeatedly pop S1 and push that item onto S2 until S1 is empty. Finally pop S2.
Perform n inserts and m≤n delete calls in arbitrary order. Let x be the total pushes and y the total pops on both stacks. Which printed bound holds for every n,m? Both reproductions use the strict upper bound x<2n; successful deletions are not explicitly guaranteed.
Options:
(A) n+m≤x<2n and 2m≤y≤n+m
(B) n+m≤x<2n and 2m≤y≤2n
(C) 2m≤x<2n and 2m≤y≤n+m
(D) 2m≤x<2n and 2m≤y≤2n
Question 49, GATE 2006: An initially empty queue is implemented using stacks S1,S2:
insert(Q,x): push(S1,x).
delete(Q): if S2 is empty, then if S1 is empty report empty and return; otherwise repeatedly pop S1 and push that item onto S2 until S1 is empty. Finally pop S2.
Perform n inserts and m≤n delete calls in arbitrary order. Let x be the total pushes and y the total pops on both stacks. Which printed bound holds for every n,m? Both reproductions use the strict upper bound x<2n; successful deletions are not explicitly guaranteed.
Options:
(A) n+m≤x<2n and 2m≤y≤n+m
(B) n+m≤x<2n and 2m≤y≤2n
(C) 2m≤x<2n and 2m≤y≤n+m
(D) 2m≤x<2n and 2m≤y≤2n
Tap to enlarge
Your answer
Answer & solution

Answer: No universally correct printed option (no official key was published)

1. Let t be the number of items moved from S1 to S2 and d the number of successful deletes. Then x=n+t pushes and y=t+d pops, with d≤t≤n.

2. If all m deletes succeed, d=m, so n+m≤x≤2n and 2m≤y≤n+m. These are the tight intended bounds.

3. The paper prints x<2n in all four options, which fails when all n inserts precede a delete: t=n and x=2n. Furthermore arbitrary ordering permits deletes on an empty queue; then d need not equal m and the printed lower bounds can fail.

4. If < were repaired to ≤ and successful deletions assumed, A states the tight bounds, but other looser options would also be true. Do not treat this the paper as a sound unique-answer MCQ without checking a better source.

6. A minimal counterexample is one insert followed by one delete: n=m=1 and x=y=2. This does not establish what the official original printed.

7. Teaching check: Every inserted item is pushed onto S1 exactly once and transferred to S2 at most once. A successful deletion adds one S2 pop. Counting these lifecycle events gives exact formulas before bounds are imposed; testing n=m=1 immediately exposes the printed strict inequality.

Original paper, page 7