GATE / CS ARCHIVE

SUBJECT COLLECTION

Data Structures · Stacks & Queues

33 questions, newest first

GATE CSE has asked 33 questions on Stacks & Queues (Data Structures) from 1991 to 2026, in 23 of the 46 papers; the most recent is in GATE CSE 2026 Set 2. In the last five years it carried 1.2 marks per 100-mark paper on average, against 0.8 since 2003. Since 2014 the questions have been 4 NAT, 4 MCQ and 3 MSQ. 4 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
2 marks · MSQ
Report issue

Reconstructing stack and queue assignments

Stacks & Queues

Question 50, GATE 2026: Items 1,2,3,4,5 arrive in order and are each placed in an initially empty stack S or queue Q (capacity 10 each). After all arrivals, empty S then Q to output 4,3,1,2,5. Possible assignments are A S={1,3,4}; B S={3,4}; C S={4,5}; D S={1,2,3}, with every other item in Q. Select all valid assignments.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, B

Step through it (4 options)

Givens and goal

Items 1,2,3,4,5 arrive in order and are each placed in an initially empty stack S or queue Q (capacity 10 each). After all arrivals, empty S then Q to output 4,3,1,2,5. Possible assignments are A S={1,3,4}; B S={3,4}; C S={4,5}; D S={1,2,3}, with every other item in Q. Select all valid assignments.

Approach

The stack part reverses its arrival subsequence; the queue part preserves its arrival subsequence.

Step-by-step solution

1. For A, the stack receives 1,3,4 and pops 4,3,1. The queue receives 2,5 and dequeues 2,5. Concatenating the two outputs gives 4,3,1,2,5, so A works.
2. For B, the stack receives 3,4 and pops 4,3. The queue receives 1,2,5 and outputs 1,2,5. The same target output results, so B also works.
3. For C, the stack pops 5,4 and the queue outputs 1,2,3, producing 5,4,1,2,3. For D, the stack pops 3,2,1 and the queue outputs 4,5, producing 3,2,1,4,5. Both differ from the target.
4. Thus select A,B. The stack prefix must be decreasing and the queue suffix increasing, but the split point is not uniquely determined: here it can be after 4,3 or after 4,3,1. No interleaved dequeue or pop is permitted before all arrivals.

Original paper, page 36 · Answer key

2 marks · MSQ
Report issue

Stacks & Queues

Question 45, GATE 2025: A stack stores records with distinct positive integer keys. Add MIN, which returns a pointer to the minimum-key record without removing it, in O(1) time while keeping PUSH and POP O(1). Which proposed augmentations suffice?
A. Every record stores a pointer to the smallest-key record below it.
B. Store only one pointer to the current minimum record.
C. Maintain an auxiliary sorted array of all present keys.
D. Maintain a min-heap of all present keys.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A

A preserves the minimum of every stack prefix, which is the information needed after future pops. For an empty stack use a null pointer. When pushing a new record r onto old top t, compare t.key with the key at t's saved below-minimum pointer, if present. The smaller of those records is the minimum of the entire old stack, so store its pointer in r. This takes O(1). MIN compares the current top with its saved below-minimum and returns the smaller. POP simply exposes the previous top, whose saved pointer is still valid because stack deletion has removed nothing below it. Thus all three operations remain O(1).

B alone supports a new minimum on PUSH but loses the information needed when POP removes the current minimum. Finding the next minimum can require scanning the remaining stack. C permits constant-time access to the smallest sorted entry, but inserting an arbitrary new key can require Θ(n) shifts. D exposes its minimum in O(1), but maintaining the heap on insertions and deletions costs O(log n), not O(1) worst case. Those structures do not meet all requirements merely because their MIN operation is fast. Select A only.

Original paper, page 34 · Answer key

2 marks · MSQ
Report issue

Generate output through two bounded stacks

Stacks & Queues

Question 48, GATE 2024: S1 has capacity 4 and initially contains bottom-to-top 100,200,300,400. S2 has capacity 2 and is empty. T pops S1 into S2; R pops S2 into S1; O pops S1 to output. Empty pops and full pushes are forbidden. Which output orders are possible? A 100,200,400,300; B 200,300,400,100; C 400,200,100,300; D 300,200,400,100.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: B, C, D

Step through it (4 options)

Approach: Give legal operation witnesses for possible orders and a storage lower bound for the impossible order.

