chapter

    Recursion and Recursive Program Analysis Short Notes for Gate Cs

    Recursion and Recursive Program Analysis short notes for GATE CS: 9 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice qu

    Recursion and Recursive Program Analysis short notes

    Formula Matrix

    f(S+1, n-1) shifts view by 1. Condition: S is base type pointer.
    return val + f(...) accumulates on descent. Condition: op precedes call.
    f(...); op(S) executes on ascent. Condition: op follows call (reverses sequence).
    Time O(N). Condition: single recursive call f(S+1, n-1).
    Aux space O(N). Condition: deferred action prevents tail-call optimization.
    Aux space O(1). Condition: recursive call is absolute last operation.
    Blocks = transitions + 1. Condition: contiguous identical elements.

    If You See This, Do This

    Stem TriggerFirst Move
    f(S+1) before printfTrace descent to base case; execute on ascent (reverse).
    return 1 + f(...)Count transitions or accumulate state on descent.
    size == 1 with S[1] accessFlag out-of-bounds / undefined behavior.
    *(s+1) in deferred printPrint all chars except the last one, in reverse.
    "auxiliary space" + deferred printAnswer O(N); ignore total space.
    Iterative filter to recursiveCheck if dst advances (dst+1) or offsets (dst+c).
    S+1 with char vs intCompiler scales by sizeof(type); view shifts, not data.

    Traps and Invariants

    Confuses pre/post order execution. Check: Mark recursive call; anything after executes on return path.
    Off-by-one in base case boundary. Check: Substitute size=1 and size=0; verify no S[1] access.
    Misinterprets S+1 as value addition. Check: Verify S is pointer; S+1 shifts memory address by sizeof(type).
    Invariant violation: S must point to current unprocessed element, size to exact remaining count. Check: Verify pointer shift and size reduction preserve this mapping.

    6 more cards in this chapter

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    Level 1: Warm-up

    Assertion (A): A recursive function that calls itself before printing the current character will output the string in reverse order.

    Reason (R): The print statement is deferred until the call stack unwinds, causing the last character processed to be printed first.

    Question 2
    Level 1: Warm-up

    Consider an array of size consisting of elements that are either 0 or 1. The array is processed by a recursive function that counts the number of contiguous blocks of identical elements. What is the maximum possible number of contiguous blocks this function can return for an array of size 8?

    Question 3
    Level 1: Warm-up

    Consider a recursive function that makes a recursive call and then prints the current element. Which of the following statements is true regarding the execution of the print statements?

    Question 4
    Level 1: Warm-up

    In the subtraction-based Euclidean algorithm for finding the Greatest Common Divisor (GCD) of two positive integers and , which of the following state transitions is impossible if the algorithm strictly maintains its mathematical invariant?

    Question 5
    Level 1: Warm-up

    An unknown recursive function f(x, y) is defined as:

    ```c

    int f(int x, int y) {

    if (y == 0) return x;

    return f(x + 1, y - 1);

    }

    ```

    What is the value returned by f(10, 3)?

    Question 6
    Level 1: Warm-up

    Let S be a pointer to the first element of an array of int in C, where the size of int is 4 bytes. If the memory address stored in S is 2048, what is the memory address (in bytes) obtained by evaluating S + 3?

    Question 7
    Level 1: Warm-up

    A recursive function counts the number of contiguous blocks of identical elements in an array of size . If the array is guaranteed to contain at least one occurrence of every integer from 1 to , what is the minimum possible number of blocks the function can return?

    Question 8
    Level 1: Warm-up

    A recursive array traversal function reduces the problem size by 1 in each step and accesses S[0] and S[1]. To safely prevent out-of-bounds access for any initial array size , how many distinct base cases are minimally required?

    Question 9
    Level 1: Warm-up

    In the subtraction-based Euclidean algorithm, what is the maximum number of recursive calls initiated (excluding the final base case evaluation) to compute ?

    Question 10
    Level 1: Warm-up

    Which of the following statements is strictly true regarding the evaluation bound of the nested recursive expression ?

    Free preview ends here

    Login to view the complete short notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Recursion and Recursive Program Analysis Short Notes for GATE CS

    Recursion and Recursive Program Analysis short notes for GATE CS: 9 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Formula Matrix

    f(S+1, n-1) shifts view by 1. Condition: S is base type pointer.
    return val + f(...) accumulates on descent. Condition: op precedes call.
    f(...); op(S) executes on ascent. Condition: op follows call (reverses sequence).
    Time O(N). Condition: single recursive call f(S+1, n-1).
    Aux space O(N). Condition: deferred action prevents tail-call optimization.
    Aux space O(1). Condition: recursive call is absolute last operation.
    Blocks = transitions + 1. Condition: contiguous identical elements.

    If You See This, Do This

    Stem TriggerFirst Move
    f(S+1) before printfTrace descent to base case; execute on ascent (reverse).
    return 1 + f(...)Count transitions or accumulate state on descent.
    size == 1 with S[1] accessFlag out-of-bounds / undefined behavior.
    *(s+1) in deferred printPrint all chars except the last one, in reverse.
    "auxiliary space" + deferred printAnswer O(N); ignore total space.
    Iterative filter to recursiveCheck if dst advances (dst+1) or offsets (dst+c).
    S+1 with char vs intCompiler scales by sizeof(type); view shifts, not data.

    Traps and Invariants

    Confuses pre/post order execution. Check: Mark recursive call; anything after executes on return path.
    Off-by-one in base case boundary. Check: Substitute size=1 and size=0; verify no S[1] access.
    Misinterprets S+1 as value addition. Check: Verify S is pointer; S+1 shifts memory address by sizeof(type).
    Invariant violation: S must point to current unprocessed element, size to exact remaining count. Check: Verify pointer shift and size reduction preserve this mapping.

    Recall Check

    Recursion and Recursive Program Analysis: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming and Data Structures MCQ

    Assertion (A): A recursive function that calls itself before printing the current character will output the string in reverse order.

    Reason (R): The print statement is deferred until the call stack unwinds, causing the last character processed to be printed first.

    1. A.

      Both A and R are true and R is the correct explanation of A.

    2. B.

      Both A and R are true but R is not the correct explanation of A.

    3. C.

      A is true but R is false.

    4. D.

      A is false but R is true.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is an assertion-reason question about string reversal using recursion.

    Step 1: Evaluate Assertion (A). Calling the function before printing defers the print to the ascent phase, reversing the string. (A is true).

    Step 2: Evaluate Reason (R). The print is indeed deferred until the stack unwinds, meaning the deepest call (last character) prints first. (R is true).

    Step 3: Check if R explains A. Yes, the deferred execution on the unwinding stack is exactly why the string is reversed.

    Answer: Both A and R are true and R is the correct explanation of A.

    Question 2 · Programming and Data Structures MCQ

    Consider an array of size consisting of elements that are either 0 or 1. The array is processed by a recursive function that counts the number of contiguous blocks of identical elements. What is the maximum possible number of contiguous blocks this function can return for an array of size 8?

    1. A.

      4

    2. B.

      7

    3. C.

      8

    4. D.

      9

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is an observation question about the maximum number of blocks in an array.

    Step 1: A contiguous block is a sequence of identical adjacent elements.

    Step 2: To maximize the number of blocks, every element must be different from its neighbors.

    Step 3: For an array of size 8, the alternating sequence 0, 1, 0, 1, 0, 1, 0, 1 creates 8 separate blocks.

    Step 4: Thus, the maximum number of blocks is 8.

    Answer: 8

    Question 3 · Programming and Data Structures MCQ

    Consider a recursive function that makes a recursive call and then prints the current element. Which of the following statements is true regarding the execution of the print statements?

    1. A.

      They execute during the descent phase.

    2. B.

      They execute in the reverse order of the recursive calls.

    3. C.

      They execute in the exact same order as the recursive calls.

    4. D.

      They execute before the base case is reached.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a statement truth question about deferred actions in recursion.

    Step 1: The print statement is placed after the recursive call.

    Step 2: This means the print action is deferred until the recursive call returns.

    Step 3: The descent phase reaches the base case without printing anything.

    Step 4: As the call stack unwinds (ascent phase), the deferred print statements execute in reverse order of the calls.

    Answer: They execute in the reverse order of the recursive calls.

    Question 4 · Programming and Data Structures MCQ

    In the subtraction-based Euclidean algorithm for finding the Greatest Common Divisor (GCD) of two positive integers and , which of the following state transitions is impossible if the algorithm strictly maintains its mathematical invariant?

    1. A.

      when

    2. B.

      when

    3. C.

      when

    4. D.

      when

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a GCD invariant question, recognizable because it asks about state transitions in the subtraction-based Euclidean algorithm.

    Step 1: The invariant of the Euclidean algorithm is that the set of common divisors remains unchanged when the larger number is replaced by its difference with the smaller number.

    Step 2: Therefore, valid transitions are when , or when .

    Step 3: Adding the numbers () changes the set of common divisors and violates the invariant.

    Answer: when is impossible.

    Question 5 · Programming and Data Structures MCQ

    An unknown recursive function f(x, y) is defined as:

    ```c

    int f(int x, int y) {

    if (y == 0) return x;

    return f(x + 1, y - 1);

    }

    ```

    What is the value returned by f(10, 3)?

    1. A.

      10

    2. B.

      12

    3. C.

      13

    4. D.

      30

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a reverse engineering question, recognizable because it provides an unknown recursive function and asks for its output on specific inputs.

    Step 1: Trace the function f(x, y) with initial values .

    Step 2: Call 1: f(10, 3). Since , it returns f(10 + 1, 3 - 1) = f(11, 2).

    Step 3: Call 2: f(11, 2). Returns f(12, 1).

    Step 4: Call 3: f(12, 1). Returns f(13, 0).

    Step 5: Call 4: f(13, 0). Base case is met, so it returns , which is 13.

    Answer: 13

    Question 6 · Programming and Data Structures NAT

    Let S be a pointer to the first element of an array of int in C, where the size of int is 4 bytes. If the memory address stored in S is 2048, what is the memory address (in bytes) obtained by evaluating S + 3?

    Correct Answer:

    2060.00

    Step-by-Step Solution

    Key idea: This is a pointer arithmetic question. Adding an integer to a pointer scales by the size of the base type.

    Step 1: The base type is int, which occupies 4 bytes.

    Step 2: The expression S + 3 advances the pointer by 3 elements, not 3 bytes.

    Step 3: The byte offset is bytes.

    Step 4: The new address is .

    Answer: 2060.00

    Question 7 · Programming and Data Structures MCQ

    A recursive function counts the number of contiguous blocks of identical elements in an array of size . If the array is guaranteed to contain at least one occurrence of every integer from 1 to , what is the minimum possible number of blocks the function can return?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a contradiction/bounding question about the minimum number of blocks given distinct elements.

    Step 1: The array contains all integers from 1 to at least once.

    Step 2: Each distinct integer must form at least one contiguous block.

    Step 3: To minimize the total number of blocks, all occurrences of the same integer should be grouped together.

    Step 4: If grouped perfectly, there is exactly 1 block per distinct integer, totaling blocks.

    Answer:

    Question 8 · Programming and Data Structures MCQ

    A recursive array traversal function reduces the problem size by 1 in each step and accesses S[0] and S[1]. To safely prevent out-of-bounds access for any initial array size , how many distinct base cases are minimally required?

    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. D.

      4

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a boundary condition question, recognizable because it asks for the minimal base cases to prevent out-of-bounds access.

    Step 1: The function accesses S[0] and S[1]. If the size n is 1, accessing S[1] is out of bounds. Thus, n == 1 must be a base case.

    Step 2: If the initial array size can be 0, accessing S[0] is also out of bounds. Thus, n == 0 must also be a base case.

    Answer: 2 distinct base cases are minimally required.

    Question 9 · Programming and Data Structures MCQ

    In the subtraction-based Euclidean algorithm, what is the maximum number of recursive calls initiated (excluding the final base case evaluation) to compute ?

    1. A.

      2

    2. B.

      4

    3. C.

      5

    4. D.

      10

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is an observation question, recognizable because it asks for the number of steps in a specific, small-scale execution of a known algorithm.

    Step 1: Trace the calls for . Since , it calls . (Call 1)

    Step 2: calls . (Call 2)

    Step 3: calls . (Call 3)

    Step 4: calls . (Call 4)

    Step 5: meets the base case and returns 2 without initiating further calls.

    Answer: 4 recursive calls are initiated.

    Question 10 · Programming and Data Structures MCQ

    Which of the following statements is strictly true regarding the evaluation bound of the nested recursive expression ?

    1. A.

      The outer function must be fully expanded before is evaluated.

    2. B.

      The inner function must resolve to a concrete integer before can proceed.

    3. C.

      Both and evaluate simultaneously to bound the execution time.

    4. D.

      The base case of is checked before is evaluated.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a bounding statement question, recognizable because it asks for the strict evaluation constraint of a nested recursive expression.

    Step 1: Identify the structure: is called with the result of as its argument.

    Step 2: Apply the rule of nested call resolution: strict inner-first evaluation.

    Step 3: Conclude that must completely finish and return a concrete integer value before the outer function can substitute that value and proceed.

    Answer: The inner function must resolve to a concrete integer before can proceed.

    More short notes in this unit