GATE Computer ScienceVery hardGATE CS (PYQ pattern)
int w(int n) { if (n < 0) return 0; if (n == 0) return 1; return w(n - 1) + w(n - 3) + w(n - 4); } What does w(10) return, and how many calls to w (including the first, and including calls with n < 0) does it make?
- A. 64, 313 calls
- B. 64, 168 calls
- C. 69, 313 calls
- D. 64, 469 calls
Correct answer: A. 64, 313 calls
Values: w(0..10) = 1, 1, 1, 2, 4, 6, 9, 15, 25, 40, 64, so w(10) = 64. Calls: C(n) = 1 for n <= 0 and C(n) = 1 + C(n-1) + C(n-3) + C(n-4) otherwise, giving C(1..10) = 4, 7, 10, 16, 28, 46, 73, 118, 193, 313. Leaving out the calls with n < 0 gives 168.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int x = 0; void q(int n) { if (n > 0) { x += n; q(n - 1); printf("%d ", x); if (n > 2) q(n - 2); } } What does q(4) print?
- Integers 1, 2, 3, 4, 5 are pushed on an initially empty stack in this order; pops may be interleaved with pushes at any time. How many of…
- The integers 1 to 6 are pushed in this order onto a stack that can hold at most 2 elements at any moment; pops may be interleaved with…
- A circular queue stores items in an array of 8 slots (indices 0 to 7) and keeps two fields, front (index of the first item) and count…
- The infix expression a + b (c ^ d - e) ^ (f + g h) - i (with ^ right-associative and higher than and +) is converted to postfix with the…
- Evaluate the postfix expression 9 3 - 4 2 5 - 7 3 / + where / is integer division truncating toward zero.