1. Derivation. Write T for PushToS2, R for PushToS1, and O for GenerateOutput. Starting with S1 bottom-to-top100, 200, 300, 400 and empty S2: B is realized by T, T, O, R, O, R, O, O; C by O, T, O, O, R, O; D by T, O, O, R, O, O. These produce the stated sequences without exceeding S2 capacity 2. A cannot start with 100: reaching it before any output requires all three higher elements outside S1, but S2 holds only two. Thus B, C, D.

2. Explanation and checks. For B, the first two T operations park 400 then 300 in S2, exposing 200. Output 200, restore and output 300, restore and output 400, then output 100. For C, output 400 first and park only 300; this exposes both 200 and 100 before restoring 300. For D, park only 400 and output 300,200 before restoring it. No hidden extra buffer is available, which is why starting with 100 is impossible.

Original paper, page 33 · Answer key

2 marks · NAT
Report issue

Stacks & Queues

Question 59, GATE 2023: Start with empty stack S and queue Q. Push/enqueue 1,5,7,8,9,2 in that order into each. Then pop S, dequeue Q, pop S, dequeue Q. Four times, dequeue Q and push that element into S. Finally pop S twice. What is the remaining top?
Tap to enlarge
Answer & solution

Official answer: 8

Step through it (24 steps)

Write the stack from bottom to top. After the initial pushes it is [1, 5, 7, 8, 9, 2], and the queue front-to-back is [1, 5, 7, 8, 9, 2]. The two pops remove 2 and 9; the two dequeues remove 1 and 5. The stack is then [1, 5, 7, 8] and the queue [7, 8, 9, 2]. Operation VII and its three repetitions append 7, 8, 9, 2 to the stack. The final two pops remove 2 and 9, leaving top element 8.

Teaching detail. Use different ends for the two structures: a stack removes the most recently pushed item, while a queue removes the earliest enqueued one. After the four transfers, the stack bottom-to-top is [1,5,7,8,7,8,9,2] and the queue is empty. Removing its top two entries leaves [1,5,7,8,7,8], whose final entry is 8. Operation VIII says repeat VII three times after its first execution, making four transfers total. Mistaking this for only three transfers changes the trace.

Original paper, page 62 · Answer key

2 marks · NAT
Report issue

Stacks & Queues

Question 62, GATE 2022: Q1 initially contains four elements in front-to-back order 1,2,3,4 and Q2 is empty. Only Enqueue and Dequeue on either queue are permitted, with no extra storage. End with Q1 empty and Q2 front-to-back 4,3,2,1. What minimum number of Enqueue operations on Q1 is necessary?
Tap to enlarge
Answer & solution

Official answer: 0

No enqueue into Q1 is necessary. Move its front item to Q2, then rotate Q2 by moving each of its previously present items from front to back. Starting with Q2 empty, the successive states after these steps are [1], [2, 1], [3, 2, 1], [4, 3, 2, 1]. Each insertion is Enqueue(Q2, Dequeue(Q1)), and rotations are Enqueue(Q2, Dequeue(Q2)); Q1 is never enqueued. Since the count cannot be negative, the minimum is 0. As in the allowed operation model, a dequeued value can be passed directly to enqueue without an auxiliary storage data structure.

Construction using only queue operations: after transferring item k from Q1 to Q2, rotate Q2 exactly k−1 times by Enqueue(Q2,Dequeue(Q2)). Before rotation Q2=[k−1,…,1,k], and afterward [k,k−1,…,1]. Induction establishes reversal after four transfers. The rotations cost enqueues on Q2, which the objective does not count.

Original paper, page 40 · Answer key

1 mark · NAT
Report issue

Stacks & Queues

Question 21, GATE 2021: An initially empty stack executes push(54); push(52); pop(); push(55); push(62); s=pop(). An initially empty queue executes enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q=dequeue(). Find s+q.
Tap to enlarge
Answer & solution

Official answer: 86

Step through it (14 steps)

The first pop removes 52. After pushing 55 and 62, the next pop gives s=62. In the queue the first dequeue removes 21, leaving 24 at the front even after 28 and 32 are enqueued. Thus q=24 and s+q=86.

Write stack contents bottom-to-top: [54], [54,52], [54], [54,55], [54,55,62], then pop returns 62. Write queue contents front-to-back: [21], [21,24], [24], [24,28], [24,28,32], then dequeue returns 24. The stack is last-in-first-out and the queue first-in-first-out. Recording contents after every operation prevents confusing the earlier discarded pop/dequeue results with s and q.

Original paper, page 21 · Answer key

1 mark · MCQ
Report issue

Stacks & Queues

