GATE DA Programming, Data Structures and Algorithms PYQ with Solutions
Also searched as: GATE Python PYQ · GATE DSA PYQ · GATE Data Structures PYQ
All 9 Programming, Data Structures and Algorithms questions from the GATE Data Science & Artificial Intelligence (DA) papers, starting with GATE DA 2026, with the official answer key and a step-by-step solution. Programming, Data Structures and Algorithms carries about 14 marks per paper on average. Questions are grouped chapter-wise, newest paper first.
| 2026 |
|---|
| 14 |
Programming, Data Structures and Algorithms: Python Programming — GATE DA PYQ (3)
- GATE DA 2026 · Q16Programming, Data Structures and Algorithmsmcq1 markConsider the given Python program.
Which of the following is the correct output of this program?pythondef append_to_lst(val, lst=[]): lst.append(val) return lst print(append_to_lst(1)) print(append_to_lst(2)) print(append_to_lst(3, []))- A.
[1] [2] [3] - B.✓
[1] [1, 2] [3] - C.
[1] [2] [1, 2, 3] - D.
[1] [1, 2] [1, 3]
Open GATE DA 2026 Q16 · discuss this question →Answer: B
Solution
A default argument value is created once, when the function is defined, and the same list object is reused on every call that does not passlst. -append_to_lst(1): the default list becomes[1]. -append_to_lst(2): the same default list becomes[1, 2]. -append_to_lst(3, []): a new empty list is passed, so the result is[3]. Answer: (B). - A.
- GATE DA 2026 · Q50Programming, Data Structures and Algorithmsmsq2 marksConsider the given Python program.
Which of the following options is/are correct?pythondef 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- A.f1 and f2 share the same list x
- B.Output of Line Q is [10, 20]✓
- C.Output of Line R is [10, 20, 30]
- D.Output of Line S is [10, 20, 40]✓
Open GATE DA 2026 Q50 · discuss this question →Answer: B, D
Solution
Each call toouter()creates a new listx, and the returnedinnerkeeps (closes over) its ownx. Sof1andf2have separate lists. - Line P:f1list is[10]- Line Q:[10, 20]- Line R:f2list is[30]- Line S:f1list is[10, 20, 40]Answer: (B), (D). - A.
- GATE DA 2026 · Q58Programming, Data Structures and Algorithmsnat2 marksConsider the given Python program.
The output of the program is __________ . (Answer in integer)pythondef 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)Open GATE DA 2026 Q58 · discuss this question →Answer: 8
Solution
funmakes one bubble-sort pass over the list and returns the number of swaps in that pass. The loop runs 5 passes, socountis the total number of swaps bubble sort makes. Each bubble-sort swap removes exactly one inversion, so the total equals the number of inversions in[5, 3, 4, 1, 2]: - 5 is before and larger than 3, 4, 1, 2: 4 inversions. - 3 is before and larger than 1, 2: 2. - 4 is before and larger than 1, 2: 2. - 1 before 2: 0. Total . Pass by pass: 4, then 2, then 2, then 0, then 0 swaps. Answer: 8.
Programming, Data Structures and Algorithms: Sorting — GATE DA PYQ (2)
- GATE DA 2026 · Q15Programming, Data Structures and Algorithmsmcq1 markConsider that the quick sort algorithm is used to sort an array of distinct randomly ordered elements. In every call, the pivot is chosen as the first element of the current subarray. Let denote the expected time to sort the array. Assume that the time to partition is linear in the size of the current subarray. Which of the following recurrence relations correctly represents in this scenario?
- A.
- B.
- C.
- D.✓
Open GATE DA 2026 Q15 · discuss this question →Answer: D
Solution
The input is in random order, so the first element is equally likely to be any rank. If the pivot is the -th smallest, the two subarrays have sizes and , each with probability . Averaging over all pivot positions and adding linear partition time:This solves to . (A) is the worst case, and (B) and (C) assume fixed split ratios, which is not the expected case. Answer: (D). - A.
- GATE DA 2026 · Q49Programming, Data Structures and Algorithmsmsq2 marksConsider the problem of sorting the given array in ascending order: P = [1, 2, 3, 5, 4] Consider two sorting algorithms Bubble Sort (BS) and Insertion Sort (IS). Let N1 be the total number of comparisons done by BS on the elements of P and N2 be the total number of comparisons done by IS on the elements of P. Which of the following options is/are correct?
- A.N1 = 10, N2 = 4
- B.N1 > N2✓
- C.IS on P will perform only one swap✓
- D.Both BS and IS on P will make at least one unnecessary comparison (i.e., comparing elements that are already in correct order)✓
Open GATE DA 2026 Q49 · discuss this question →Answer: B, C, D
Solution
Insertion sort, inserting each element into the sorted prefix: - Inserting 2: 1 comparison (with 1). - Inserting 3: 1 comparison (with 2). - Inserting 5: 1 comparison (with 3). - Inserting 4: compare with 5 and shift, then compare with 3 and stop. 2 comparisons, 1 swap. So , with exactly one swap. Bubble sort: the standard version does comparisons. Even the early-exit version does (the second pass makes no swaps). Either way . - (A) , not 4. False. - (B) True. - (C) True. - (D) Both compare already-ordered pairs such as (1, 2). True. Answer: (B), (C), (D). - A.
Programming, Data Structures and Algorithms: Trees — GATE DA PYQ (1)
- GATE DA 2026 · Q25Programming, Data Structures and Algorithmsmsq1 markYou are given the following Pre-order and In-order traversals of a Binary Tree T with nodes E, F, G, P, Q, R, S.
Which of the following statements is/are true about the Binary Tree T?Pre-order: P Q S E R F G In-order: S Q E P F R G- A.Node P is the root of T✓
- B.The Post-order traversal of T is: S E Q F G R P✓
- C.Node Q has only one child
- D.The left subtree of node R contains the node G
Open GATE DA 2026 Q25 · discuss this question →Answer: A, B
Solution
The first pre-order element, P, is the root. In the in-order sequence, S Q E lies to the left of P and F R G to the right. - Left part: next in pre-order is Q, so Q is the root with S on the left and E on the right. - Right part: next is R, so R is the root with F on the left and G on the right.
- (A) P is the root. True. - (B) Post-order (left, right, root): S E Q F G R P. True. - (C) Q has two children. False. - (D) G is the right child of R. False. Answer: (A), (B).P / \ Q R / \ / \ S E F G - A.
Programming, Data Structures and Algorithms: Searching — GATE DA PYQ (1)
- GATE DA 2026 · Q31Programming, Data Structures and Algorithmsnat1 markLet 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)Open GATE DA 2026 Q31 · discuss this question →
Answer: 10
Solution
Each recursive step does one (three-way) comparison and then continues in a half of size at most :That is 10 steps before the range becomes empty. In general the worst case is comparisons. Answer: 10.
Programming, Data Structures and Algorithms: Recursion — GATE DA PYQ (1)
- GATE DA 2026 · Q39Programming, Data Structures and Algorithmsmcq2 marksA recursive function in Python is given.
Now, consider the following function call:pythondef mystery(n): if n <= 0: return 1 else: return mystery(n-1) + mystery(n-2)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)?- A.5
- B.9
- C.15✓
- D.17
Open GATE DA 2026 Q39 · discuss this question →Answer: C
Solution
Let be the number of calls made bymystery(n), counting itself. Then for , and for . - - - - - Answer: (C) 15. - A.
Programming, Data Structures and Algorithms: Graphs — GATE DA PYQ (1)
- GATE DA 2026 · Q40Programming, Data Structures and Algorithmsmcq2 marksConsider a directed graph , where is the finite set of vertices and is the set of directed edges between the vertices. may contain cycles but there is no self-loop. Further, may not be strongly connected. Let be the graph obtained by reversing the directions of all the edges in without changing the set of vertices. Assume that Breadth First Search (BFS) or Depth First Search (DFS) from any given vertex of a graph visits only the reachable vertices from in that graph. Which of the following statements must always be true, regardless of the structure of ?
- A.If is a reachable vertex in the BFS of from , then is also a reachable vertex in the DFS of from .
- B.In , the BFS traversal from will visit exactly the same set of vertices as the DFS from in .
- C.The order of vertices visited in the BFS of from is the reverse of the order of vertices visited in the DFS of from .
- D.If is a reachable vertex in the DFS of from , then is also a reachable vertex in the BFS of from .✓
Open GATE DA 2026 Q40 · discuss this question →Answer: D
Solution
Key fact: a path in is exactly a path in . - (D) If is reachable from in , reversing that path gives in . So BFS of from reaches . Always true. - (A) and (B) Reachable from in means "can reach in ", which is not the same set. Counterexample: the single edge . In , reaches , but in , does not reach . - (C) Visiting orders have no such reversal relationship. Answer: (D). - A.
Take a Programming, Data Structures and Algorithms chapter-wise test
Pick any chapters and attempt their PYQs with a timer in the GATE interface, with instant analysis — free.
Create free account →