GATE CS Algorithms Previous Year Questions

60 solved GATE CS questions on Algorithms, drawn from 10 exam years and grouped by year. Every question shows the official answer and a step-by-step solution.

Want to practise this topic and ask follow-up questions? Explore Success Tracker.

Revision companion

Algorithms: choose the invariant before the formula

Identify the input model, required output, and property that makes an algorithm correct before calculating time or space. These selected revision notes connect asymptotic analysis, graph traversal, and design techniques. Reproduce the small examples by hand, explaining each decision before moving to larger practice problems.

Our study notes and original examples support the PYQs below; they are not official exam questions or a replacement for the current syllabus.

Before you start

  • Logarithms, summations, induction, and probability for expected bounds.
  • Arrays, queues, heaps, recursion, and adjacency-list graph representations.

Concepts to revise before solving

Bounds and input models

O denotes an asymptotic upper bound, Ω a lower bound, and Θ a tight bound. These do not inherently mean worst, best, or average case. Define the cost function and input size; graphs often require both vertex count V and edge count E.

Check yourself: Are you bounding worst-case cost or an expectation?

Recurrences and recursion trees

Separate recursive work from work outside recursive calls. In T(n) = 2T(n/2) + n for powers of two, each internal level contributes n, across log₂ n levels. Include leaf costs for an exact expression, not just a growth rate.

Check yourself: Does the expression satisfy the recurrence and base case?

BFS and Dijkstra

BFS uses a FIFO queue to discover vertices by edge-count distance. Dijkstra selects the smallest tentative weighted distance; its standard correctness argument requires nonnegative weights. With adjacency lists, BFS takes O(V + E), while binary-heap Dijkstra takes O((V + E) log V).

Check yourself: Are you minimizing hops or weight, and are negative weights possible?

Greedy choices need proof

Justify greedy choices through an exchange argument, cut property, or invariant showing an optimal solution can include them. For maximizing the number of nonoverlapping intervals, earliest finish time leaves room for later intervals; earliest start time need not maximize the count.

Check yourself: Can your choice replace one in an optimal solution without worsening it?

Dynamic-programming states

Define each state's meaning, base cases, and dependency order before writing transitions. For 0/1 knapsack with a one-dimensional capacity array, descending capacity prevents reuse of the current item. Ascending capacity would permit repeated use within the same item pass.

Check yourself: Could the state you read already include the current item?

Mistakes to avoid

Using BFS for arbitrary positive weighted paths.
BFS minimizes hops. Equal positive weights make this equivalent to minimizing weight; unequal weights do not.
Finalizing Dijkstra distances at first discovery.
For nonnegative weights, finalize at selection with minimum tentative distance; earlier estimates can improve.
Applying a recurrence formula without checking its form.
Identify subproblem sizes, branching, nonrecursive work, and base cases. Expand when a standard theorem does not apply.

Original teaching example · not a PYQ

Work through the reasoning

Original mini-example: a directed graph has vertices S, A, T and exactly these edges: S → T (weight 7), S → A (weight 2), A → T (weight 3). Compare BFS's hop-count path from S to T with Dijkstra's weighted shortest path.

  1. BFS discovers A and T one edge from S. Its path to T is the direct edge, with weight 7.
  2. Dijkstra initializes S at 0 and others at infinity. Relaxing S's edges sets A to 2 and T to 7.
  3. Select A next, the smallest unsettled distance. Relaxing A → T improves T to 2 + 3 = 5.
  4. Select T at distance 5. S → A → T is lighter despite using more hops; the graph has only two S-to-T paths.

BFS: one hop, weight 7. Dijkstra: weight 5 via A.

Try it before reading the answer

For n a positive power of two (including 1), solve T(1) = 1 and T(n) = 2T(n/2) + n for n > 1 exactly. Find T(8).

Show answer and reasoning

T(n) = n log₂ n + n; T(8) = 32.

There are log₂ n internal levels contributing n each, plus n unit-cost leaves. For n = 8, this totals 24 + 8 = 32. The expression also returns 1 at the base case.

Go deeper with free learning resources

Supplemental reading, not an official GATE reading list or an endorsement of these notes.

Apply this to the previous-year questions

Previous-year questions by year

This page shows 60 recent questions from the released archive, newest first. For older questions and complete papers, browse all GATE CS papers. Questions can carry more than one subject tag; counts are not marks weightage.

