ProgrammingHardProduct company OA

What does p(5) print?

What does p(5) print?
void p(int n) {
    if (n <= 0) return;
    p(n - 2);
    printf("%d", n);
    p(n - 3);
}
  1. A. 13252
  2. B. 1325
  3. C. 5312
  4. D. 1352

Correct answer: D. 1352

This is an in-order walk: left subtree p(n − 2), then n, then right subtree p(n − 3). p(5) → p(3) → p(1) prints 1, then 3 (p(0) is empty), then 5, then p(2) prints 2. Result 1352.

More C Programming questions

Exams that ask this topic