Stacks, Queues and Deques Previous Year Questions (PYQs) for GATE DA: 3+ Solved Questions with Step-by-Step Solutions

    Solve 3+ Stacks, Queues and Deques previous year questions for GATE DA with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Stacks, Queues, and Deques

    Chapter Journey: Linear Data Structures

    01
    Stack Operations and Simulation
    Master Push, Pop, and Top. Learn to trace complex pseudocode executions manually.
    Weightage Hint: High frequency in simulation questions.
    02
    Queue and Deque Operations
    Understand F I F O behavior, circular queues, and double-ended insertion and removal.
    Weightage Hint: Essential for scheduling and buffering concepts.
    03
    ADT Properties: FIFO, LIFO, Lookup
    Formalize the abstract definitions. Match properties to real-world applications.
    Weightage Hint: Conceptual clarity for matching and theory questions.

    The Stack Intuition: The Plate Pile

    What is a Stack?

    A Stack is a linear data structure that follows a particular order in which operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last Out).

    Real-World Analogy

    Imagine a stack of books on a table:

    1. You place Book A down.
    2. You place Book B on top of A.
    3. You place Book C on top of B.

    To access Book A, you must first remove Book C, then Book B. The last item added (C) is the first one removed.

    Book C
    Book B
    Book A

    Key Terminology

    • Top: The only end where insertion and deletion occur.
    • Push: The operation of adding an element to the top.
    • Pop: The operation of removing the element from the top.

    Stacks, Queues and Deques: Solved Questions with Step-by-Step Explanations (3 Problems)

    Question 1 · Programming, Data Structures and Algorithms NAT
    Consider the following pseudocode.

    Create empty stack S
    
    Set x=0, flag=0, sum=0
    
    Push x onto S
    
    while (S is not empty){
    
        if (flag equals 0){
    
            Set x = x+1
    
            Push x onto S}
    
        if (x equals 8):
    
        Set flag=1
    
        if (flag equals 1){
    
        x = Pop(S)
    
        if (x is odd):
    
        Pop(S)
    
        Set sum = sum + x}
    
    }
    
    Output sum

    The value of sum output by a program executing the above pseudocode is
    (Answer in integer)
    Correct Answer:

    24

    Step-by-Step Solution

    Key idea: This is a stack simulation question, recognizable because it provides pseudocode with stack operations (Push, Pop) and conditional logic, asking for the final state of a variable.

    Step 1: Initialize variables. , , . Stack is initialized with , so .

    Step 2: Trace the loop while is not empty.

    • Iterations 1 to 7: is . increments by each time and is pushed. becomes . reaches .
    • Iteration 8: , so becomes and is pushed. . Now , so becomes . Since , we execute the pop block: . is even, so the inner pop is skipped. .
    • Iteration 9: . . is odd, so we again, removing . is now . .
    • Iteration 10: . . is odd, so we again, removing . is now . .
    • Iteration 11: . . is odd, so we again, removing . is now . .
    • Iteration 12: . . is odd, so we again, removing . is now . .

    Step 3: The stack is now empty, so the loop terminates. The final value of is .

    Answer: 24

    Question 2 · Programming, Data Structures and Algorithms MCQ
    Match the items in Column 1 with the items in Column 2 in the following table:

    Column 1Column 2
    (p) First In First Out(i) Stacks
    (q) Lookup Operation(ii) Queues
    (r) Last In First Out(iii) Hash Tables
    1. A.

      (p) − (ii), (q) − (iii), (r) − (i)

    2. B.

      (p) − (ii), (q) − (i), (r) − (iii)

    3. C.

      (p) − (i), (q) − (ii), (r) − (iii)

    4. D.

      (p) − (i), (q) − (iii), (r) − (ii)

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is an ADT property matching question, recognizable because it asks to pair fundamental access patterns (FIFO, LIFO, Lookup) with their corresponding data structures.

    Step 1: Analyze "First In First Out" (p). This is the defining property of a Queue, where the first element added is the first to be removed. So, (p) matches with (ii).

    Step 2: Analyze "Last In First Out" (r). This is the defining property of a Stack, where the most recently added element is the first to be removed. So, (r) matches with (i).

    Step 3: Analyze "Lookup Operation" (q). Hash Tables are specifically designed to provide fast, average-case key-based lookup operations. So, (q) matches with (iii).

    Step 4: Combine the matches: (p) - (ii), (q) - (iii), (r) - (i).

    Answer: Option A

    Question 3 · Programming, Data Structures and Algorithms NAT
    The fundamental operations in a double-ended queue are:
    insertFirst(e) – Insert a new element e at the beginning of D.
    insertLast(e) – Insert a new element e at the end of D.
    removeFirst() – Remove and return the first element of D.
    removeLast() – Remove and return the last element of D.
    In an empty double-ended queue, the following operations are performed:
    insertFirst(10)
    insertLast(32)
    a ←removeFirst()
    insertLast(28)
    insertLast(17)
    a ←removeFirst()
    a ← removeLast()
    The value of a is ______.
    Correct Answer:

    17

    Step-by-Step Solution

    Key idea: This is a deque simulation question, recognizable because it provides a sequence of double-ended queue operations and asks for the final value of a variable.

    Step 1: Start with an empty deque .

    Step 2: insertFirst(10):

    Step 3: insertLast(32):

    Step 4: a <- removeFirst(): Removes and returns the first element (). ,

    Step 5: insertLast(28):

    Step 6: insertLast(17):

    Step 7: a <- removeFirst(): Removes and returns the first element (). ,

    Step 8: a <- removeLast(): Removes and returns the last element (). ,

    Step 9: The final value assigned to is .

    Answer: 17

    More previous year questions (pyqs) in this unit

    chapter
    Stacks, Queues and Deques Previous Year Questions (PYQs) for GATE DA: 3+ Solved Questions with Step-by-Step Solutions

    Solve 3+ Stacks, Queues and Deques previous year questions for GATE DA with answers and detailed solutions. Free sample questions below.

    A question from this chapter

    Question 1
    Consider the following pseudocode.

    Create empty stack S
    
    Set x=0, flag=0, sum=0
    
    Push x onto S
    
    while (S is not empty){
    
        if (flag equals 0){
    
            Set x = x+1
    
            Push x onto S}
    
        if (x equals 8):
    
        Set flag=1
    
        if (flag equals 1){
    
        x = Pop(S)
    
        if (x is odd):
    
        Pop(S)
    
        Set sum = sum + x}
    
    }
    
    Output sum

    The value of sum output by a program executing the above pseudocode is
    (Answer in integer)
    Question 2
    Match the items in Column 1 with the items in Column 2 in the following table:

    Column 1Column 2
    (p) First In First Out(i) Stacks
    (q) Lookup Operation(ii) Queues
    (r) Last In First Out(iii) Hash Tables
    Question 3
    The fundamental operations in a double-ended queue are:
    insertFirst(e) – Insert a new element e at the beginning of D.
    insertLast(e) – Insert a new element e at the end of D.
    removeFirst() – Remove and return the first element of D.
    removeLast() – Remove and return the last element of D.
    In an empty double-ended queue, the following operations are performed:
    insertFirst(10)
    insertLast(32)
    a ←removeFirst()
    insertLast(28)
    insertLast(17)
    a ←removeFirst()
    a ← removeLast()
    The value of a is ______.
    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.