Skip to interview prep
Apexed

How do you write and solve the recurrence for an algorithm that splits a problem into a subproblems of size n/b and does f(n) extra work?

MediumDivide & conquer

Technical

Content review

Report an error

Not yet reviewed

Getting your answer ready…

Show model answer

The recurrence is T(n) = a·T(n/b) + f(n). Compare f(n) with n^(log_b a), the work done at the leaves of the recursion tree. If f grows polynomially slower, the leaves dominate and T(n) = Θ(n^(log_b a)). If they match, every level costs the same and T(n) = Θ(n^(log_b a) · log n). If f grows polynomially faster (and a·f(n/b) ≤ c·f(n) for some c < 1), the root dominates and T(n) = Θ(f(n)).

Examples: binary search T(n) = T(n/2) + 1 gives Θ(log n); merge sort 2T(n/2) + n gives Θ(n log n); Karatsuba 3T(n/2) + n gives Θ(n^1.585); Strassen 7T(n/2) + n² gives Θ(n^2.807). Cases outside the basic theorem, such as 2T(n/2) + n log n = Θ(n log² n) or uneven splits like T(n/3) + T(2n/3) + n = Θ(n log n), are handled by drawing the recursion tree or with the Akra–Bazzi method.

What interviewers listen for

  • Compare f(n) with n^(log_b a)
  • Recursion tree when the master theorem does not apply
How do you write and solve the recurrence for an algorithm that splits a problem into a subproblems of size n/b and does f(n) extra work — Data structures & alg