Question 13, GATE 2017: A circular queue uses singly linked nodes, each with one next pointer, and exactly two external pointers FRONT and REAR. For O(1) insertion and deletion, which general structural conditions hold? I:front.next points to rear. II:rear.next points to front. A:I only. B:II only. C:both. D:neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. For queue order to follow next links, the circular chain must run FRONT → second → ... → REAR → FRONT. Its closing link is therefore REAR.next=FRONT, statement II.

2. To enqueue node u, set u.next=FRONT, REAR.next=u and REAR=u. To dequeue, save the old front, advance FRONT=FRONT.next and set REAR.next=FRONT. Both require only a fixed number of pointer changes, with empty and singleton cases handled separately.

3. If links instead run backward through queue order, removing the front would require finding its predecessor by traversal. Statement I is not the general rule, even though both statements happen to hold in a two-node circle. Choose B.

Original paper, page 5 · Answer key

1 mark · MCQ
Report issue

Stacks & Queues

Question 10, GATE 2016: An efficiently implemented array-based queue contains n items. Which complexity statement is correct? A: Both ENQUEUE and DEQUEUE can be O(1). B: At most one is O(1), with the other worst-case Ω(n). C: Both are worst-case Ω(n). D: Both are worst-case Ω(log n).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

A fixed-capacity circular array stores front/rear indices and wraps them modulo capacity. Enqueue writes at rear and advances it; dequeue reads at front and advances it. Both take O(1) without shifting elements, A, with overflow handled separately from dynamic resizing.

Maintain an item count or an extra full/empty flag to distinguish a full circular queue from an empty one when indices coincide. Each operation performs a bounded number of array accesses and index updates independent of n. Removing the head does not require compacting the stored elements because the front index identifies the new head. An implementation that shifts every item is possible, but the question asks what an efficient representation can achieve, not what every array implementation does.

Original paper, page 5 · Answer key

2 marks · NAT
Report issue

Stacks & Queues

Question 41, GATE 2016: Initially queue Q holds 16 numbers and stack S is empty. Head and Top inspect without removing. Repeat while Q is nonempty: if S is empty or Top(S)≤Head(Q), dequeue Q's head and push it on S; otherwise pop S's top and enqueue it at Q's tail. What is the maximum number of loop iterations?
Tap to enlarge
Answer & solution

Official answer: 256.0 : 256.0

Our answer: 256

Let F(n) be the maximum number of loop iterations with n queued values and no relevant stack entries. Until a minimum queued value is pushed, each value originally preceding that minimum is pushed at most once and popped at most once: popped items return behind the minimum. Thus this phase costs at most 2n−1 iterations. Once the minimum is pushed it never pops, leaving at most n−1 values to process above it. Hence F(n)≤2n−1+F(n−1), giving F(n)≤n². A strictly descending initial queue attains equality at each phase, because all preceding values are pushed and popped and return in descending order behind the minimum. Therefore F(16)=256.

Each loop iteration moves one item either from Q to S or from S to Q. For a descending input 16,15,...,1, the first phase moves 15 earlier values onto the stack and back off before finally pushing 1:31 moves. That 1 never leaves, and the remaining values are again descending, giving phases 29,27,...,1. Their sum is 162=256. The upper-bound argument counts at most one push and one pop before the next minimum is fixed, so the example achieves the maximum rather than merely a large runtime.

Original paper, page 16 · Answer key

1 mark · MCQ
Report issue

Stacks & Queues

Question 28, GATE 2015: Evaluate the postfix expression whose tokens are: 10 5 + 60 6 / * 8 −. Options: (A) 284; (B) 213; (C) 142; (D) 71.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (10 steps)

1. A stack evaluator pops the right operand first and left operand second. After + the stack contains 15; after / it contains 15,10; after * it contains 150; the final subtraction is 150−8, not 8−150.

2. Postfix evaluation gives 10+5=15, 60/6=10, then 15·10=150, and 150−8=142. Thus C.

Original paper, page 62 · Answer key

2 marks · MCQ
Report issue

Stacks & Queues

Question 41, GATE 2014: A stack supports PUSH, POP, and REVERSE, which reverses its current element order. Which statement about implementing a queue is true? A impossible; B ENQUEUE takes one instruction and DEQUEUE two; C ENQUEUE takes three and DEQUEUE one; D both take one.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Set up the reasoning. Choose and maintain an invariant: the queue's front is always at the stack top. Then removing the front is simply POP; a newly enqueued item must be placed at the bottom without disturbing the old order.

