chapter
    Programming, Data Structures and Algorithms PYQs for GATE DA

    GATE DA Programming, Data Structures and Algorithms: 3 units and 13 chapters, weightage from 31 previous year questions across 3 papers, a study order by exam

    A question from this chapter

    Question 1
    2025 PYQ
    Level 4: Challenger
    Consider the following Python code snippet.

    A={"this","that"}
    B={"that","other"}
    C={"other","this"}
    while "other" in C:
        if "this" in A:
            A,B,C=A-B,B-C,C-A
        if "that" in B:
            A,B,C=C|A,A|B,B|C

    When the above program is executed, at the end, which of the following sets contains "this"?
    Question 2
    2026 PYQ
    Level 3: Exam Standard
    A recursive function in Python is given.

    def mystery(n):
        if n <= 0:
            return 1
        else:
            return mystery(n-1) + mystery(n-2)

    Now, consider the following function call:

    mystery(4)

    Assume that a typical runtime stack is used to manage function calls. Each function call is pushed onto the stack and removed only after it finishes execution.

    Which of the following options denotes the total number of function calls (i.e., the total number of stack activations), including the initial call, to compute mystery(4)?
    Question 3
    2026 PYQ
    Level 3: Exam Standard
    Consider the given Python program.

    def fun(L, i=0):
        if i >= len(L)-1:
            return 0
        if L[i] > L[i+1]:
            L[i+1], L[i] = L[i], L[i+1]
            return 1+fun(L, i+1)
        else:
            return fun(L, i+1)

    data = [5, 3, 4, 1, 2]
    count = 0
    for _ in range(len(data)):
        count += fun(data)
    print(count)

    The output of the program is __________ . (Answer in integer)
    Question 4
    2026 PYQ
    Level 3: Exam Standard
    Consider the given Python program.

    def outer():
        x = []
        def inner(val):
            x.append(val)
            return x
        return inner

    f1 = outer()
    f2 = outer()
    print(f1(10)) # Line P
    print(f1(20)) # Line Q
    print(f2(30)) # Line R
    print(f1(40)) # Line S

    Which of the following options is/are correct?
    Question 5
    2025 PYQ
    Level 3: Exam Standard
    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 6
    2025 PYQ
    Level 3: Exam Standard
    Consider a hash table of size with indices , with the hash function


    where linear probing is used to handle collisions. The hash table is initially empty and then the following sequence of keys is inserted into the hash table: . The indices where the keys and are stored are, respectively
    Question 7
    2026 PYQ
    Level 3: Exam Standard
    You are given the following Pre-order and In-order traversals of a Binary Tree T with nodes E, F, G, P, Q, R, S.
    Pre-order: P Q S E R F G
    In-order: S Q E P F R G
    Which of the following statements is/are true about the Binary Tree T?
    Question 8
    2026 PYQ
    Level 3: Exam Standard
    Let A be a sorted array containing 1000 distinct integers. You perform a recursive binary search on A to find an element y. Suppose each comparison checks whether the middle element computed during the current recursive step is equal to, less than, or greater than y.
    The maximum number of comparisons that may have to be performed if y is not an element of A is _______ . (Answer in integer)
    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.

    Programming, Data Structures and Algorithms PYQs for GATE DA

    GATE DA Programming, Data Structures and Algorithms: 3 units and 13 chapters, weightage from 31 previous year questions across 3 papers, a study order by exam weight and 1678 practice questions.

    About Programming, Data Structures and Algorithms Previous Year Questions (PYQs)

    31 previous year questions from Programming, Data Structures and Algorithms in GATE DA, grouped by chapter with the exam year, answer key and step-by-step solution for each.

    GATE DA Programming, Data Structures and Algorithms Unit-wise Weightage from Past Papers

    We counted every GATE DA Programming, Data Structures and Algorithms previous year question in our bank (31 questions from 3 papers) and grouped them by unit.

    UnitChaptersPYQsShare of sectionAvg per paper
    Programming Fundamentals4929%3
    Data Structures3826%2.7
    Algorithms61445%4.7

    Suggested Programming, Data Structures and Algorithms Study Order for GATE DA

    1. Algorithms: 45% of past Programming, Data Structures and Algorithms questions, about 4.7 per paper.
    2. Programming Fundamentals: 29% of past Programming, Data Structures and Algorithms questions, about 3 per paper.
    3. Data Structures: 26% of past Programming, Data Structures and Algorithms questions, about 2.7 per paper.

    Start where the marks are. Units at the top of this list have appeared most often in past GATE DA papers.

    Units in GATE DA Programming, Data Structures and Algorithms

    All Programming, Data Structures and Algorithms chapters

    One Solved Question from Each Programming, Data Structures and Algorithms Chapter

    Question 1 · Python Data Structures: Lists, Sets and Dictionaries · 2025 MCQ
    Consider the following Python code snippet.

    A={"this","that"}
    B={"that","other"}
    C={"other","this"}
    while "other" in C:
        if "this" in A:
            A,B,C=A-B,B-C,C-A
        if "that" in B:
            A,B,C=C|A,A|B,B|C

    When the above program is executed, at the end, which of the following sets contains "this"?
    1. A.

      Only A

    2. B.

      Only B

    3. C.

      Only C

    4. D.

      A, C

    Correct Answer:

    B

    Step-by-Step Solution

    Insight: This is a set loop tracing question. The critical rule is that in simultaneous assignment, all right-hand side expressions are evaluated using the old values before any assignment occurs.

    Exam route: Trace the sets A, B, and C iteration by iteration. Evaluate the right-hand sides of the assignments using the current state, then update the sets, and finally recheck the loop condition.

    Learning route:

    Initial state: A={"this","that"}, B={"that","other"}, C={"other","this"}

    Iteration 1:

    • Loop condition: "other" in C is True.
    • First if-block: "this" in A is True.

    Evaluate RHS: A-B={"this"}, B-C={"that"}, C-A={"other"}.

    Assign: A={"this"}, B={"that"}, C={"other"}.

    • Second if-block: "that" in B is True.

    Evaluate RHS: C|A={"other","this"}, A|B={"this","that"}, B|C={"that","other"}.

    Assign: A={"other","this"}, B={"this","that"}, C={"that","other"}.

    Iteration 2:

    • Loop condition: "other" in C is True.
    • First if-block: "this" in A is True.

    Evaluate RHS: A-B={"other"}, B-C={"this"}, C-A={"that"}.

    Assign: A={"other"}, B={"this"}, C={"that"}.

    • Second if-block: "that" in B is False. Skip.

    Iteration 3:

    • Loop condition: "other" in C is False (C is {"that"}). Loop terminates.

    Final state: A={"other"}, B={"this"}, C={"that"}.

    The element "this" is present only in set B.

    Question 2 · Python Functions and Recursion · 2026 MCQ
    A recursive function in Python is given.

    def mystery(n):
        if n <= 0:
            return 1
        else:
            return mystery(n-1) + mystery(n-2)

    Now, consider the following function call:

    mystery(4)

    Assume that a typical runtime stack is used to manage function calls. Each function call is pushed onto the stack and removed only after it finishes execution.

    Which of the following options denotes the total number of function calls (i.e., the total number of stack activations), including the initial call, to compute mystery(4)?
    1. A.

      5

    2. B.

      9

    3. C.

      15

    4. D.

      17

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: The total number of function calls follows a recurrence relation , where the accounts for the current function call itself.

    Exam route: Compute iteratively from base cases. . Then , , , .

    Learning route: Draw the recursion tree for mystery(4). The root is 1 call. It branches to mystery(3) and mystery(2). Count all nodes in this call tree. Total nodes = 15. Note that mystery(0) and mystery(-1) are base cases that make 1 call each and return immediately without further branching.

    Question 3 · List Processing and In-Place Mutation · 2026 NAT
    Consider the given Python program.

    def fun(L, i=0):
        if i >= len(L)-1:
            return 0
        if L[i] > L[i+1]:
            L[i+1], L[i] = L[i], L[i+1]
            return 1+fun(L, i+1)
        else:
            return fun(L, i+1)

    data = [5, 3, 4, 1, 2]
    count = 0
    for _ in range(len(data)):
        count += fun(data)
    print(count)

    The output of the program is __________ . (Answer in integer)
    Correct Answer:

    8.00

    Step-by-Step Solution

    Insight: The inner function fun performs one left-to-right pass of adjacent swaps, returning the number of swaps. The outer loop calls it n times, which is exactly the Bubble Sort algorithm. The total number of swaps in Bubble Sort equals the number of inversions in the initial array.

    Exam route: Count the inversions in [5, 3, 4, 1, 2]. 5 is greater than 3, 4, 1, 2 (4 inversions). 3 is greater than 1, 2 (2 inversions). 4 is greater than 1, 2 (2 inversions). Total = 4 + 2 + 2 = 8.

    Learning route:

    Pass 1: [5, 3, 4, 1, 2] -> 5 bubbles to the end. Swaps: (5,3), (5,4), (5,1), (5,2). Count = 4. Array: [3, 4, 1, 2, 5].

    Pass 2: [3, 4, 1, 2, 5] -> 4 bubbles to index 3. Swaps: (4,1), (4,2). Count = 2. Array: [3, 1, 2, 4, 5].

    Pass 3: [3, 1, 2, 4, 5] -> 3 bubbles to index 2. Swaps: (3,1), (3,2). Count = 2. Array: [1, 2, 3, 4, 5].

    Pass 4 & 5: Already sorted, 0 swaps.

    Total count = 4 + 2 + 2 + 0 + 0 = 8.

    Question 4 · Function Scope, Closures and Default Arguments · 2026 MSQ
    Consider the given Python program.

    def outer():
        x = []
        def inner(val):
            x.append(val)
            return x
        return inner

    f1 = outer()
    f2 = outer()
    print(f1(10)) # Line P
    print(f1(20)) # Line Q
    print(f2(30)) # Line R
    print(f1(40)) # Line S

    Which of the following options is/are correct?
    1. A.

      f1 and f2 share the same list x

    2. B.

      Output of Line Q is [10, 20]

    3. C.

      Output of Line R is [10, 20, 30]

    4. D.

      Output of Line S is [10, 20, 40]

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Insight: Each call to the outer function creates a completely independent closure with its own isolated cell objects for enclosed variables.

    Exam route: f1 and f2 are created by separate calls to outer(), so they have separate x lists. f1(10) -> [10], f1(20) -> [10, 20] (Line Q). f2(30) -> [30] (Line R). f1(40) -> [10, 20, 40] (Line S).

    Learning route: This is a closure instance isolation question, recognizable by the nested function returning an inner function that modifies an enclosed mutable variable.

    Step 1: When f1 = outer() is executed, a new list x is created in outer's local scope. The inner function captures this specific list in its closure cell. f1 now points to this specific inner function instance.

    Step 2: When f2 = outer() is executed, a completely new list x is created. A new inner function instance is created, capturing this new list. f2 points to this second instance. f1 and f2 do not share any state.

    Step 3: Line P: f1(10) appends 10 to f1's list. Returns [10].

    Step 4: Line Q: f1(20) appends 20 to f1's list. Returns [10, 20]. Option B is correct.

    Step 5: Line R: f2(30) appends 30 to f2's list. Since f2's list is independent and starts empty, it returns [30]. Option C is incorrect.

    Step 6: Line S: f1(40) appends 40 to f1's list. Returns [10, 20, 40]. Option D is correct.

    Answer: Options B and D.

    Question 5 · Stacks, Queues and Deques · 2025 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 6 · Hash Tables and Collision Resolution · 2025 MCQ
    Consider a hash table of size with indices , with the hash function


    where linear probing is used to handle collisions. The hash table is initially empty and then the following sequence of keys is inserted into the hash table: . The indices where the keys and are stored are, respectively
    1. A.

      and

    2. B.

      and

    3. C.

      and

    4. D.

      and

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is a hash table tracing question, recognizable because it provides a specific hash function, collision resolution strategy, and a sequence of keys to insert.

    Step 1: Understand the hash function and table size. The table has size 10 (indices 0 to 9). The hash function is .

    Step 2: Trace the insertion of each key sequentially using linear probing.

    • Insert 1: . Index 3 is empty. Place 1 at index 3.
    • Insert 4: . Index 2 is empty. Place 4 at index 2.
    • Insert 5: . Index 5 is empty. Place 5 at index 5.
    • Insert 6: . Index 8 is empty. Place 6 at index 8.
    • Insert 14: . Index 2 is occupied (by 4).
    • Probe 1: . Index 3 is occupied (by 1).
    • Probe 2: . Index 4 is empty. Place 14 at index 4.
    • Insert 15: . Index 5 is occupied (by 5).
    • Probe 1: . Index 6 is empty. Place 15 at index 6.

    Step 3: Identify the final indices. Key 14 is at index 4, and key 15 is at index 6.

    Answer: D

    Question 7 · Binary Trees: Properties and Traversals · 2026 MSQ
    You are given the following Pre-order and In-order traversals of a Binary Tree T with nodes E, F, G, P, Q, R, S.
    Pre-order: P Q S E R F G
    In-order: S Q E P F R G
    Which of the following statements is/are true about the Binary Tree T?
    1. A.

      Node P is the root of T

    2. B. The Post-order traversal of T is:
      S E Q F G R P
    3. C.

      Node Q has only one child

    4. D.

      The left subtree of node R contains the node G

    Correct Answer:

    ["A","B"]

    Step-by-Step Solution

    Insight: Reconstruct the tree using the standard Pre-order and In-order split method.

    Exam route: Root is P. Left In-order has 3 elements, Right has 3. Split Pre-order accordingly. Build left and right subtrees. Check options.

    Learning route:

    Step 1: Root is the first element in Pre-order: P.

    Step 2: Find P in In-order. Left of P is S, Q, E (size 3). Right of P is F, R, G (size 3).

    Step 3: Split remaining Pre-order (Q, S, E, R, F, G) into Left Pre-order (Q, S, E) and Right Pre-order (R, F, G).

    Step 4: Left subtree: Pre (Q, S, E), In (S, Q, E). Root is Q. Left In is S, Right In is E. So Q has left child S, right child E.

    Step 5: Right subtree: Pre (R, F, G), In (F, R, G). Root is R. Left In is F, Right In is G. So R has left child F, right child G.

    Step 6: Evaluate options.

    A) P is root. (True)

    B) Post-order is Left Post + Right Post + Root. Left Post: S, E, Q. Right Post: F, G, R. Total: S, E, Q, F, G, R, P. (True)

    C) Q has two children (S and E). (False)

    D) G is in the right subtree of R. (False)

    Question 8 · Searching Algorithms and Binary Search · 2026 NAT
    Let A be a sorted array containing 1000 distinct integers. You perform a recursive binary search on A to find an element y. Suppose each comparison checks whether the middle element computed during the current recursive step is equal to, less than, or greater than y.
    The maximum number of comparisons that may have to be performed if y is not an element of A is _______ . (Answer in integer)
    Correct Answer:

    10.00

    Step-by-Step Solution

    Insight: A 3-way comparison counts as exactly 1 comparison per step. The max comparisons is simply the depth of the recursion tree.

    Exam route: Use the formula . For , .

    Learning route:

    The problem specifies a 3-way comparison (equal, less, greater). This counts as 1 comparison per recursive step.

    The maximum number of comparisons is the maximum depth of the recursion tree, which occurs when the element is not found or is at the deepest leaf.

    The exact formula for the maximum depth (number of comparisons) is .

    Given :

    .

    .

    Total comparisons = .

    If the problem had specified two separate checks (e.g., if A[mid] == y then else if A[mid] < y), the worst case would be 2 comparisons per step, yielding 20. But the 3-way comparison explicitly avoids this trap.