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?
- A. 1280 bytes, 1023 calls
- B. 1248 bytes, 59 calls
- C. 192 bytes, 59 calls
- 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.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- void swap(int x, int y) { x ^= y; y ^= x; x ^= y; } int main() { int a[5] = {3, 7, 9, 12, 5}; for (int i = 0; i < 5; i++) swap(&a[i], &a[4…
- for (i = 2; i <= 100; i++) if (!comp[i]) for (j = i i; j <= 100; j += i) { comp[j] = 1; cnt++; } With comp[] initially all 0, what is the…
- long p(long x, int n) { if (n == 0) return 1; if (n % 2 == 0) { long h = p(x, n / 2); return h h; } return x p(x, n - 1); } How many…
- void perm(char s, int k) { if (s[k] == 0) { puts(s); return; } for (int i = k; s[i]; i++) { char t = s[k]; s[k] = s[i]; s[i] = t; perm(s…
- void h(int n, char a, char b, char c) { if (n == 0) return; h(n - 1, a, c, b); printf("move disk %dn", n); h(n - 1, c, b, a); } During…
- Bubble sort with early exit (a pass that makes no swap ends the sort, and every pass scans the whole array from index 0 to n-2) is run on…