ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int calls;
int f(int n) { calls++; return n < 3 ? n : f(n - 1) + f(n - 3); }
int main(void) {
int r = f(8);
printf("%d %d", r, calls);
}- A. 15 25
- B. 15 17
- C. 21 67
- D. 13 25
Correct answer: A. 15 25
Values: f(0..2) = 0, 1, 2, then f(n) = f(n − 1) + f(n − 3): 2, 3, 5, 7, 10, 15. Calls obey C(n) = 1 + C(n − 1) + C(n − 3) with C(0..2) = 1: 3, 5, 7, 11, 17, 25. Both operands update the same global, but each update happens inside a function call, and function calls are not interleaved, so the total is well defined whichever operand runs first. '21 67' uses the Fibonacci recurrence f(n − 2).
More C Programming questions
- What is printed? c char s[] = "aaabccdddd"; char r = s, w = s; while (r) { char c = r; int n = 0; while (r == c) { r++; n++; } w++ = c; if…
- Assume 32-bit unsigned int. What is printed? c unsigned int x = 0x5Cu; / 0101 1100 / unsigned int low = x & -x; unsigned int y = x + low…
- Assume 4-byte int with 4-byte alignment. What is printed? c struct P { char tag; int v; }; struct P a[4] = {{'a', 1}, {'b', 2}, {'c', 3}…
- Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed? c char c = 'xff'; printf("%d %d %d %d", c == 0xFF…
- Read the declaration: c char ((x)[4])(int); What is x?
- What is printed? c char a[8] = "abcdefg"; memmove(a + 2, a, 4); char b[6] = "XXXXX"; strncpy(b, "hi", 4); char c[4]; strncpy(c, "wxyz"…