GATE / CS ARCHIVE

GATE CSE 2001 / QUESTION 13(b)

Count recursion output characters

Programming in C · Recursion · Full GATE CSE 2001 paper

GATE 2001, Q13(b) Descriptive
Report issue

Recursion

Question 13(b), GATE 2001: The preceding abc routine is called on a null-terminated string of n characters, excluding the terminator.
Question 13(b), GATE 2001: The preceding abc routine is called on a null-terminated string of n characters, excluding the terminator.
Tap to enlarge
Answer & solution

Answer: 2^n − 1 characters (no official key was published)

For n=0 the function immediately returns and prints zero characters. For n>0 it makes two calls on length n−1 and prints one character itself. Thus C(0)=0 and C(n)=2C(n−1)+1. Expanding gives C(n)=1+2+4+…+2n−1=2n−1. Equivalently, induction substitutes C(n−1)=2n−1−1 to get 2n−1. The null-terminator calls contribute no output. For n=3 this predicts seven characters, agreeing with 3323321.

Original paper, page 9