Data Structures & AlgorithmsHardProduct Company SDE pattern
Solve the recurrences numerically: how many 'work units' does each do for n = 64?
Solve the recurrences numerically: how many 'work units' does each do for n = 64?
from functools import lru_cache @lru_cache(None) def A(n): return 1 if n <= 1 else 2 * A(n // 2) + n @lru_cache(None) def B(n): return 1 if n <= 1 else A_(n) def A_(n): return B(n // 2) + 1 @lru_cache(None) def Cc(n): return 1 if n <= 1 else 4 * Cc(n // 2) + n print(A(64), B(64), Cc(64))
- A. 384 6 4096
- B. 448 6 8128
- C. 448 7 4096
- D. 448 7 8128
Correct answer: D. 448 7 8128
T(n) = 2T(n/2) + n with T(1) = 1 is n log₂ n + n = 64·6 + 64 = 448 → Θ(n log n). T(n) = T(n/2) + 1 is log₂ n + 1 = 7 → Θ(log n). T(n) = 4T(n/2) + n is 2n² − n = 8128 → Θ(n²) (master theorem case 1, since n^(log₂4) = n² dominates n).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- 0/1 knapsack, capacity 50: items (weight, value) = (10, 60), (20, 100), (30, 120). What do greedy-by-ratio and DP give? python items =…
- Activity selection: activities (start, finish). How many can one person attend, and which? python acts = [(1, 4), (3, 5), (0, 6), (5, 7)…
- Huffman coding: frequencies a:5, b:9, c:12, d:13, e:16, f:45. What is the total number of bits to encode the text? python import heapq f =…
- Longest common subsequence: what is the length and one LCS read back from the table (preferring the upper cell on ties)? python a, b =…
- Edit distance between "kitten" and "sitting", and between "intention" and "execution". What is printed? python def ed(a, b): prev =…
- Matrix chain order for dimensions 5 × 10, 10 × 3, 3 × 12, 12 × 5, 5 × 50, 50 × 6. What is the minimum number of scalar multiplications?…