2. Derive the result. Keep the queue front at the stack top. ENQUEUE(x) performs REVERSE, PUSH(x), REVERSE, placing x at the bottom while preserving the previous order. DEQUEUE performs POP. Thus three and one instructions respectively, C. A single reversal followed by a pop would leave the remaining stack in the wrong orientation for an arbitrary future operation sequence.

3. Check the interpretation. REVERSE, PUSH, REVERSE inserts at the bottom and restores the invariant. Counting only the reversal and pop needed for one isolated removal ignores the orientation needed by later queue operations.

Original paper, page 40 · Answer key

2 marks · MCQ
Report issue

Empty and full circular queue conditions

Stacks & Queues

Question 35, GATE 2012: Suppose a circular queue of capacity (n −1) elements is implemented with an array of n elements.
        Assume that the insertion and deletion operations are carried out using REAR and FRONT as array
        index variables, respectively. Initially, REAR = FRONT = 0. The conditions to detect queue full
        and queue empty are
        (A) full: (REAR+1) mod n == FRONT               (B) full: (REAR+1) mod n == FRONT
            empty: REAR == FRONT                            empty: (FRONT+1) mod n == REAR

        (C) full: REAR == FRONT                         (D) full: (FRONT+1) mod n == REAR
            empty: (REAR+1) mod n == FRONT                 empty: REAR == FRONT
Tap to enlarge
Your answer
Answer & solution

Official answer: A

With one of n slots deliberately unused, equal FRONT and REAR denotes the initial empty queue and all subsequent empty states. The queue is full when advancing REAR by one modulo n would collide with FRONT: (REAR+1) mod n=FRONT. These conditions permit n−1 stored elements and distinguish empty from full. Hence A.

Check: For example, with n=4 the usable capacity is three. Starting at FRONT=REAR=0, three insertions advance REAR to 3; its next value would be 0, equal to FRONT, so the full test succeeds without confusing it with the empty state.

Original paper, page 8 · Answer key

2 marks · MCQ
Report issue

Stack after the first multiplication

Stacks & Queues

Question 38, GATE 2007: Evaluate the postfix expression 8 2 3 ^ / 2 3 * + 5 1 * − using a stack; ^ denotes exponentiation. What are the top two stack elements, top first, immediately after the first * is evaluated?
Options:
(A) 6,1
(B) 5,7
(C) 3,2
(D) 1,5
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (9 steps)

1. Approach. For each operand, push; for each binary operator, pop right operand then left operand, compute left op right, and push the result. Stop at the first multiplication rather than evaluating the whole expression.

2. Derivation and interpretation. Read 8,2,3, then exponentiation replaces 2,3 with 8; division replaces 8,8 with 1. Push 2 and 3; the first multiplication replaces them with 6. The stack is now [1,6] from bottom to top, so its top two elements are 6,1, A.

Original paper, page 9

2 marks · MCQ
Report issue

Push and pop counts for a two-stack queue

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

1 mark · MCQ
Report issue

Two stacks sharing an array

Stacks & Queues

Question 3, GATE 2004: Two stacks share A[1..MAXSIZE], growing from opposite ends. top1<top2 identify their topmost occupied cells. What full condition uses the available space efficiently?
Options:
(A) top1=MAXSIZE/2 AND top2=MAXSIZE/2+1
(B) top1+top2=MAXSIZE
(C) top1=MAXSIZE/2 OR top2=MAXSIZE
(D) top1=top2−1
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. The unused positions lie strictly between top1 and top2. There is no free position exactly when the tops are adjacent, top1=top2−1, D.

2. This lets either stack use all available space instead of reserving half the array.

3. Teaching check: The number of unused cells is top2−top1−1. Setting this to zero immediately gives the full condition. A fixed half-array boundary can falsely declare overflow while the other stack leaves unused cells, defeating the purpose of sharing capacity.

Original paper, page 1

1 mark · MCQ
Report issue

Balanced parentheses

Stacks & Queues

Question 5, GATE 2004: Which listed data structure is best for checking whether parentheses in an arithmetic expression are balanced?
Options:
(A) Queue
(B) Stack
(C) Tree
(D) List
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. A stack matches each closing delimiter with the most recently unmatched opening delimiter. Reject an unmatched/mismatched close and accept only if the stack is empty at the end.

2. Thus B. With only one kind of parenthesis a counter is sufficient, but stack is the appropriate listed general delimiter-matching structure.

3. Teaching check: On an opening parenthesis, push it; on a closing one, require and pop its matching opener. The last-in-first-out rule handles nesting such as (()()). A FIFO queue does not retain the most recently opened delimiter at its removal end.

