GATE CS Algorithms PYQ with Solutions
Also searched as: GATE Algo PYQ · GATE DAA PYQ · GATE Design and Analysis of Algorithms PYQ
All 52 Algorithms questions from every GATE Computer Science (CS/CSE) paper from 2021 to 2026, both sets where there were two, with the official answer key and a step-by-step solution. Algorithms carries about 8.3 marks per paper on average. Questions are grouped chapter-wise, newest paper first.
| 2026 | 2025 | 2024 | 2023 | 2022 | 2021 |
|---|---|---|---|---|---|
| 10 | 7.5 | 5.5 | 4 | 7 | 13 |
Algorithms: Graph Algorithms — GATE CS PYQ (23)
- GATE CS 2026 Set 1 · Q41Algorithmsmcq2 marksLet be an undirected, edge-weighted graph with integer weights. The weight of a path is the sum of the weights of the edges in that path. The length of a path is the number of edges in that path. Let be a vertex in . For every and for every , let denote the weight of a shortest path (in terms of weight) from to of length at most . If there is no path from to of length at most , then . Consider the statements: S1: For every and , . S2: For every , if is part of a shortest path (in terms of weight) from to , then for every , . Which one of the following options is correct?
- A.Only S1 is true✓
- B.Only S2 is true
- C.Both S1 and S2 are true
- D.Neither S1 nor S2 is true
Open GATE CS 2026 Set 1 Q41 · discuss this question →Answer: A
Solution
Answer: (A) Only S1 is true S1 is true: every path of length also has length , so allowing more edges can only keep or lower the minimum. (This is the Bellman-Ford invariant.) S2 is false. Counterexample: edges s–a (1), a–b (1), b–u (1), u–v (1), and s–v (10). The shortest path to is s-a-b-u-v with weight 4, so edge (u, v) lies on it. But with : and , so . - A.
- GATE CS 2026 Set 1 · Q49Algorithmsmsq2 marksLet be a simple, undirected, edge-weighted graph with unique edge weights. Which of the following statements about the minimum spanning trees (MST) of is/are true?
- A.In every cycle of , the edge with the largest weight in is not in any MST✓
- B.In every cycle of , the edge with the smallest weight in is in every MST
- C.For every vertex , the edge with the largest weight incident on is not in any MST
- D.For every vertex , the edge with the smallest weight incident on is in every MST✓
Open GATE CS 2026 Set 1 Q49 · discuss this question →Answer: A, D
Solution
Answer: A, D (with unique weights the MST is unique) • (A) True: this is the cycle property. The heaviest edge of any cycle can be swapped out for a lighter one. • (B) False: in triangle u-v-w with weights 5, 6, 7, add a path u-x-v with weights 1, 1. Edge u-v (5) is the lightest in its triangle but is not in the MST. • (C) False: for a leaf vertex, its only edge is both its heaviest and its lightest, and it must be in the MST. • (D) True: this is the cut property applied to the cut vs . - A.
- GATE CS 2026 Set 1 · Q50Algorithmsmsq2 marksConsider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph as input, where and are the discovery time and finishing time, respectively, of the vertex .
Suppose that the input directed graph is a directed acyclic graph (DAG). For an edge , which of the following options will NEVER be correct?DFS(G): Explore(G, v, t): unmark all v ∈ V mark v t ← 0 t ← t + 1 for each v ∈ V d[v] ← t if v is unmarked for each (v, w) ∈ E t ← Explore(G, v, t) if w is unmarked end if t ← Explore(G, w, t) end for end if end for t ← t + 1 f[v] ← t return t- A.
- B.✓
- C.
- D.✓
Open GATE CS 2026 Set 1 Q50 · discuss this question →Answer: B, D
Solution
Answer: B, D Key fact: in a DAG, every edge satisfies . This is why reverse finishing order is a topological sort. • (A) Tree or forward edge: possible. • (B) is nested inside , so is a back edge, which means a cycle. Never. • (C) Cross edge to an already finished : possible. • (D) The intervals partly overlap, which the parenthesis theorem rules out for any DFS. Never. - A.
- GATE CS 2026 Set 1 · Q55Algorithmsmsq2 marksAn undirected, unweighted, simple graph is said to be 2-colorable if there exists a function such that for every , . Which of the following statements about 2-colorable graphs is/are true?
- A.If is 2-colorable, then may contain cycles of odd length
- B.If is 2-colorable, then may contain cycles of even length✓
- C.An optimal algorithm for testing whether is 2-colorable runs in time , if is represented as an adjacency list✓
- D.An optimal algorithm for testing whether is 2-colorable runs in time , if is represented as an adjacency list
Open GATE CS 2026 Set 1 Q55 · discuss this question →Answer: B, C
Solution
Answer: B, C 2-colourable is the same as bipartite, which is the same as having no odd cycle. So (A) is false and (B) is true (for example ). BFS or DFS colouring checks every edge once, in time. That is optimal, because every edge must be read. So (C) is true and (D) false. - A.
- GATE CS 2026 Set 2 · Q36Algorithmsmcq2 marksConsider a complete graph with vertices (). Note that multiple spanning trees can be constructed over . Each of these spanning trees is represented as a set of edges. The Jaccard coefficient between any two sets is defined as the ratio of the size of the intersection of the two sets to the size of the union of the two sets. Which one of the following options gives the lowest possible value for the Jaccard coefficient between any two spanning trees of ?
- A.
- B.
- C.✓
- D.
Open GATE CS 2026 Set 2 Q36 · discuss this question →Answer: C
Solution
Answer: (C) Each spanning tree of has edges, and has edges. For , , so two edge-disjoint spanning trees exist. Their intersection is empty, giving a Jaccard coefficient of . - A.
- GATE CS 2026 Set 2 · Q37Algorithmsmcq2 marksLet be a weighted directed acyclic graph with edges and vertices. Given and a source vertex in , which one of the following options gives the worst case time complexity of the fastest algorithm to find the lengths of shortest paths from to all vertices that are reachable from in ?
- A.✓
- B.
- C.
- D.
Open GATE CS 2026 Set 2 Q37 · discuss this question →Answer: A
Solution
Answer: (A) On a DAG, relaxing edges in topological order computes all single-source shortest paths in one linear pass. Topological sort plus the relaxations are both , so the fastest algorithm runs in (Dijkstra/Bellman-Ford are not needed). - A.
- GATE CS 2025 Set 1 · Q18Algorithmsmcq1 markLet be any undirected graph with positive edge weights, and be a minimum spanning tree of . For any two vertices, and , let and be the shortest distances between and in and , respectively. Which ONE of the options is CORRECT for all possible and ?
- A.
- B.✓
- C.
- D.
Open GATE CS 2025 Set 1 Q18 · discuss this question →Answer: B
Solution
Answer: (B) is a subgraph of (it uses only edges of ). Any – path in is also a path in , so the shortest path in can only be shorter or equal:Equality can occur (e.g. a tree edge), and strict inequality is common (a direct non-tree edge in may be shorter), so (A), (C), (D) fail in general. - A.
- GATE CS 2025 Set 1 · Q43Algorithmsmcq2 marksLet be an undirected, unweighted graph with 100 vertices. Let denote the number of edges in a shortest path between and . Suppose (the diameter). Let be any breadth-first-search tree of . Which ONE of the options is CORRECT for every such graph ?
- A.The height of is exactly 15.
- B.The height of is exactly 30.
- C.The height of is at least 15.✓
- D.The height of is at least 30.
Open GATE CS 2025 Set 1 Q43 · discuss this question →Answer: C
Solution
Answer: (C) The height of is at least 15. A BFS tree rooted at has height equal to the eccentricity of (the maximum distance from to any vertex). For any root and the diametral pair with :So , i.e. the BFS-tree height is at least 15 for every root. It can be exactly 15 (root at the center) or as large as 30 (root at a diametral endpoint), so "exactly 15/30" and "at least 30" are not guaranteed. - A.
- GATE CS 2025 Set 1 · Q64Algorithmsnat2 marksThe maximum value of such that the edge between the nodes B and C is included in every minimum spanning tree of the given graph is _________. (answer in integer)
Open GATE CS 2025 Set 1 Q64 · discuss this question →Answer: 5
Solution
Answer: 5 Edge weights: , and the diagonals and are and . An edge is in every MST iff, in every cycle containing , some other edge is strictly heavier than (cut/cycle property). The binding cycle through is , whose other edges are with maximum weight . For to be forced into every MST we need , i.e. . Check : after adding the two light diagonals ( and ) the two components and must be joined by the cheapest crossing edge; the crossing edges are , and is the unique minimum → is in every MST. At , ties with and is no longer forced. Hence . - GATE CS 2025 Set 2 · Q29Algorithmsmsq1 markWhich of the following statements regarding BFS and DFS on an undirected simple graph is/are TRUE?
- A.A DFS tree of is a Shortest Path tree of .
- B.Every non-tree edge of with respect to a DFS tree is a forward/back edge.✓
- C.If is a non-tree edge of with respect to a BFS tree, then the distances from the source to and in the BFS tree are within of each other.✓
- D.Both BFS and DFS can be used to find the connected components of .✓
Open GATE CS 2025 Set 2 Q29 · discuss this question →Answer: B, C, D
Solution
Answer: (B), (C) and (D) - (A) A DFS tree is not generally a shortest-path tree. FALSE. - (B) On an undirected graph, DFS produces only tree edges and back edges (no forward/cross edges), so every non-tree edge is a back edge — consistent with "forward/back edge". TRUE. - (C) For BFS on an undirected graph, any non-tree edge connects vertices whose BFS levels differ by at most 1. TRUE. - (D) Running BFS/DFS from each unvisited vertex identifies all connected components. TRUE. - A.
- GATE CS 2025 Set 2 · Q37Algorithmsmcq2 marksLet be an edge-weighted undirected graph with positive weights. A positive constant is added to every edge weight. Which ONE is TRUE about MSTs and shortest paths (SPs) before and after?
- A.Every MST remains an MST, and every SP remains an SP.
- B.MSTs need not remain MSTs, and every SP remains an SP.
- C.Every MST remains an MST, and SPs need not remain SPs.✓
- D.MSTs need not remain MSTs, and SPs need not remain SPs.
Open GATE CS 2025 Set 2 Q37 · discuss this question →Answer: C
Solution
Answer: (C) Every MST remains an MST, and SPs need not remain SPs. Every spanning tree has exactly edges, so adding increases every spanning tree's weight by the same . The relative ordering is unchanged, so an MST stays an MST. Shortest paths can change: a path with more edges gains more total weight ( for edges), so a formerly-longer path with fewer edges may become shorter. Hence SPs need not be preserved. - A.
- GATE CS 2025 Set 2 · Q59Algorithmsnat2 marksConsider the algorithm someAlgo(G): (1) run BFS from any vertex , let be a farthest vertex; (2) run BFS from , let be a farthest vertex; (3) output the distance between and . For the tree shown in the figure, the output of someAlgo() is ___________. (Answer in integer)
Open GATE CS 2025 Set 2 Q59 · discuss this question →Answer: 6
Solution
Answer: 6 The double-BFS procedure is the standard algorithm for the diameter of a tree: the farthest vertex from any start is always an endpoint of a longest path, and BFS from reaches the other endpoint , so the returned distance equals the tree's diameter. Tracing the longest path in the given tree gives a diameter of edges, so the output is . - GATE CS 2024 Set 1 · Q45Algorithmsmsq2 marksis a directed graph, a DFS spanning tree rooted at that is also a BFS tree rooted at . For every such , which statements is/are TRUE?
- A.There are no back-edges in G wrt T
- B.There are no cross-edges in G wrt T
- C.There are no forward-edges in G wrt T✓
- D.The only edges in G are the edges in T
Open GATE CS 2024 Set 1 Q45 · discuss this question →Answer: C
Solution
Answer: (C) There are no forward-edges in G with respect to the tree T If one tree T is simultaneously a DFS tree and a BFS tree of a directed graph, it can have no forward edges: a forward edge (with a proper descendant more than one level below) would have been used by BFS to place directly one level under , contradicting the tree. Back edges and cross edges can still exist (e.g. when has a cycle), so (A) and (B) are not guaranteed; extra non-tree edges may exist, so (D) is false. - A.
- GATE CS 2024 Set 1 · Q60Algorithmsnat2 marksA DFS traversal of an undirected graph with 100 vertices produces a forest with 40 edges. The number of connected components in is _________Open GATE CS 2024 Set 1 Q60 · discuss this question →
Answer: 60
Solution
Answer: 60 A DFS forest with trees on vertices has edges (each tree on vertices contributes edges). Here . Each DFS tree corresponds to one connected component, so there are 60 components. - GATE CS 2024 Set 2 · Q59Algorithmsnat2 marksThe number of distinct minimum-weight spanning trees of the shown weighted graph (vertices a,b,c,d,e,f,g) is _________
Open GATE CS 2024 Set 2 Q59 · discuss this question →Answer: 9
Solution
Answer: 9 All four weight-1 edges () are forced into every MST (no cycle among them), connecting and , with isolated. The MST (weight 8) then needs 2 weight-2 edges to join the three parts , , :Counting the valid ways the weight-2 edges connect all three components without a cycle gives distinct MSTs.to g from {a,b,f}: a-g, b-g, f-g (3 choices) to join {c,d,e}: either g-c / g-d / g-e (to g side) - GATE CS 2022 · Q49Algorithmsmsq2 marksis a simple undirected weighted graph with all distinct edge weights. Which statements about its MSTs are TRUE?
- A.The edge with the second smallest weight is always part of any MST.✓
- B.One or both of the edges with the third and fourth smallest weights are part of any MST.✓
- C.The minimum-weight edge crossing any non-trivial cut (S, V\S) is always part of any MST.✓
- D.G can have multiple MSTs.
Open GATE CS 2022 Q49 · discuss this question →Answer: A, B, C
Solution
Answer: (A), (B) and (C) With all distinct weights the MST is unique, so (D) is false. By the cut property, the lightest edge across any cut is in the MST (C). The two globally smallest edges are always in the MST (they cannot form a cycle alone), so the second-smallest is included (A). Among the third and fourth smallest, at least one must join without forming a cycle, so (B) holds. - A.
- GATE CS 2022 · Q58Algorithmsnat2 marksDirected graph , , for . A directed spanning tree rooted at has a directed path from to every vertex. The number of such directed spanning trees rooted at vertex 5 is __________.Open GATE CS 2022 Q58 · discuss this question →
Answer: 24
Solution
Answer: 24 The adjacency matrix has for (a lower-triangular reachability). A directed spanning tree rooted at vertex 5 must have a path from 5 to every other vertex; counting the valid arborescences rooted at 5 in this DAG-like structure gives 24. - GATE CS 2021 Set 1 · Q27Algorithmsnat1 markConsider the undirected graph with edge weights as shown in the figure. The number of minimum-weight spanning trees of the graph is ______.
Open GATE CS 2021 Set 1 Q27 · discuss this question →Answer: 3
Solution
The minimum spanning tree has weight formed by the small (0.1) edges; counting the ways to complete an MST while breaking ties among equal-weight edges gives 3 distinct minimum-weight spanning trees. - GATE CS 2021 Set 1 · Q46Algorithmsmcq2 marksLet be an undirected unweighted connected graph. The *diameter* of is defined as:Let be the adjacency matrix of . Define graph on the same set of vertices with adjacency matrix , where if or (where ), and otherwise. Which one of the following statements is true?
- A.✓
- B.
- C.
- D.
Open GATE CS 2021 Set 1 Q46 · discuss this question →Answer: A
Solution
connects any two vertices within distance 2 in , so every edge in corresponds to a path of length in . Distances halve (with ceiling), giving . - A.
- GATE CS 2021 Set 1 · Q51Algorithmsmsq2 marksAn *articulation point* in a connected graph is a vertex such that removing the vertex and its incident edges disconnects the graph into two or more connected components. Let be a DFS tree obtained by doing DFS in a connected undirected graph . Which of the following options is/are correct?
- A.Root of can never be an articulation point in .
- B.Root of is an articulation point in if and only if it has 2 or more children.✓
- C.A leaf of can be an articulation point in .
- D.If is an articulation point in such that is an ancestor of in and is a descendent of in , then all paths from to in must pass through .✓
Open GATE CS 2021 Set 1 Q51 · discuss this question →Answer: B, D
Solution
The DFS-tree characterization: the root is an articulation point iff it has children (B true, A false). A leaf is never an articulation point (C false). The ancestor–descendant separation property (D) holds for any articulation point. The official key is (B), (D). - A.
- GATE CS 2021 Set 2 · Q11Algorithmsmcq1 markLet be a connected undirected weighted graph. Consider the following two statements. : There exists a minimum weight edge in which is present in every minimum spanning tree of . : If every edge in has distinct weight, then has a unique minimum spanning tree. Which one of the following options is correct?
- A.Both and are true
- B.is true and is false
- C.is false and is true✓
- D.Both and are false
Open GATE CS 2021 Set 2 Q11 · discuss this question →Answer: C
Solution
If several edges share the minimum weight (e.g. a triangle of equal weights), no single minimum-weight edge lies in every MST, so is false. Distinct edge weights force a unique MST, so is true. - A.
- GATE CS 2021 Set 2 · Q56Algorithmsmsq2 marksConsider the directed graph shown in the figure. Which of the following is/are correct about the graph?

