GateAI

GATE CSE Recursion PYQs

Medium yield13.8% of Programming and Data Structures

GATE tests Recursion primarily through direct execution tracing of recursive C code snippets, stack-unwinding behavior (pre-order vs. post-order execution), and… Read the guide

Questions
19
Asked
since 2011
Total marks
34
Marks / paper
1.3

19 GATE CSE Recursion PYQs with solutions, from Programming and Data Structures. Browse all GATE CSE PYQs.

Questions

Every official paper that asked this topic, newest first.

What GATE tests in Recursion

Concepts, patterns, formulas and traps distilled from past papers.

GATE tests Recursion primarily through direct execution tracing of recursive C code snippets, stack-unwinding behavior (pre-order vs. post-order execution), and termination/infinite recursion analysis. Questions frequently incorporate static variables, pointer/reference aliasing, mutual recursion, and tree/array traversals to test whether candidates can systematically maintain call frames. In recent years, the focus has expanded from standard tracing to higher-order mathematical characterizations (e.g., finding the smallest input or maximum output value of composed recurrences).

Core concepts

  • Call stack frame allocation, activation records, and unwinding order
  • Pre-order actions (before recursive call) vs Post-order actions (after recursive call)
  • Base conditions and off-by-one boundary failures causing infinite recursion (Stack Overflow)
  • Lifetime and visibility of `static` local variables across recursive frames
  • Pass-by-value vs pass-by-reference semantics in recursive parameter mutation
  • Postfix (`val--`) vs prefix (`--val`, `++n`) operators inside recursive argument expressions
  • Mutual recursion between two or more interdependent functions
  • Array slicing via pointer arithmetic (`S + 1`, `size - 1`)
  • Tree post-order bottom-up subtree metric computation

Recurring question patterns

  • Stack Unwinding and Reverse Printing

    CommonMCQ1 marks2024, 2015, 2011

    A recursive function reads or processes input (strings, digits, or characters) and performs output operations (like putchar or printf) after the recursive call returns, effectively reversing the input or accumulating from bottom to top.

  • Static Variable and Reference Side Effects in Recursion

    CommonMCQ2 marks2016, 2014, 2013

    A recursive snippet uses a static local variable or pass-by-reference parameter that persists across activation records. Output depends heavily on tracking the shared state before and after recursive calls.

  • Recursive Call Counting and Recurrence Relations

    CommonNAT2 marks2015, 2014

    Evaluating the total number of function invocations, runtime complexity, or dynamic programming-like recurrence evaluations inside a loop.

  • Termination Flaws and Infinite Recursion vs Infinite Loop

    CommonMCQ2 marks2019, 2017, 2014

    Analyzing code with subtle bugs such as wrong base cases (e.g., n < 0 instead of n <= 0), postfix decrements in arguments (foo(val--)), or unmodified loop variables to identify stack overflow vs non-terminating loops.

  • Mutual and Multi-branch Recursion

    Occasionalmixed2 marks2021, 2017

    Two functions calling each other (fun1/fun2) or a function making multiple branch calls with parameter arithmetic, requiring candidate to draw or trace a small recursion tree.

  • Structural / Pointer Recursion (Trees and Arrays)

    Occasionalmixed2 marks2025, 2023

    Recursive traversal over data structures (arrays with pointer arithmetic S+1, binary tree post-order sum) to compute counts, transitions, or subtree properties.

  • Mathematical Abstraction and Composed Recurrences

    OccasionalNAT2 marks2026, 2014

    Recursive definitions encoding mathematical operations (e.g., ⌊log⁡2n⌋\lfloor \log_2 n \rfloor, Newton-Raphson fixed points, modulo transitions) asking for the smallest input nn for an output target or maximum attainable return value.

Formulas GATE uses

Total Invocations Recurrence

T(n)=1+∑iT(gi(n))T(n) = 1 + \sum_{i} T(g_i(n))

Used to count the total number of function activations including base cases (e.g., T(n)=1+T(n−1)+T(n−3)T(n) = 1 + T(n-1) + T(n-3)).

Digit Sum in Base $r$

S(n,r)=(n mod r)+S(⌊n/r⌋,r)S(n, r) = (n \bmod r) + S(\lfloor n/r \rfloor, r)

Used when recursive functions peel off least significant digits/bits via modulo and integer division.

Catalan Convolution Recurrence

xn=1+∑k=1n−1xk⋅xn−kx_n = 1 + \sum_{k=1}^{n-1} x_k \cdot x_{n-k}

Used when tracing recursive loops computing combinations of subproblem products.

Repeated Logarithm / Tower Function

bar(n)=⌊log⁡2n⌋  ⟹  foo(n)=1+foo(⌊log⁡2n⌋)\text{bar}(n) = \lfloor \log_2 n \rfloor \implies \text{foo}(n) = 1 + \text{foo}(\lfloor \log_2 n \rfloor)

Used for nested recursive functions where the base-case depth grows like an iterated logarithm or tower of powers.

Examiner traps in Recursion

  • Postfix operators in arguments: writing `foo(val--)` passes the un-decremented `val`, causing immediate infinite recursion, whereas `foo(--val)` decrements first.
  • Static variable persistence: forgetting that a static variable is not reset upon re-entering or unwinding stack frames, leading to incorrect post-unwinding outputs.
  • Wrong base case triggering infinite recursion: e.g., using `if (n < 0)` for division by 2, causing `n = 0` to recurse endlessly on `0 / 2 = 0`.
  • Evaluation order ambiguity: assuming standard left-to-right operand evaluation when pass-by-reference side effects occur within the same expression (e.g., `f(x, c) * x`).
  • Counting only recursive steps rather than total invocations: forgetting to count base case calls (leaf calls) in invocation count questions.
  • Failing to observe finite-state transitions in parameter spaces: missing that modulo arithmetic transitions form unreachable states or sink cycles.

How questions are asked

  • Trace and predict terminal printed output or return value
  • NAT: Find the total number of invocations of a recursive function
  • NAT: Find the smallest positive input integer n achieving a target output
  • NAT: Find the maximum/minimum possible return value across all valid inputs
  • Identify termination behavior (normal termination vs infinite loop vs stack overflow)
  • Trace tree/array traversal outputs with structural pointers

Paper trends

  • Shift from standard single-variable digit/string recursion MCQs to multi-variable branching, mutual recursion, and edge-case termination analysis.

    2019, 2017, 2015, 2011

  • Increase in NAT questions requiring exact integer computation, invocation counts, or reverse-solving for the minimum input size / maximum possible return value.

    2026, 2025, 2021

  • Integration of recursion with core data structures (arrays passed via pointer offsets, binary tree subtree aggregations).

    2025, 2023

Difficulty notes

Easy: Single recursive call with direct digit extraction, string reversal, or simple array traversal without static variables (1 mark MCQ/NAT). Medium: Branching recursion (2+ calls per frame), mutual recursion, static variables mutating across recursive steps, or computing invocation trees (2 marks MCQ/NAT). Hard: Composed recurrences requiring inverse function analysis (e.g., smallest nn requiring power tower calculation), interval modulo invariant analysis, or subtle compiler evaluation/termination traps.