GATE Computer ScienceHardGATE CS (PYQ pattern)

void f(int n) { if (n <= 1) return; f(n - 1); f(n / 2); } Each activation record takes 32 bytes. What is the peak stack usage in bytes during f(40) (counting the frame of the first call), and how many calls in all (including the first) does f(10) make?

  1. A. 1280 bytes, 1023 calls
  2. B. 1248 bytes, 59 calls
  3. C. 192 bytes, 59 calls
  4. D. 1280 bytes, 59 calls

Correct answer: D. 1280 bytes, 59 calls

The deepest chain always takes the f(n - 1) branch: f(40), f(39), ..., f(1) is 40 frames, 1280 bytes (the f(n / 2) branches are shallower). Calls satisfy C(n) = 1 for n <= 1 and C(n) = 1 + C(n - 1) + C(n / 2): C(2) = 3, C(3) = 5, C(4) = 9, C(5) = 13, C(6) = 19, C(7) = 25, C(8) = 35, C(9) = 45, C(10) = 59.

More Programming and Data Structures (GATE) questions

Exams that ask this topic