ProgrammingVery hardProduct company OA

What does r(3) print, and how many digits would r(6) print?

What does r(3) print, and how many digits would r(6) print?
void r(int n) {
    if (n == 0) return;
    r(n - 1);
    printf("%d", n);
    r(n - 1);
}
  1. A. 3212123, and 63 digits
  2. B. 121312, and 63 digits
  3. C. 1213121, and 64 digits
  4. D. 1213121, and 63 digits

Correct answer: D. 1213121, and 63 digits

r(n) is r(n − 1), then n, then r(n − 1) again: r(1) = 1, r(2) = 121, r(3) = 1213121. The length obeys L(n) = 2L(n − 1) + 1 with L(0) = 0, so L(n) = 2^n − 1 and r(6) prints 63 digits.

More C Programming questions

Exams that ask this topic