GATE CS 202610 questions

  1. Set 1 Q17Consider the following recurrence relations: For all n>1n > 1, T1(n)=4T1(n2)+T2(n)T_1(n) = 4T_1\left(\frac{n}{2}\right) + T_2(n)…MCQ · +1 marks · Medium
  2. Set 1 Q41Let G(V, E) 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…MCQ · +2 marks · Hard
  3. Set 1 Q49Let G(V, E) be a simple, undirected, edge-weighted graph with unique edge weights. Which of the following statements about the minimum spanning trees (MST)…MSQ · +2 marks · Medium
  4. Set 1 Q50Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph G(V, E) as input, where d[v]d[v] and f[v]f[v] are the…MSQ · +2 marks · Hard
  5. Set 2 Q24Consider the following functions, where nn is a positive integer. n1/3,log⁡(n),log⁡(n!),2log⁡(n)n^{1/3}, \log(n), \log(n!), 2^{\log(n)} Which one of the following options lists the…MCQ · +1 marks · Easy
  6. Set 2 Q25Which of the following can be recurrence relation(s) corresponding to an algorithm with time complexity Θ(n)\Theta(n)?MSQ · +1 marks · Medium
  7. Set 2 Q32Consider an array A=[10,7,8,19,41,35,25,31]A = [10, 7, 8, 19, 41, 35, 25, 31]. Suppose the merge sort algorithm is executed on array AA to sort it in increasing order. The merge…NAT · +1 marks · Medium
  8. Set 2 Q37Let GG be a weighted directed acyclic graph with mm edges and nn vertices. Given GG and a source vertex ss in GG, which one of the following options…MCQ · +2 marks · Easy
  9. Set 2 Q38Consider an array AA of integers of size nn. The indices of AA run from 1 to nn. An algorithm is to be designed to check whether AA satisfies the…MCQ · +2 marks · Medium
  10. Set 2 Q39Consider a table TT, where the elements T[i][j],0≤i,j≤nT[i][j], 0 \le i, j \le n, represent the cost of the optimal solutions of different subproblems of a problem that is…MCQ · +2 marks · Medium