Original paper, page 1

2 marks · MCQ
Report issue

One-pointer circular queue

Stacks & Queues

Question 36, GATE 2004: A queue is represented by a singly linked circular list: front→successive queue nodes→rear→front. One pointer p is available. Where should p point so both enqueue and dequeue take constant time?
Options:
(A) Rear node
(B) Front node
(C) Impossible with one pointer
(D) Node next to front
Question 36, GATE 2004: A queue is represented by a singly linked circular list: front→successive queue nodes→rear→front. One pointer p is available. Where should p point so both enqueue and dequeue take constant time?
Options:
(A) Rear node
(B) Front node
(C) Impossible with one pointer
(D) Node next to front
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Keep p at the rear, whose next pointer is the front. Enqueue inserts after p and advances p to the new rear; dequeue removes p.next, with a special empty result for a singleton.

2. Both take constant time, A. Keeping only the front would require finding its predecessor to update the rear link.

3. Teaching check: For enqueue, set new.next=p.next, then p.next=new and p=new. For dequeue, remove p.next and advance that link; if it was the sole node, mark the queue empty. A circular rear pointer therefore exposes both ends without a predecessor scan.

Original paper, page 5

2 marks · MCQ
Report issue

Postfix with right-associative exponentiation

Stacks & Queues

Question 38, GATE 2004: Operators +,−,× are left-associative and ^ is right-associative. Precedence from highest to lowest is ^,×,+,−. Find postfix for a+b×c−d^e^f. Tokens in each alternative are separated for readability.
Options:
(A) a b c × + d e f ^ ^ −
(B) a b c × + d e ^ f ^ −
(C) a b + c × d − e ^ f ^
(D) − + a × b c ^ ^ d e f
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step through it (13 steps)

1. The grouping is(a+(b×c))−(de^f). Write each subtree’s operands before its operator: a b c × + d e f ^ ^ −.

2. Thus A. Right associativity places the ef operation before raising d to that result.

3. Teaching check: The parse first builds b×c and ef, then de^f, then a+(b×c), and finally subtraction. Postorder traversal emits operands before operators. The alternative with d e ^ f ^ instead encodes (de)f and violates right associativity.

Original paper, page 5

2 marks · MCQ
Report issue

Average residence time in a push-then-pop stack

Stacks & Queues

Question 64, GATE 2003: An initially empty stack of capacity n≥1 receives pushes of 1,...,n in order, followed by n pops. Each push/pop takes X seconds; a gap of Y seconds separates the end of each operation from the start of the next. The stack-life of m is the interval from the end of push(m) to the start of the pop removing m. What is the average stack-life?
A. n(X+Y)
B. 3Y+2X
C. n(X+Y)−X
D. Y+2X
Question 64, GATE 2003: An initially empty stack of capacity n≥1 receives pushes of 1,...,n in order, followed by n pops. Each push/pop takes X seconds; a gap of Y seconds separates the end of each operation from the start of the next. The stack-life of m is the interval from the end of push(m) to the start of the pop removing m. What is the average stack-life?
A. n(X+Y)
B. 3Y+2X
C. n(X+Y)−X
D. Y+2X
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. LIFO means m is removed after the n−m elements above it. Between the two specified endpoints lie n−m later pushes and n−m earlier pops, a total of 2(n−m) complete operations.
2. There is one more gap than intervening operations, so life(m)=2(n−m)X+[2(n−m)+1]Y. The push of m and its own pop are excluded by the endpoint definition.
3. Average n−m over m=1,...,n: [0+...+(n−1)]/n=(n−1)/2. Substitution gives (n−1)X+nY=n(X+Y)−X.
4. Hence C. For n=1, life is just Y, confirming the endpoint accounting.

Solution: For item m, there are n−m later pushes and n−m earlier pops between the end of push(m) and the start of its pop. Those 2(n−m) complete intervening operations cost 2(n−m)X and the 2(n−m)+1 gaps cost [2(n−m)+1]Y. Average n−m over m=1,…,n is (n−1)/2, so the mean is (n−1)X+nY=n(X+Y)−X, option C. For n=1 it correctly reduces to the single gap Y.

Original paper, page 9

2 marks · MCQ
Report issue

Stack space for expression evaluation

Stacks & Queues

Question 2.19, GATE 2002: To evaluate an expression containing no embedded function calls, which claim is correct?
(A) One stack suffices.
(B) Two stacks are required.
(C) The number of required stacks equals the expression-tree height.
(D) A Turing machine is required in the general case.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step 1: Walk through a one-stack evaluation.

