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
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 when it is called as ?
An undirected graph contains nodes named . Two nodes are connected if and only if . Each edge is assigned a weight . A sample graph with is shown below. What will be the cost of the minimum spanning tree (MST) of such a graph with …
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 when it is called as ?
Four Matrices and of dimensions and respectively can be multiplied in several ways with different number of total scalar multiplications. For example when multiplied as , the total number of scalar multiplications is …
Which of the given options provides the increasing order of asymptotic complexity of functions and ?
An algorithm to find the length of the longest monotonically increasing sequence of numbers in an array is given below. Let , denote the length of the longest monotonically increasing sequence starting at index in the array. Initialize . For all such that …
Consider a complete undirected graph with vertex set . Entry in the matrix below is the weight of the edge …
Consider a complete undirected graph with vertex set . Entry in the matrix below is the weight of the edge …
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; }
The weight of a sequence of real numbers is defined as . A subsequence of a sequence is obtained by deleting some elements from the sequence, keeping the order of the remaining elements the same. Let denote the maximum possible weight of a subsequence of and …
Two alternative packages and are available for processing a database having records. Package requires time units and package requires time units to process records. What is the smallest value of for which package will be preferred over ?
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 and of lengths and , respectively with indexes of and starting from . We wish to find the length of the longest common sub-sequence (LCS) of and as …
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 and of lengths and , respectively with indexes of and starting from . We wish to find the length of the longest common sub-sequence (LCS) of and as …
In quick-sort, for sorting elements, the smallest element is selected as pivot using an time algorithm. What is the worst case time complexity of the quick sort?
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?
The running time of an algorithm is represented by the following recurrence relation: Which one of the following represents the time complexity of the algorithm?
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:
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.
What is the number of swaps required to sort elements using selection sort, in the worst case?
Consider the following C program that attempts to locate an element in an array 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 …
