Data Structures & AlgorithmsHardProduct company OA
Problem: A divide-and-conquer routine makes 3 recursive calls on halves and does linear extra work: T(n) = 3T(n/2) + n. The code evaluates T exactly. What growth does the ratio T(2n)/T(n) reveal?
Problem: A divide-and-conquer routine makes 3 recursive calls on halves and does linear extra work: T(n) = 3T(n/2) + n. The code evaluates T exactly. What growth does the ratio T(2n)/T(n) reveal?
from functools import lru_cache
@lru_cache(None)
def T(n):
if n <= 1: return 1
return 3 * T(n // 2) + n
print(T(1024), T(2048), round(T(2048) / T(1024), 2))What is printed?- A. 11264 24576 2.18
- B. 175099 350198 2.0
- C. 175099 527345 2.0
- D. 175099 527345 3.01
Correct answer: D. 175099 527345 3.01
Master theorem: a = 3, b = 2, f(n) = n, and n^(log₂ 3) ≈ n^1.585 dominates n, so T(n) = Θ(n^log₂3). Doubling n multiplies the time by about 2^1.585 = 3 – the ratio 3.01 confirms it (an n log n algorithm would show ≈ 2.2, a linear one 2). This is the recurrence of Karatsuba multiplication.
More Coding Interview Problems questions
- Problem: A sliding-window routine has a while loop nested inside a for loop. For a 12 000-character string, how many inner (left-pointer)…
- Problem: Find the inefficiency: two BFS versions on an R × C open grid. One marks a cell visited when it is pushed, the other only when it…
- Problem: Find the bug: binary search for the LAST x in [lo, hi] with ok(x) true (here the largest x with x² ≤ 30). The first version uses…
- Problem: n ≤ 2·10⁵ meetings [s, e); choose the maximum number of non-overlapping ones. Compare three greedy orders: by end time, by start…
- Problem: A road network has weights 1..5 (n, m ≤ 2·10⁵). A candidate solution runs plain BFS and sets dist[v] = dist[u] + w the first time…
- Problem: Each link succeeds with a probability; find the most reliable path from 0 to 3 (maximise the product). Dijkstra works with a…