- A.The graph does not have a topological order.✓
- B.A depth-first traversal starting at vertex classifies three directed edges as back edges.✓
- C.The graph does not have a strongly connected component.
- D.For each pair of vertices and , there is a directed path from to .
Open GATE CS 2021 Set 2 Q56 · discuss this question →Answer: A, B
Solution
The graph contains directed cycles, so it has no topological order (A true). A DFS from encounters three edges that go back to an ancestor, i.e. three back edges (B true). Every graph has strongly connected components (at least singletons), so (C) is false, and the graph is not strongly connected, so (D) is false. - A.
- GATE CS 2021 Set 2 · Q65Algorithmsnat2 marksIn a directed acyclic graph with a source vertex , the quality-score of a directed path is defined to be the product of the weights of the edges on the path. Further, for a vertex other than , the quality-score of is defined to be the maximum among the quality-scores of all the paths from to . The quality-score of is assumed to be 1. (See the figure for the weighted DAG.) The sum of the quality-scores of all the vertices in the graph shown is ______.
Open GATE CS 2021 Set 2 Q65 · discuss this question →Answer: 929
Solution
Processing the vertices in topological order and taking, for each vertex, the maximum product of edge weights over all paths from , then summing these best scores across all vertices, gives 929.
Algorithms: Algorithm Analysis — GATE CS PYQ (16)
- GATE CS 2026 Set 1 · Q17Algorithmsmcq1 markConsider the following recurrence relations: For all ,Assume that for all , and . Which one of the following options is correct?
- A.✓
- B.
- C.
- D.
Open GATE CS 2026 Set 1 Q17 · discuss this question →Answer: A
Solution
Answer: (A) Step 1, solve : , so . This dominates polynomially (Master theorem case 1), giving . Step 2, solve : , so . The extra term is polynomially smaller than (case 1 again), giving . - A.
- GATE CS 2026 Set 2 · Q24Algorithmsmcq1 markConsider the following functions, where is a positive integer.Which one of the following options lists the functions in increasing order of asymptotic growth rate? Note: Assume the base of log to be 2.
- A.✓
- B.
- C.
- D.
Open GATE CS 2026 Set 2 Q24 · discuss this question →Answer: A
Solution
Answer: (A) With base-2 logs, and . Ordering by growth:This is option (A). - A.
- GATE CS 2026 Set 2 · Q25Algorithmsmsq1 markWhich of the following can be recurrence relation(s) corresponding to an algorithm with time complexity ?
- A.✓
- B.✓
- C.
- D.
Open GATE CS 2026 Set 2 Q25 · discuss this question →Answer: A, B
Solution
Answer: (A), (B) (A) . ✓ (B) : by Master theorem dominates . ✓ (C) . ✗ (D) . ✗ - A.
- GATE CS 2026 Set 2 · Q38Algorithmsmcq2 marksConsider an array of integers of size . The indices of run from 1 to . An algorithm is to be designed to check whether satisfies the condition given below.Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?
- A.✓
- B.
- C.
- D.
Open GATE CS 2026 Set 2 Q38 · discuss this question →Answer: A
Solution
Answer: (A) Let be the consecutive differences. The condition says the sequence is strictly increasing. This can be verified by a single linear scan comparing each with , i.e. in . - A.
- GATE CS 2025 Set 1 · Q20Algorithmsmcq1 markConsider the following recurrence relation:Which ONE of the following options is CORRECT?
- A.✓
- B.
- C.
- D.
Open GATE CS 2025 Set 1 Q20 · discuss this question →Answer: A
Solution
Answer: (A) Divide by . Let :With , . Therefore - A.
- GATE CS 2025 Set 2 · Q20Algorithmsmcq1 markConsider an unordered list of distinct integers. What is the minimum number of element comparisons required to find an integer in the list that is NOT the largest in the list?
- A.1✓
- B.
- C.
- D.
Open GATE CS 2025 Set 2 Q20 · discuss this question →Answer: A
Solution
Answer: (A) 1 Compare any two elements once. The smaller of the two is certainly not the largest element of the whole list. So a single comparison suffices. - A.
- GATE CS 2024 Set 1 · Q42Algorithmsmcq2 marksfor , . Which is CORRECT?
- A.✓
- B.
- C.
- D.
Open GATE CS 2024 Set 1 Q42 · discuss this question →Answer: A
Solution
Answer: (A) Let . Dividing by : , i.e. . The variable square-roots down to the base in steps, so and . - A.
- GATE CS 2024 Set 2 · Q15Algorithmsmcq1 markfor . Which is TRUE?
- A.✓
- B.
- C.
- D.
Open GATE CS 2024 Set 2 Q15 · discuss this question →Answer: A
Solution
Answer: (A) Characteristic equation . General solution . Using and : solving gives , so . (The term vanishes.) - A.
- GATE CS 2023 · Q29Algorithmsmsq1 markand . Which statements are TRUE?
- A.✓
- B.
- C.✓
- D.
Open GATE CS 2023 Q29 · discuss this question →Answer: A, C
Solution
Answer: (A) and (C) so (A). so (C), which also rules out (B) and (D). - A.
- GATE CS 2023 · Q54Algorithmsmsq2 marksFunction 1:
while n>1: for i=1..n: x++; n=floor(n/2). Function 2:for i=1..100*n: x++. count the x++ executions. Which statements are TRUE?- A.✓
- B.
- C.
- D.✓
Open GATE CS 2023 Q54 · discuss this question →Answer: A, D
Solution
Answer: (A) and (D) . . Since both are : (A) and (D). They are the same order, so (B) and (C) are false. - A.
- GATE CS 2022 · Q11Algorithmsmcq1 markWhich one of the following statements is TRUE for all positive functions ?
- A., when is a polynomial✓
- B.
- C., when is an exponential function
- D.
Open GATE CS 2022 Q11 · discuss this question →Answer: A
Solution
Answer: (A) For a polynomial : and - equal, so . TRUE. (C) fails for exponentials: if then grows far faster than , so . (B) and (D) are false in general. - A.
- GATE CS 2022 · Q51Algorithmsmsq2 marksThe recurrence , for , for . Which statements are TRUE?
- A.✓
- B.✓
- C.✓
- D.
Open GATE CS 2022 Q51 · discuss this question →Answer: A, B, C
Solution
Answer: (A), (B) and (C) Computing values: - (A) : TRUE. - (B) : . TRUE. - (C) : , TRUE. - (D) : but , so FALSE. Hence (A), (B), (C). - A.
- GATE CS 2021 Set 1 · Q12Algorithmsmcq1 markLet P be an array containing integers. Let be the lowest upper bound on the number of comparisons of the array elements, required to find the minimum and maximum values in an arbitrary array of elements. Which one of the following choices is correct?
- A.
- B.and
- C.and✓
- D.and
Open GATE CS 2021 Set 1 Q12 · discuss this question →Answer: C
Solution
The tournament (pair-wise) method finds both min and max using at most comparisons, and more than are needed, so and . - A.
- GATE CS 2021 Set 1 · Q13Algorithmsmcq1 markConsider the following three functions.Which one of the following options arranges the functions in the increasing order of asymptotic growth rate?
- A.
- B.
- C.
- D.✓
Open GATE CS 2021 Set 1 Q13 · discuss this question →Answer: D
Solution
Take logs: , , . Order of these: , so . - A.
- GATE CS 2021 Set 1 · Q40Algorithmsmcq2 marksConsider the following recurrence relation.Which one of the following options is correct?
- A.
- B.
- C.✓
- D.
Open GATE CS 2021 Set 1 Q40 · discuss this question →Answer: C
Solution
The subproblem sizes sum to . With linear work per level and a geometrically shrinking total, the work is dominated by the top level, giving . - A.
- GATE CS 2021 Set 2 · Q49Algorithmsmcq2 marksFor constants and , consider the following recurrence defined on the non-negative integers:Which one of the following options is correct about the recurrence ?
- A.If is , then is .
- B.If is , then is .
- C.If is for some , then is .✓
- D.If is , then is .
Open GATE CS 2021 Set 2 Q49 · discuss this question →Answer: C
Solution
This is Master theorem Case 1: if then — option (C). (Case 2 with gives an extra factor, so (D) is wrong.) - A.
Algorithms: Sorting & Searching — GATE CS PYQ (6)
- GATE CS 2026 Set 2 · Q32Algorithmsnat1 markConsider an array . Suppose the merge sort algorithm is executed on array to sort it in increasing order. The merge sort algorithm will carry out a total of 7 merge operations. A merge operation on sorted left array and sorted right array is said to be void if the output of the merge operation is the elements of array followed by the elements of array . The number of void merge operations among these 7 merge operations is __________. (answer in integer)Open GATE CS 2026 Set 2 Q32 · discuss this question →
Answer: 3
Solution
A merge is void when . The 7 merges are:merge(10,7)→no;merge(8,19)→void;merge([7,10],[8,19])→no (10>8);merge(41,35)→no;merge(25,31)→void;merge([35,41],[25,31])→no; finalmerge([7,8,10,19],[25,31,35,41])→void (19≤25). Void merges . - GATE CS 2025 Set 1 · Q33Algorithmsnat1 markThe pseudocode of a function fun() is given below:
Let be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if fun() is called with , is __________. (Answer in integer)fun(int A[0..n-1]){ for i=0 to n-2 for j=0 to n-i-2 if (A[j] > A[j+1]) then swap A[j] and A[j+1] }Open GATE CS 2025 Set 1 Q33 · discuss this question →Answer: 435
Solution
Answer: 435funis bubble sort. For an array sorted in strictly descending order (the worst case), every compared adjacent pair is out of order, so each comparison causes a swap. The number of comparisons (and hence swaps) is - GATE CS 2025 Set 2 · Q41Algorithmsmcq2 marksAn array of length with distinct elements is bitonic if is non-decreasing and is non-increasing for some . Which ONE is the best asymptotic bound for the worst-case number of comparisons to search for an element in a bitonic array?
- A.
- B.
- C.
- D.✓
Open GATE CS 2025 Set 2 Q41 · discuss this question →Answer: D
Solution
Answer: (D) First find the peak index with binary search in . Then binary search the increasing part and the decreasing part, each . Total comparisons in the worst case. - A.
- GATE CS 2024 Set 1 · Q17Algorithmsmcq1 markAn algorithm checks if an array of size is sorted (ascending or descending) by a single pass comparing each element only with its adjacent elements. The worst-case time complexity is
- A.both and✓
- B.but not
- C.but not
- D.neither nor
Open GATE CS 2024 Set 1 Q17 · discuss this question →Answer: A
Solution
Answer: (A) both and A single pass does up to comparisons, so it is . In the worst case it must examine essentially all elements (e.g. an array that is sorted except the last pair), so it is also . Thus the worst-case time is — both and . - A.
- GATE CS 2021 Set 1 · Q19Algorithmsmcq1 markConsider the following array.
Which algorithm out of the following options uses the least number of comparisons (among the array elements) to sort the above array in ascending order?23 32 45 69 72 73 89 97- A.Selection sort
- B.Mergesort
- C.Insertion sort✓
- D.Quicksort using the last element as pivot
Open GATE CS 2021 Set 1 Q19 · discuss this question →Answer: C
Solution
The array is already sorted. Insertion sort on an already-sorted array performs only comparisons (best case), fewer than the others. - A.
- GATE CS 2021 Set 2 · Q18Algorithmsmcq1 markConsider a standard recursive binary search on a sorted array of elements. In the worst case, the number of comparison operations performed is:
- A.
- B.✓
- C.
- D.
Open GATE CS 2021 Set 2 Q18 · discuss this question →Answer: B
Solution
Binary search halves the search range each step, performing operations in the worst case. - A.
Algorithms: Algorithm Design Techniques — GATE CS PYQ (5)
- GATE CS 2026 Set 2 · Q39Algorithmsmcq2 marksConsider a table , where the elements , , represent the cost of the optimal solutions of different subproblems. The recursive formulation is:All are initialized to 1. Consider the two algorithms below.
is correct iff it computes the correct for all . Which one of the following statements is true?Algorithm B1: For i = 1,2,...,n For j = 1,2,...,n T[i][j] = 2*T[i-1][j] + 3*T[i][j-1] Algorithm B2: For s = 2,3,...,2n For i = 1,2,...,n For j = 1,2,...,n If (i+j == s) T[i][j] = 2*T[i-1][j] + 3*T[i][j-1]- A.Both algorithms and are correct✓
- B.Algorithm is correct, but algorithm is incorrect
- C.Algorithm is correct, but algorithm is incorrect
- D.Both algorithms and are incorrect
Open GATE CS 2026 Set 2 Q39 · discuss this question →Answer: A
Solution
Answer: (A) Each depends only on and (both have smaller index-sum ). - B1 (row-major): when computing , was done in the previous row and earlier in the same row. ✓ - B2 (by diagonals ): both dependencies have sum , computed in earlier diagonals. ✓ Both are correct. - A.
- GATE CS 2024 Set 2 · Q35Algorithmsnat1 markDistance of array = minimum number of elements to replace so becomes non-decreasing. The distance of [2,5,3,1,4,2,6] is __________Open GATE CS 2024 Set 2 Q35 · discuss this question →
Answer: 3
Solution
Answer: 3 Elements that can stay form a longest non-decreasing subsequence (LNDS); the rest must be replaced. For [2,5,3,1,4,2,6], an LNDS is (length 4). So replacements . - GATE CS 2021 Set 1 · Q50Algorithmsmsq2 marksDefine to be the maximum amount earned by cutting a rod of length meters into one or more pieces of integer length and selling them. For , let denote the selling price of a rod whose length is meters. Consider the array of prices:Which of the following statements is/are correct about ?
- A.✓
- B.
- C.is achieved by three different solutions.✓
- D.cannot be achieved by a solution consisting of three pieces.
Open GATE CS 2021 Set 1 Q50 · discuss this question →Answer: A, C
Solution
The optimal revenue is , achievable as , or (5+10=... ) — in fact three distinct cuts give 18: , , . So (A) and it is achieved by three different solutions (C). - A.
- GATE CS 2021 Set 2 · Q36Algorithmsmcq2 marksConsider the string
abbccddeee. Each letter in the string must be assigned a binary code satisfying the following properties: 1. For any two letters, the code assigned to one letter must not be a prefix of the code assigned to the other letter. 2. For any two letters of the same frequency, the letter which occurs earlier in the dictionary order is assigned a code whose length is at most the length of the code assigned to the other letter. Among the set of all binary code assignments which satisfy the above two properties, what is the minimum length of the encoded string?- A.21
- B.23✓
- C.25
- D.30
Open GATE CS 2021 Set 2 Q36 · discuss this question →Answer: B
Solution
Frequencies: . Building an optimal prefix (Huffman) code and summing (frequency code length) gives the minimum total length of 23 bits. - A.
- GATE CS 2021 Set 2 · Q39Algorithmsmcq2 marksIn an examination, a student can choose the order in which two questions (QuesA and QuesB) must be attempted. - If the first question is answered wrong, the student gets zero marks. - If the first question is answered correctly and the second question is not answered correctly, the student gets the marks only for the first question. - If both the questions are answered correctly, the student gets the sum of the marks of the two questions. The following table shows the probability of correctly answering a question and the marks of the question respectively.
Assuming that the student always wants to maximize her expected marks, in which order should she attempt the questions and what is the expected marks for that order (assume that the questions are independent)?question probability of answering correctly marks QuesA 0.8 10 QuesB 0.5 20- A.First QuesA and then QuesB. Expected marks 14.
- B.First QuesB and then QuesA. Expected marks 14.
- C.First QuesB and then QuesA. Expected marks 22.
- D.First QuesA and then QuesB. Expected marks 16.✓
Open GATE CS 2021 Set 2 Q39 · discuss this question →Answer: D
Solution
Order A then B: . Order B then A: . The larger is 16, attempting QuesA first. - A.
Algorithms: Hashing — GATE CS PYQ (2)
- GATE CS 2023 · Q20Algorithmsmcq1 markAn adversary generates keys to maximize collisions in a hash table ( keys, slots, ). Which is the best hashing strategy?
- A.Division method h(k)=k mod m.
- B.Multiplication method.
- C.Universal hashing method.✓
- D.If k is prime use Division, else Multiplication.
Open GATE CS 2023 Q20 · discuss this question →Answer: C
Solution
Answer: (C) Universal hashing method For any fixed hash function (division or multiplication), an adversary who knows it can choose keys that all collide. Universal hashing picks the hash function randomly from a family, so no adversary (choosing keys before the function is drawn) can force many collisions — the expected number of collisions is bounded. - A.
- GATE CS 2021 Set 1 · Q57Algorithmsmsq2 marksConsider a *dynamic* hashing approach for 4-bit integer keys: 1. There is a main hash table of size 4. 2. The 2 least significant bits of a key is used to index into the main hash table. 3. Initially, the main hash table entries are empty. 4. Thereafter, when more keys are hashed into it, to resolve collisions, the set of all keys corresponding to a main hash table entry is organized as a binary tree that grows on demand. 5. First, the 3rd least significant bit is used to divide the keys into left and right subtrees. 6. To resolve more collisions, each node of the binary tree is further sub-divided into left and right subtrees based on the 4th least significant bit. 7. A split is done only if it is needed, i.e., only when there is a collision. Consider the state of the hash table shown in the figure. Which of the following sequences of key insertions can cause the above state of the hash table (assume the keys are in decimal notation)?

- A.5, 9, 4, 13, 10, 7
- B.9, 5, 10, 6, 7, 1
- C.10, 9, 6, 7, 5, 13✓
- D.9, 5, 13, 6, 10, 14
Open GATE CS 2021 Set 1 Q57 · discuss this question →Answer: C
Solution
Tracing each insertion sequence through the index (2 LSBs) and the on-demand splits (3rd then 4th LSB), only sequence (C) 10, 9, 6, 7, 5, 13 reproduces exactly the shown tree structure, with splits occurring only where collisions force them. - A.
Take a 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 →