Skip to content

GATE CS Algorithms questions

Practise Algorithms questions for GATE CS. Explore official PYQs and original subject-wise quizzes, check answers and view available solutions. Free, no sign-in.

Compare greedy choices, dynamic-programming states and graph algorithms. Identify the input size before analysing the time and space complexity of each solution.

Subject, topic, year and type filter the full library. Match any choice within a filter, and all selected filter categories. Search, source and sort apply to this page.

20 on this page
Official PYQ2011AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
Consider the following recursive C function that takes two arguments. unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return 0; } What is the return value of the function foo\text{foo} when it is called as foo(513, 2)\text{foo(513, 2)}?
Official PYQ2011AlgorithmsMinimum Spanning TreesMCQ · 1 mark
An undirected graph G(V,E)G(V,E) contains n (n>2)n \: (n>2) nodes named v1,v2,…,vnv_1,v_2, \dots, v_n. Two nodes vi,vjv_i, v_j are connected if and only if 0<∣i−j∣≤20 < \mid i-j\mid \leq 2. Each edge (vi,vj)(v_i,v_j) is assigned a weight i+ji+j. A sample graph with n=4n=4 is shown below. What will be the cost of the minimum spanning tree (MST) of such a graph with nn …
Official PYQ2011AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
Consider the following recursive C function that takes two arguments. unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return 0; } What is the return value of the function foo\text{foo} when it is called as foo(345, 10)\text{foo(345, 10)}?
Official PYQ2011AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
Four Matrices M1,M2,M3M_1, M_2, M_3 and M4M_4 of dimensions p×q,  q×r,  r×sp \times q, \:\:q \times r, \:\:r \times s and s×ts \times t respectively can be multiplied in several ways with different number of total scalar multiplications. For example when multiplied as ((M1×M2)×(M3×M4))((M_1 \times M_2) \times (M_3 \times M_4)), the total number of scalar multiplications is …
Official PYQ2011AlgorithmsAsymptotic Worst Case Time and Space ComplexityMCQ · 1 mark
Which of the given options provides the increasing order of asymptotic complexity of functions f1,f2,f3f_1, f_2, f_3 and f4f_4? f1(n)=2nf_1(n) = 2^n f2(n)=n3/2f_2(n) = n^{3/2} f3(n)=nlog⁡2nf_3(n) = n \log_2 n f4(n)=nlog⁡2nf_4(n) = n^{\log_2 n}
Official PYQ2011AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
An algorithm to find the length of the longest monotonically increasing sequence of numbers in an array A[0:n−1]A[0:n-1] is given below. Let LiL_i, denote the length of the longest monotonically increasing sequence starting at index ii in the array. Initialize Ln−1=1L_{n-1} = 1. For all ii such that 0≤i≤n−20 \leq i \leq n-2 …
Official PYQ2010AlgorithmsMinimum Spanning TreesMCQ · 1 mark
Consider a complete undirected graph with vertex set {0,1,2,3,4}\{0, 1, 2, 3, 4\}. Entry WijW_{ij} in the matrix WW below is the weight of the edge {i,j}\{i, j\} …
Official PYQ2010AlgorithmsMinimum Spanning TreesMCQ · 1 mark
Consider a complete undirected graph with vertex set {0,1,2,3,4}\{0, 1, 2, 3, 4\}. Entry WijW_{ij} in the matrix WW below is the weight of the edge {i,j}\{i, j\} …
Official PYQ2010AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
What is the value printed by the following C program? #include<stdio.h> int f(int *a, int n) { if (n <= 0) return 0; else if (*a % 2 == 0) return *a+f(a+1, n-1); else return *a - f(a+1, n-1); } int main() { int a[] = {12, 7, 13, 4, 11, 6}; printf("%d", f(a, 6)); return 0; }
Official PYQ2010AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
The weight of a sequence a0,a1,…,an−1a_0,a_1, \dots, a_{n-1} of real numbers is defined as a0+a1/2+⋯+an−1/2n−1a_0+a_1/2+ \dots + a_{n-1}/2^{n-1}. A subsequence of a sequence is obtained by deleting some elements from the sequence, keeping the order of the remaining elements the same. Let XX denote the maximum possible weight of a subsequence of ao,a1,…,an−1a_o,a_1, \dots, a_{n-1} and …
Official PYQ2010AlgorithmsAsymptotic Worst Case Time and Space ComplexityMCQ · 1 mark
Two alternative packages AA and BB are available for processing a database having 10k10^k records. Package AA requires 0.0001n20.0001 n^2 time units and package BB requires 10nlog⁡10n10n\log_{10} n time units to process nn records. What is the smallest value of kk for which package BB will be preferred over AA?
Official PYQ2009AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences X[m]X[m] and Y[n]Y[n] of lengths mm and nn, respectively with indexes of XX and YY starting from 00. We wish to find the length of the longest common sub-sequence (LCS) of X[m]X[m] and Y[n]Y[n] as …
Official PYQ2009AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
A sub-sequence of a given sequence is just the given sequence with some elements (possibly none or all) left out. We are given two sequences X[m]X[m] and Y[n]Y[n] of lengths mm and nn, respectively with indexes of XX and YY starting from 00. We wish to find the length of the longest common sub-sequence (LCS) of X[m]X[m] and Y[n]Y[n] as …
Official PYQ2009AlgorithmsSearching, Sorting, HashingMCQ · 1 mark
In quick-sort, for sorting nn elements, the (n/4)th\left(n/4\right)^{th} smallest element is selected as pivot using an O(n)O(n) time algorithm. What is the worst case time complexity of the quick sort?
Official PYQ2009AlgorithmsMinimum Spanning TreesMCQ · 1 mark
Consider the following graph: Which one of the following is NOT the sequence of edges added to the minimum spanning tree using Kruskal’s algorithm?
Official PYQ2009AlgorithmsAsymptotic Worst Case Time and Space ComplexityMCQ · 1 mark
The running time of an algorithm is represented by the following recurrence relation: T(n)={nn≤3T(n3)+cn otherwise T(n) = \begin{cases} n & n \leq 3 \\ T(\frac{n}{3})+cn & \text{ otherwise } \end{cases} Which one of the following represents the time complexity of the algorithm?
Official PYQ2009AlgorithmsAlgorithm Design: Dynamic ProgrammingMCQ · 1 mark
Consider the program below: #include <stdio.h> int fun(int n, int *f_p) { int t, f; if (n <= 1) { *f_p = 1; return 1; } t = fun(n-1, f_p); f = t + *f_p; *f_p = t; return f; } int main() { int x = 15; printf("%d/n", fun(5, &x)); return 0; } The value printed is:
Official PYQ2009AlgorithmsShortest Paths (Dijkstra, Bellman-Ford)MCQ · 1 mark
Which of the following statement(s) is/are correct regarding Bellman-Ford shortest path algorithm? P: Always finds a negative weighted cycle, if one exists. Q: Finds whether any negative weighted cycle is reachable from the source.
Official PYQ2009AlgorithmsSearching, Sorting, HashingMCQ · 1 mark
What is the number of swaps required to sort nn elements using selection sort, in the worst case?
Official PYQ2008AlgorithmsSearching, Sorting, HashingMCQ · 1 mark
Consider the following C program that attempts to locate an element xx in an array Y[ ]Y[ \ ] using binary search. The program is erroneous. f (int Y[10] , int x) { int i, j, k; i= 0; j = 9; do { k = (i + j) / 2; if( Y[k] < x) i = k;else j = k; } while (Y[k] != x) && (i < j)) ; if(Y[k] == x) printf(" x is in the array ") ; else printf(" x is …