Step 2: For postfix tokens a b + c *, push a then b, pop them and push a+b, then push c, and finally replace the top two values with(a+b)c. The same procedure handles arbitrary finite postfix expressions by induction on the expression tree. Extra stacks can help conversion or implementation, but are not inherently required for evaluation.

Conclusion: For a postfix representation, push operands and replace the required top operands by the result whenever an operator is encountered. One evaluation stack suffices for any finite expression tree without embedded calls, A. Infix parsing can use a combined stack representation or first produce postfix; two separate stacks are a convenient implementation, not a necessity.

Original paper, page 5

2 marks · MCQ
Report issue

Implementing a queue with stacks

Stacks & Queues

Question 2.16, GATE 2001: Under the usual stack-only auxiliary-storage model, what is the minimum number of size-n stacks needed to implement a size-n queue: one, two, three, or four?
Tap to enlarge
Your answer
Answer & solution

Answer: B under the standard auxiliary-storage model (no official key was published)

Step 1: Push new elements onto an input stack. To dequeue, pop from an output stack; when that stack is empty, move all input elements to it. The reversal places the oldest element on top, so two stacks suffice.

Step 2: With only one LIFO stack and O(1) other element storage, reaching the oldest of n elements requires removing and retaining n−1 newer elements somewhere, so the usual general implementation needs the second stack.

Hence B in this model. A recursive “one explicit stack” implementation uses the call stack as another unbounded stack; allowing an auxiliary array or such hidden storage changes what is being counted.

Original paper, page 6

1 mark · MCQ
Report issue

Algorithm and data-structure matching

Stacks & Queues

Question 1.13, GATE 2000: Match X depth-first search,Y breadth-first search,Z sorting to 1 heap,2 queue,3 stack.
Question 1.13, GATE 2000: Match X depth-first search,Y breadth-first search,Z sorting to 1 heap,2 queue,3 stack.
Tap to enlarge
Your answer
Answer & solution

Answer: C: X3,Y2,Z1 (no official key was published)

Step 1: DFS must return to the latest unfinished branch, a last-in-first-out behavior implemented by an explicit stack or recursive call stack. Thus X→3.

Step 2: BFS processes older discovered vertices first, using a FIFO queue, so Y→2. A heap supports repeated minimum/maximum extraction for sorting, so Z→1.

Together these matches give C. The sorting match refers to heap sort, not to every possible sorting algorithm.

Original paper, page 2

Descriptive
Report issue

Implement queue with reversible stack

Stacks & Queues

Question 13(a), GATE 2000: A stack supports PUSH, POP and REVERSE, which reverses all its elements. Implement ENQUEUE with one operation and DEQUEUE with three.
Question 13(a), GATE 2000: A stack supports PUSH, POP and REVERSE, which reverses all its elements. Implement ENQUEUE with one operation and DEQUEUE with three.
Tap to enlarge
Answer & solution

Answer: ENQUEUE(x): PUSH(x). DEQUEUE: REVERSE; x = POP(); REVERSE; return x. (no official key was published)

Maintain the queue front at the bottom of the stack and its rear at the top. If queue order is a,b,c (a oldest), the stack bottom-to-top is [a,b,c]. ENQUEUE(d) executes PUSH(d), producing [a,b,c,d], exactly the required FIFO order. To remove the oldest element, REVERSE changes the stack to [d,c,b,a], POP returns a, and the second REVERSE restores [b,c,d]. Thus the invariant is restored after each completed operation. The return statement is not an additional stack operation. On an empty queue, detect underflow rather than POP an absent item. The question counts interface operations, not their implementation costs: REVERSE may require linear time in an ordinary stack representation.

Original paper, page 8

Report issue

Trace first postfix prefix

Stacks & Queues

Question 13(b)(i), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 +.
Question 13(b)(i), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 +.
Question 13(b)(i), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 +.
Tap to enlarge
Answer & solution

Answer: [10, 7], top = 7 (no official key was published)

Step through it (7 steps)

Read left to right. An operand is pushed; an operator pops the right operand first and then the left, computes left operator right, and pushes the result. The successive stacks are [5], [5,2], [10], [10,3], [10,3,4], [10,7]. The multiplication consumes 5 and 2 to produce 10; the addition consumes only 3 and 4 to produce 7. The earlier 10 remains below the new result. Therefore the requested stack is bottom [10,7] top.

Original paper, page 8

Report issue

Trace second postfix prefix

