GATE / CS ARCHIVE

GATE CSE 2001 / QUESTION 13(a)

Trace doubled suffix recursion

Programming in C · Recursion · Full GATE CSE 2001 paper

GATE 2001, Q13(a) Descriptive
Report issue

Recursion

Question 13(a), GATE 2001: abc(s) returns at the null terminator; otherwise it calls abc(s+1) twice and then prints s[0]. main calls abc("123").
Question 13(a), GATE 2001: abc(s) returns at the null terminator; otherwise it calls abc(s+1) twice and then prints s[0]. main calls abc("123").
Tap to enlarge
Answer & solution

Answer: 3323321 (no official key was published)

Step through it (33 steps)

Let F(s) be the string printed by a call. F(empty)=empty and F(cs)=F(s)F(s)c. Therefore F("3")="3", F("23")="3"+"3"+"2"="332", and F("123")="332"+"332"+"1"="3323321". The recursive calls finish before the current character is printed, so the initial character appears last. No spaces or newline are printed.

Original paper, page 9