Skip to content
Early access — you're among the first to try PYQLabs. Share feedback
Concept drill

recursion

GATE CSE & IT · Complexity Theory · 1990-2026

49
PYQs
82%
keyed
7
elite explanations
24
years appeared

Study anchor

Cormen et al. — Introduction to Algorithms (CLRS)

Algorithms, data structures, graph algorithms, complexity

Practice action

Start latest PYQ

PYQs in this concept

All concepts →
2026 PYQ

Consider the following code snippet in C language that computes the number of nodes in a non-empty singly linked list pointed to by the pointer variable head. struct node{ int elt;...

easyanswer keyelite explanation
2026 PYQ

Consider the recursive functions represented by the following code segment: int bar(int n){ if (n == 1) return 0; else return 1 + bar(n/2); } int foo(int n){ if (n == 1) return 1;...

medium
2025 Q61

#include int foo(int S[],int size) { if(size == 0) return 0; if(size == 1) return 1; if(S[0] != S[1]) return 1+foo(S+1,size-1); return foo(S+1,size-1); } int main() { int A[]={0,1,...

mediumanswer key
2025 PYQ

#include <stdio.h> int foo(int S[ ], int size) { if(size == 0) return 0; if(size == 1) return 1; if(S[0]!=S[1]) return 1 + foo(S + 1, size - 1); return foo(S + 1, size - 1); } int...

easybasic explanation
2024 Q19

Consider the following C program: #include void fX(); int main() { fX(); return 0;} void fX() { char a; if((a=getchar()) != '\n') fX(); if (a != '\n') putchar (a); } Assume that th...

mediumanswer key
2024 PYQ

Consider the following C program: #include <stdio.h> void fX(); int main() { fX(); return 0;} void fX() { char a; if ((a = getchar()) != '\n') fX(); if (a != '\n') putchar(a);} Ass...

easyanswer keybasic explanation
2023 PYQ

Consider the following program: int main ( ) { f1( ); f2(2); f3( ); return (0); } int f1( ) { return (1); } int f2 (int X) { f3( ); if (X==1) return f1 ( ); else return (X*f2(X$$-$...

easyanswer keybasic explanation
2021 PYQ

Consider the following ANSI C program #include <stdio.h> int foo(int x, int y, int q) { if ((x <= 0 && (y <= 0)) return q; if (x <= 0) return foo(x, y - q, q); if (y <= 0) return f...

mediumbasic explanation
2021 PYQ

What is the worst-case number of arithmetic operations performed by recursive binary search on a sorted array of size n?

easyanswer keybasic explanation
2021 PYQ

Consider the following ANSI C function: int SomeFunction (int x, int y) { if ( (x == 1) I I (y == 1)) return 1; if (x == y) return x; if (x > y) return SomeFunction(x - y, y); if (...

easybasic explanation
2020 PYQ

Consider the following C functions. int fun1 (int n) { static int i = 0; if (n > 0) { ++i; fun1(n-1); } return (i); } ___________________ int fun2 (int n) { static int i = 0; if (n...

mediumbasic explanation
2019 PYQ

Consider the following C function. void convert(int n){ if(n < 0) printf("%d",n); else { convert(n/2); printf("%d",n%2); } } Which one of the following will happen when the functio...

easyanswer keybasic explanation
2018 PYQ

Consider the following C program: #include < stdio.h > int counter = 0; int calc (int a, int b) { int c; counter++; if (b==3) return (a*a*a); else { c = calc(a, b/3); return (c*c*c...

easy
2015 PYQ

Consider the following function written in the C programming language. void foo(char *a){ if ( *a && *a != ' '){ foo(a+1); putchar(*a); } } The output of the above function on inpu...

easyanswer key
2015 PYQ

Consider the following C function. int fun ( int n ) { int x = 1, k ; if ( n == 1) return x ; for( k = 1 ; k < n ; ++ k ) x = x + fun( k ) * fun( n - k ) ; return x ; } The return...

medium
2015 PYQ

Suppose you are provided with the following function declaration in the C programming language. int partition(int a[], int n); The function treats the first element of a[ ] as a pi...

mediumanswer key
2014 PYQ

Consider the pseudocode given below. The function Dosomething () takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling represen...

mediumanswer key
2014 PYQ

Which of the following statements are CORRECT? 1) Static allocation of all data areas by a compiler makes it impossible to implement recursion. 2) Automatic garbage collection is e...

mediumanswer key
2014 PYQ

Consider the C function given below. int f(int j) { static int i = 50; int k; if (i == j) { printf("something"); k = f(i); return 0; } else return 0; } Which one of the following i...

easyanswer key
2014 PYQ

Consider the following function double f (double x) { if ( abs (x * x – 3) < 0. 01) return x; else return f (x / 2 + 1.5/x); } Give a value q (to 2 decimals) such that f(q) will re...

medium
2013 PYQ

What is the return value of f (p, p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is p...

mediumanswer key
2012 PYQ

The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudocode below is invoked as height (root) to compute the height...

mediumanswer key
2012 PYQ

The recurrence relation capturing the optional execution time of the Towers of Hanoi problem with $$n$$ discs is

easyanswer key
2011 PYQ

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; }...

easyanswer key
2011 PYQ

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; }...

easyanswer key
2008 PYQ

Which of the following are true? I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can...

mediumanswer key
2008 PYQ

Which of the following are true? I. A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can...

mediumanswer key
2008 PYQ

Choose the correct option to fill ? 1 and ? 2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character....

easyanswer key
2008 PYQ

Consider the following C functions: int f1(int n){ if(n == 0 || n == 1){ return n; } return (2 * f1(n - 1) + 3 * f1(n - 2)); } int f2(int n){ int i; int X[N], Y[N], Z[N]; X[0] = Y[...

mediumanswer keyelite explanation
2008 PYQ

Consider the following C functions: int f1(int n){ if(n == 0 || n == 1){ return n; } return (2 * f1(n - 1) + 3 * f1(n - 2)); } int f2(int n){ int i; int X[N], Y[N], Z[N]; X[0] = Y[...

easyanswer keyelite explanation
2007 PYQ

In the following C function, let n $$ \ge $$ m. int gcd(n,m) { if (n % m == 0) return m; n = n % m; return gcd(m,n); } How many recursive calls are made by this function?

mediumanswer keyelite explanation
2007 PYQ

Consider the following C program segment where CellNode represents a node in a binary tree: struct CellNode { struct CellNOde *leftChild; int element; struct CellNode *rightChild;...

easyanswer key
2007 PYQ

Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue are: i. isEmpty (Q) — returns true if the queue is empty, false ot...

easyanswer key
2007 PYQ

Consider the following C function: int f(int n) { static int r = 0; if(n <= 0) return 1; if(n>3) { r = n; return f(n-2)+2; } return f(n-1)+r; } What is the value of f(5)?

mediumanswer key
2006 PYQ

The following function computes the value of m C n correctly for all legal values m and n (m≥1,n≥0 and m>n) int func(int m, int n) { if (E) return 1; else return(func(m -1, n) + fu...

easyanswer key
2005 PYQ

A function f defined on stacks of integers satisfies the following properties. f(∅) = 0 and f (push (S, i)) = max (f(S), 0) + i for all stacks S and integers i. If a stack S contai...

easyanswer key
2005 PYQ

Consider the following C program: void foo (int n, int sum) { int k = 0, j = 0; if(n==0) return; k=n%10; j = n /10; sum = sum + k; foo(j, sum); printf("%d",k); } int main () { int...

easyanswer key
2005 PYQ

Consider the following C - function: double foo(int n){ int i; double sum; if(n == 0) return 1.0; sum = 0.0; for (i = 0; i < n; i++){ sum += foo(i); } return sum; } Suppose we modi...

easyanswer keyelite explanation
2005 PYQ

Consider the following C - function: double foo(int n){ int i; double sum; if(n == 0) return 1.0; sum = 0.0; for (i = 0; i < n; i++){ sum += foo(i); } return sum; } The space compl...

mediumanswer keyelite explanation
2004 PYQ

The time complexity of the following C function is (assume n > 0) int recursive(int n){ if(n == 1){ return (1); } return (recursive(n - 1) + recursive(n - 1)); }

easyanswer keyelite explanation
2004 PYQ

Consider the following C program segment struct CellNode{ struct CellNode *leftChild; int element; struct CellNode *rightChild; }; int Dosomething (struct CellNode *ptr) { int valu...

easyanswer key
2004 PYQ

Consider the following C function: int f(int n) { static int i = 1; if(n>=5) return n; n = n+1; i++; return f(n); } The value returned by f(1) is

easyanswer key
2003 PYQ

Consider the function f defined below. struct item { int data; struct item * next; }; int f(struct item *p) { return ((p == NULL) || (p ->next == NULL) || ((p->data <= p -> next ->...

easyanswer key
2001 PYQ

Consider the following C program: void abc(char *s) { if(s[0]=='\0') return; abc(s+1); abc(s+1); printf("%c",s[0]); } main() { abc("123"); } (a) What will be the output of the prog...

medium
1999 PYQ

Consider the following C function definition int Trial (int a, int b, int c) { if ((a > = b) && (c < b)) return b; else if (a > = b) return Trial (a,c,b); else return Trial (b,a,c)...

mediumanswer key
1998 PYQ

What value would the following function return for the input x = 95? function fun (x:integer):integer; Begin If x > 100 then fun : x – 10 Else fun : fun(fun (x + 11)) End;

mediumanswer key
1998 PYQ

What value would the following function return for the input x = 95? Function fun (x:integer):integer; Begin If x > 100 then fun : x – 10 Else fun : fun(fun (x + 11)) End;

mediumanswer key
1991 PYQ

Indicate the following statement true or false : A programming language not supporting either recursion or pointer type does not need the support of dynamic memory allocation.

easyanswer keybasic explanation
1990 PYQ

Match the followings: Group-I (a) Pointer data type (b) Activation Record (c) Repeat -Until (d) Coercion Group-II (p) Type Conversion (q) Dynamic Data Structure (r) Recursion (s) N...

easyanswer key