Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
What is the solution of T(n) = T(n/2) + T(n/4) + n?
- A. Θ(n log n)
- B. Θ(n^1.5)
- C. Θ(n log log n)
- D. Θ(n)
Correct answer: D. Θ(n)
Each recursion level costs a fraction of the previous one: n, then n/2 + n/4 = 3n/4, then 9n/16, … a geometric series summing to 4n. When the subproblem sizes add up to less than n, linear work at the root dominates.
More Coding Interview Problems questions
- Edge case: Java's int arithmetic wraps around in two's complement. The Python code below emulates exactly what this Java line computes…
- Edge case: Array sorting and parseInt in JavaScript. javascript console.log([3, 20, 100].sort().join(","), [1, 2…
- Problem: Answer sub-rectangle sum queries on a matrix in O(1) each with a 2-D prefix-sum table. python m = [[3, 0, 1, 4], [5, 6, 3, 2]…
- Problem: A van with fixed capacity picks up and drops passengers; trip [n, from, to] carries n passengers from km 'from' to km 'to' (they…
- Problem: Start at index 0 and finish at the last index; from i you may jump to any of i+1..i+k. Maximise the sum of visited values…
- Problem: Move from the top-left to the bottom-right cell of a height grid (4 directions). The effort of a route is the largest absolute…