Stacks & Queues

Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Question 13(b)(ii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack after prefix 5 2 * 3 4 + 5 2.
Tap to enlarge
Answer & solution

Answer: [10, 7, 5, 2], top = 2 (no official key was published)

Step through it (9 steps)

The prefix 5 2 * 3 4 + leaves [10,7]: 5*2=10 and 3+4=7 are two independent partial results. The next token 5 is an operand, so push it to obtain [10,7,5]. Push the next operand 2 to obtain [10,7,5,2]. Stop here: the following multiplication has not yet been read, so 5 and 2 must remain separate entries. The top is 2 and the bottom is 10.

Original paper, page 8

Report issue

Finish postfix evaluation

Stacks & Queues

Question 13(b)(iii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack at the end.
Question 13(b)(iii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack at the end.
Question 13(b)(iii), GATE 2000: Evaluate the postfix expression 5 2 * 3 4 + 5 2 * * + with single-digit operands, binary addition and multiplication. Stacks below are bottom-to-top. Give the stack at the end.
Tap to enlarge
Answer & solution

Answer: [80] (no official key was published)

Step through it (12 steps)

After 5 2 * 3 4 + 5 2 the stack is [10,7,5,2]. The next * replaces the top two entries 5,2 by 10, leaving [10,7,10]. The following * replaces 7,10 by 70, leaving [10,70]. Finally + replaces 10,70 by 80, leaving [80]. There are six operand tokens and five binary operators, so a valid complete evaluation must leave exactly one entry. In infix form the expression is (5*2)+((3+4)*(5*2))=10+7*10=80.

Original paper, page 8

Descriptive
Report issue

Postfix conversion

Stacks & Queues

Question 19(b), GATE 1998: Find postfix for 3*log(x+1)−a/2.
Question 19(b), GATE 1998: Find postfix for 3*log(x+1)−a/2.
Tap to enlarge
Answer & solution

Answer: 3 x 1 + log * a 2 / − (no official key was published)

Step through it (14 steps)

First parenthesize by the usual precedence and the unary function call: (3*log(x+1))−(a/2). A postfix traversal writes operands before their operator. The subtree x+1 becomes x 1 +; applying the unary log appends log, giving x 1 + log. Multiplication by 3 becomes 3 x 1 + log *. Independently, a/2 becomes a 2 /. Finally append subtraction after both subexpressions:

3 x 1 + log * a 2 / −

The spaces separate tokens, and log is one unary operator rather than three operands. Evaluating this stack form pushes 3, forms log(x+1), multiplies, forms a/2, then subtracts the right value from the left. Reversing the final operands would change the expression.

Original paper, page 7

1 mark · MCQ
Report issue

Infix-to-postfix conversion storage

Stacks & Queues

Question 1.7, GATE 1997: Which structure is essential for efficient infix-to-postfix conversion: operator stack, operand stack, both stacks, or parse tree?
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Operands can be emitted directly to the postfix output. Operators must wait until higher-precedence operators and parenthesized subexpressions are resolved, so a stack stores pending operators and delimiters. When an operator is popped, it is appended to the output. Thus the standard shunting-yard conversion needs an operator stack, A, not an operand-value stack. An operand stack is used later to evaluate postfix, and an explicit parse tree is an alternative representation rather than a requirement of this direct conversion.

Original paper, page 1

2 marks · MCQ
Report issue

Implementing a stack with delete-min

Stacks & Queues

Question 4.7, GATE 1997: A priority queue is used to implement a LIFO stack. PUSH inserts an item with a chosen integer key; POP deletes the minimum. Should keys be nonincreasing, nondecreasing, strictly increasing, or strictly decreasing?
Tap to enlarge
Your answer
Answer & solution

Answer: D as the sufficient unambiguous scheme (no official key was published)

The newest live item must have the smallest key so it is returned first. Assign successively decreasing keys, for example 0, −1, −2, …; then delete-min exactly follows last-in-first-out order. Strict decrease prevents ties whose unspecified resolution could return an older item. Thus D is the standard sufficient choice. A merely nonincreasing sequence permits equal keys and does not guarantee LIFO without an extra tie rule. Keys need only outrank still-live items, so global decrease is a simple implementation policy rather than the only possible schedule-aware assignment.

Original paper, page 4

1 mark · MCQ
Report issue

List and queue statements

Stacks & Queues

Question 1.12, GATE 1996: Assess FIFO for stacks, broad linked-list efficiency, circular queues reusing array slots, and LIFO for queues.
Question 1.12, GATE 1996: Assess FIFO for stacks, broad linked-list efficiency, circular queues reusing array slots, and LIFO for queues.
Tap to enlarge
Your answer
Answer & solution

Answer: A under the intended linked-list operation model: statements ii and iii (no official key was published)

A stack is LIFO, not FIFO; a queue is FIFO, not LIFO. In a circular array queue, the rear wraps around to locations freed by dequeues, so statement iii is correct. Linked lists permit constant-time insertion or deletion when the relevant node or predecessor is already known, avoiding array shifts; this motivates statement ii and the intended pair ii,iii. However, the assertion that linked lists are more efficient for almost all basic operations is not a mathematical universal: indexed access and cache-local traversal often favor arrays, and locating a node can require linear time. The operation and representation model must be specified.

Original paper, page 2

Report issue

A stack permutation

Stacks & Queues

Question 1.14, GATE 1994: Input 1,2,3,4,5 arrives in order. Which output is possible with one stack: 34512,34521,15234,54312?
Tap to enlarge
Your answer
Answer & solution

Answer: B: 34521 (no official key was published)

Step through it (4 options)

Push 1,2,3 and pop 3; push 4 and pop 4; push 5 and pop 5; then pop 2 and 1. This constructs 34521. For 34512, after producing 345 the stack has 2 above 1, so 1 cannot come next. For 15234, after popping 1 then pushing through 5, the next available value is 4, not 2. For 54312, after popping 5,4,3, value 2 still blocks 1. Thus only B is feasible.

Original paper, page 2

5 marks · Descriptive
Report issue

Queue-to-stack transfer preserving front order

Stacks & Queues

Question 26, GATE 1994: A queue Q contains n items and stack S is empty. Move all items into S so the original queue front is the stack top, with all remaining relative order preserved. Use only queue Delete/Insert and stack Push/Pop, O(n) time and O(1) extra storage.
Tap to enlarge
Answer & solution

Answer: Transfer Q→S, then S→Q, then Q→S; use three n-iteration loops. (no official key was published)

Use a single item variable x and a loop counter. First repeat n times: x:=Delete(Q); Push(S,x). Next repeat n times: x:=Pop(S); Insert(Q,x). Finally repeat n times: x:=Delete(Q); Push(S,x). Here Delete removes the queue front, Insert appends at its rear, Push adds at the stack top, and Pop removes the top. No direct indexing or knowledge of either implementation is needed.

Let the original queue, from front to rear, be q1,q2,…,qn. After the first pass Q is empty and S is qn,…,q2,q1 from top to bottom, because each successive pushed item covers the previous one. The second pass pops in that reversed order and enqueues it, producing Q front-to-rear qn,…,q2,q1 and leaving S empty. The third pass reverses that queue order once more on the stack, leaving S top-to-bottom q1,q2,…,qn, exactly as required, and Q empty.

There are six primitive operations per item across the three passes, hence 6n operations and O(n) time under the usual constant-time ADT operations. Only x and counters are additional storage. The originally supplied queue and stack are reused rather than allocating another container. Recursively popping and pushing could also reverse an order but would consume O(n) call-stack storage and would violate the additional-space condition. The empty case performs no operations and already meets the postcondition.

Original paper, page 7

2 marks · MSQ
Report issue

Tracing stack operations

Stacks & Queues

Question 3(vii), GATE 1991: Execute PUSH 10, PUSH 20, POP, PUSH 10, PUSH 20, POP, POP, POP, PUSH 20, POP. Options include A20, 10, 20, 10, 20; B20, 20, 10, 10, 20; C10, 20, 20, 10, 20; D20, 20, 10, 20, 10.
Question 3(vii), GATE 1991: Execute PUSH 10, PUSH 20, POP, PUSH 10, PUSH 20, POP, POP, POP, PUSH 20, POP. Options include A20, 10, 20, 10, 20; B20, 20, 10, 10, 20; C10, 20, 20, 10, 20; D20, 20, 10, 20, 10.
Tap to enlarge
Select all that apply
Answer & solution

Answer: B: 20, 20, 10, 10, 20 (no official key was published)

Step through it (12 steps)

After the first two pushes, the stack from bottom to top is [10, 20], so the first pop yields 20. Pushing 10 then 20 leaves [10, 10, 20]; the next three pops yield 20, 10, 10 and empty the stack. The final push/pop yields 20. Concatenating these five outputs gives 20, 20, 10, 10, 20. No pop is attempted on an empty stack. The result follows last-in-first-out order, not the chronological order of pushes as a queue would use.

Original paper, page 3