ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from functools import lru_cache
@lru_cache(maxsize=None)
def t(n):
if n == 0:
return 0
if n < 3:
return 1
return t(n - 1) + t(n - 2) + t(n - 3)
print(t(25), t.cache_info().currsize)- A. 1389537 26
- B. 1389537 25
- C. 1389536 26
- D. 1389537 27
Correct answer: A. 1389537 26
The tribonacci-style recurrence reuses the cached values for n from 0 to 25, so the cache holds 26 entries. The cache turns an exponential recursion into one call per n.
More Python Programming questions
- What is printed? python from collections import deque def countpaths(g): R, C = len(g), len(g[0]) dist = [[-1] C for in range(R)] ways =…
- What is printed? python def cnt(a, k): pre = {0: 1} s = ans = 0 for x in a: s += x ans += pre.get(s - k, 0) pre[s] = pre.get(s, 0) + 1…
- What is printed? python def twotx(p): b1 = b2 = float('-inf') s1 = s2 = 0 for x in p: b1 = max(b1, -x) s1 = max(s1, b1 + x) b2 = max(b2…
- What is printed? python def tile(n): dp = [0] (n + 1) dp[0] = 1 for i in range(1, n + 1): dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0)…
- A program sorts the whole list again for each of 10^5 queries on a list of 10^5 items. Which statement about running it in CPython is…
- What is printed? python from collections import Counter def mw(s, t): need = Counter(t) have = {} formed = 0 req = len(need) l = 0 best =…