Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
Problem: Ackermann's function grows faster than any primitive-recursive function. Compute A(2, 3) and count the recursive calls.
Problem: Ackermann's function grows faster than any primitive-recursive function. Compute A(2, 3) and count the recursive calls.
import sys
sys.setrecursionlimit(10000)
calls = 0
def A(m, n):
global calls
calls += 1
if m == 0: return n + 1
if n == 0: return A(m - 1, 1)
return A(m - 1, A(m, n - 1))
print(A(2, 3), calls)What is printed?- A. 9 44
- B. 7 44
- C. 9 27
- D. 11 106
Correct answer: A. 9 44
A(1, n) = n + 2 and A(2, n) = 2n + 3, so A(2, 3) = 9, yet the recursion makes 44 calls because every level re-expands the levels below it. A(4, 2) already has 19,729 digits.
More Coding Interview Problems questions
- Problem: Count the calls made by naive recursive Fibonacci versus a memoised version for n = 10. python from functools import lrucache…
- What is the time complexity of this loop? python for i in range(1, n + 1): j = 1 while j j <= i: j += 1
- What is the solution of T(n) = 2T(√n) + log n?
- A hash table with separate chaining and simple uniform hashing has n keys in m buckets (load factor α = n/m). What is the expected time of…
- An LRU cache must support get and put in O(1). Which design achieves this?
- Edge case: identity versus equality in Python. python a = [1, 2]; b = [1, 2]; c = a print(a == b, a is b, a is c, [] is []) What is printed?