ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int c = 0;
int f(int n) { c++; return n <= 1 ? 1 : f(n / 2) + f(n - n / 2); }
int main(void) {
    int r = f(7);
    printf("%d %d", r, c);
}
  1. A. 7 13
  2. B. 8 15
  3. C. 7 7
  4. D. 7 14

Correct answer: A. 7 13

Each call splits n into two parts that add up to n, down to pieces of size 1, so the result is n itself: 7. The recursion tree has 7 leaves and, being a full binary tree, 6 internal nodes: 13 calls.

More C Programming questions

Exams that ask this topic