GATE CS 20258 questions

  1. Set 1 Q20Consider the following recurrence relation: T(n)=2T(n−1)+n2n for n>0,T(0)=1.T(n) = 2T(n - 1) + n 2^n \text{ for } n > 0, T(0) = 1. Which ONE of the following options is CORRECT?MCQ · +1 marks · Medium
  2. Set 1 Q33The pseudocode of a function fun()fun() is given below: [code] Let A[0,…,29]A[0, \dots, 29] be an array storing 3030 distinct integers in descending order. The number of…NAT · +1 marks · Medium
  3. Set 1 Q43Let G(V, E) be an undirected and unweighted graph with 100 vertices. Let d(u,v)d(u, v) denote the number of edges in a shortest path between vertices uu and vv…MCQ · +2 marks · Medium
  4. Set 1 Q64The maximum value of xx such that the edge between the nodes B and C is included in every minimum spanning tree of the given graph is _________ . (answer in…NAT · +2 marks · Medium
  5. Set 2 Q20Consider an unordered list of NN distinct integers. What is the minimum number of element comparisons required to find an integer in the list that is NOT the…MCQ · +1 marks · Easy
  6. Set 2 Q29Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph GG is/are TRUE?MSQ · +1 marks · Medium
  7. Set 2 Q37Let GG be an edge-weighted undirected graph with positive edge weights. Suppose a positive constant α\alpha is added to the weight of every edge. Which ONE…MCQ · +2 marks · Medium
  8. Set 2 Q41An array AA of length nn with distinct elements is said to be bitonic if there is an index 1≤i≤n1 \le i \le n such that A[1..i]A[1..i] is sorted in the…MCQ · +2 marks · Medium

GATE CS 20245 questions

  1. Set 1 Q17Given an integer array of size NN, we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by…MCQ · +1 marks · Easy
  2. Set 1 Q42Consider the following recurrence relation: T(n)={nT(n)+nfor n≥1,1for n=1.T(n) = \begin{cases} \sqrt{n}T(\sqrt{n}) + n & \text{for } n \ge 1, \\ 1 & \text{for } n = 1. \end{cases} Which…MCQ · +2 marks · Medium
  3. Set 1 Q45Let GG be a directed graph and TT a depth first search (DFS) spanning tree in GG that is rooted at a vertex vv. Suppose TT is also a breadth first search…MSQ · +2 marks · Hard
  4. Set 2 Q35Let AA be an array containing integer values. The distance of AA is defined as the minimum number of elements in AA that must be replaced with another…NAT · +1 marks · Medium
  5. Set 2 Q59The number of distinct minimum-weight spanning trees of the following graph is _________ [figure]NAT · +2 marks · Hard

GATE CS 20232 questions

  1. Set 1 Q29Let ff and gg be functions of natural numbers given by f(n)=nf(n) = n and g(n)=n2g(n) = n^2. Which of the following statements is/are TRUE?MSQ · +1 marks · Easy
  2. Set 1 Q54Consider functions Function_1 and Function_2 expressed in pseudocode as follows: Function 1 [code] Function 2 [code] Let f1(n)f_1(n) and f2(n)f_2(n)…MSQ · +2 marks · Medium

GATE CS 20222 questions

  1. Set 1 Q11Which one of the following statements is TRUE for all positive functions f(n)f(n)?MCQ · +1 marks · Medium
  2. Set 1 Q49Consider a simple undirected weighted graph GG, all of whose edge weights are distinct. Which of the following statements about the minimum spanning trees of…MSQ · +2 marks · Medium

GATE CS 202113 questions

  1. Set 1 Q12Let PP be an array containing nn integers. Let tt be the lowest upper bound on the number of comparisons of the array elements, required to find the minimum…MCQ · +1 marks · Medium
  2. Set 1 Q13Consider the following three functions. f1=10nf2=nlog⁡nf3=nnf_1 = 10^n \quad f_2 = n^{\log n} \quad f_3 = n^{\sqrt{n}} Which one of the following options arranges the…MCQ · +1 marks · Medium
  3. Set 1 Q19Consider the following array. | 23 | 32 | 45 | 69 | 72 | 73 | 89 | 97 | |----|----|----|----|----|----|----|----| Which algorithm out of the following options…MCQ · +1 marks · Medium
  4. Set 1 Q27Consider the following undirected graph with edge weights as shown: [figure] The number of minimum-weight spanning trees of the graph is _______.NAT · +1 marks · Medium
  5. Set 1 Q40Consider the following recurrence relation. T(n)={T(n/2)+T(2n/5)+7nif n>01if n=0T(n) = \begin{cases} T(n/2) + T(2n/5) + 7n & \text{if } n > 0 \\ 1 & \text{if } n = 0 \end{cases} Which one of…MCQ · +2 marks · Medium
  6. Set 1 Q50Define RnR_n to be the maximum amount earned by cutting a rod of length nn meters into one or more pieces of integer length and selling them. For i>0i > 0, let…MSQ · +2 marks · Medium
  7. Set 1 Q51An 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…MSQ · +2 marks · Medium
  8. Set 2 Q11Let GG be a connected undirected weighted graph. Consider the following two statements. S1S_1: There exists a minimum weight edge in GG which is present in…MCQ · +1 marks · Medium
  9. Set 2 Q18What is the worst-case number of arithmetic operations performed by recursive binary search on a sorted array of size nn?MCQ · +1 marks · Easy
  10. Set 2 Q36Consider the string abbccddeee. Each letter in the string must be assigned a binary code satisfying the following properties: 1. For any two letters, the…MCQ · +2 marks · Medium
  11. Set 2 Q49For constants a≥1a \ge 1 and b>1b > 1, consider the following recurrence defined on the non-negative integers:…MCQ · +2 marks · Medium
  12. Set 2 Q56Consider the following directed graph: [figure] Which of the following is/are correct about the graph?MSQ · +2 marks · Hard
  13. Set 2 Q65In a directed acyclic graph with a source vertex ss, the quality-score of a directed path is defined to be the product of the weights of the edges on the…NAT · +2 marks · Medium

GATE CS 20204 questions

  1. Set 1 Q12For parameters aa and bb, both of which are ω(1)\omega(1), T(n)=T(n1/a)+1T(n) = T(n^{1/a}) + 1, and T(b)=1T(b) = 1. Then T(n)T(n) isMCQ · +1 marks · Hard
  2. Set 1 Q26What is the worst case time complexity of inserting nn elements into an empty linked list, if the linked list needs to be maintained in sorted order?MCQ · +1 marks · Easy
  3. Set 1 Q41Let G=(V,E)G = (V, E) be a weighted undirected graph and let TT be a Minimum Spanning Tree (MST) of GG maintained using adjacency lists. Suppose a new weighted…MCQ · +2 marks · Medium
  4. Set 1 Q50Let G=(V,E)G = (V, E) be a directed, weighted graph with weight function w:E→Rw: E \to \mathbb{R}. For some function f:V→Rf: V \to \mathbb{R}, for each edge…MCQ · +2 marks · Medium

GATE CS 20194 questions

  1. Set 1 Q30An array of 25 distinct elements is to be sorted using quicksort. Assume that the pivot element is chosen uniformly at random. The probability that the pivot…NAT · +1 marks · Medium
  2. Set 1 Q35Consider a sequence of 14 elements: A=[−5,−10,6,3,−1,−2,13,4,−9,−1,4,12,−3,0]A = [-5, -10, 6, 3, -1, -2, 13, 4, -9, -1, 4, 12, -3, 0]. The subsequence sum S(i,j)=∑k=ijA[k]S(i, j) = \sum_{k=i}^{j} A[k]. Determine…NAT · +1 marks · Medium
  3. Set 1 Q47There are nn unsorted arrays: A1,A2,…,AnA_1, A_2, \dots, A_n. Assume that nn is odd. Each of A1,A2,…,AnA_1, A_2, \dots, A_n contains nn distinct elements. There are no…MCQ · +2 marks · Medium
  4. Set 1 Q48Let GG be any connected, weighted, undirected graph. I. GG has a unique minimum spanning tree, if no two edges of GG have the same weight. II. GG has a…MCQ · +2 marks · Medium

GATE CS 20184 questions

  1. Set 1 Q40Let G be a simple undirected graph. Let TDT_D be a depth first search tree of G. Let TBT_B be a breadth first search tree of G. Consider the following…MCQ · +2 marks · Medium
  2. Set 1 Q41Assume that multiplying a matrix G1G_1 of dimension p×qp \times q with another matrix G2G_2 of dimension q×rq \times r requires pqrpqr scalar multiplications.…MCQ · +2 marks · Medium
  3. Set 1 Q57Consider the following undirected graph G: [figure] Choose a value for xx that will maximize the number of minimum weight spanning trees (MWSTs) of G. The…NAT · +2 marks · Hard
  4. Set 1 Q58Consider the weights and values of items listed below. Note that there is only one unit of each item. | Item number | Weight (in Kgs) | Value (in Rupees) |…NAT · +2 marks · Medium

GATE CS 20178 questions

  1. Set 1 Q4Consider the following functions from positive integers to real numbers: 10,n,n,log⁡2n,100n10, \sqrt{n}, n, \log_2 n, \frac{100}{n} The CORRECT arrangement of the above…MCQ · +1 marks · Easy
  2. Set 1 Q5Consider the following table: | Algorithms | Design Paradigms | |---|---| | (P) Kruskal | (i) Divide and Conquer | | (Q) Quicksort | (ii) Greedy | | (R)…MCQ · +1 marks · Easy
  3. Set 1 Q26Let G=(V,E)G = (V, E) be any connected undirected edge-weighted graph. The weights of the edges in EE are positive and distinct. Consider the following statements:…MCQ · +2 marks · Medium
  4. Set 1 Q48Let AA be an array of 31 numbers consisting of a sequence of 0's followed by a sequence of 1's. The problem is to find the smallest index ii such that A[i]A[i]…NAT · +2 marks · Medium
  5. Set 2 Q3Match the algorithms with their time complexities: | Algorithm | Time complexity | |---|---| | (P) Towers of Hanoi with nn disks | (i) Θ(n2)\Theta(n^2) | | (Q)…MCQ · +1 marks · Easy
  6. Set 2 Q30Consider the recurrence function T(n)={2T(n)+1,n>22,0<n≤2T(n) = \begin{cases} 2T(\sqrt{n}) + 1, & n > 2 \\ 2, & 0 < n \leq 2 \end{cases} Then T(n)T(n) in terms of Θ\Theta notation…MCQ · +2 marks · Medium
  7. Set 2 Q38Consider the following C function. [code] Time complexity of fun in terms of Θ\Theta notation isMCQ · +2 marks · Medium
  8. Set 2 Q43Consider the following snippet of a C program. Assume that swap(&x, &y) exchanges the contents of x and y. [code] The output of the program is ________.NAT · +2 marks · Medium

Other GATE CS topics

Continue learning with Success Tracker

Keep working on Algorithms

Reading a solution is a useful start. In Success Tracker, you can attempt questions yourself, review mistakes and return to the topics that need another pass.

AI-powered practice· Unlimited practice on eligible plans
PYQs with solutions
Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
Practice that adapts
Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
AI doubt support
Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.

Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.

This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.