skip to content

questions

4

Using the master theorem, what does T(n) = T(n/2) + O(1) solve to, and why isn't it linear?

level: juniorimportance: must knowfreq 62%

answer

  1. How many subproblems does each call spawn?
  2. a = 1, so the leaves never multiply
  3. Compare n^(log_b a) against f(n)
  4. n^0 = 1 matches constant per-call work
  5. The balanced case adds one log factor

basics

~20 s

T(n) = T(n/2) + O(1) solves to Θ(log n). Each call spawns one subproblem of half the size and does constant work, so the total is the number of halvings that fit in n, not n itself.

solid answer

~40 s

Match it to the form `T(n) = a·T(n/b) + f(n)`: here a = 1 subproblem, b = 2 (the input is divided by two), and f(n) = Θ(1) is the work outside the recursive call. The master theorem's watershed is `n^(log_b a)` = `n^(log_2 1)` = `n^0` = 1, and f(n) = Θ(1) matches it exactly, which is case 2 — the balanced case — so the answer picks up one log factor: Θ(log n). The intuition is that there is exactly one call per level, each costing Θ(1), and halving n bottoms out after about log₂ n levels. It is not linear because the recursion never branches: a call discards half the input rather than walking it.

go deeper

for a junior

Be ready to name a, b and f(n) in a given recurrence and read off the answer for the halving-with-constant-work shape. Say Θ(log n) and explain it as the number of halvings, not as half the work.

for a middle

Explain the watershed n^(log_b a) and why constant work matching n^0 lands in the balanced case. Show what changes when a goes from 1 to 2, and why f(n) is untouched in that comparison.

for a senior

Demonstrate that you read recurrences off real routines rather than recalling results, and that you report stack depth alongside time. Note when the stated bound is O rather than Θ and answer accordingly.

for a principal

Own the framing that constants, not exponents, decide most production choices at these sizes: a Θ(log n) routine with a cache-hostile access pattern can lose to a Θ(n) scan over a small contiguous region, and you should be able to say when.

## The form the theorem expects The master theorem solves recurrences shaped like ``` T(n) = a·T(n/b) + f(n) ``` where **a ≥ 1** is the number of subproblems each call spawns, **b > 1** is the factor the input size is *divided* by for each subproblem, and **f(n)** is all the non-recursive work a single call does — the splitting before the calls plus the combining after them. Both a and b must be constants; f(n) must be asymptotically positive. Reading a recurrence off a routine is a two-question exercise: *how many times does it call itself*, and *how big is each of those calls' inputs relative to n*. For a routine that inspects one position, decides, and then recurses on half of what is left, the answers are "once" and "n/2", giving a = 1 and b = 2. The remaining work — computing the midpoint, one comparison — does not grow with n, so f(n) = Θ(1). ## The comparison the theorem makes Everything hinges on one quantity, often called the watershed: ``` n^(log_b a) ``` This is the total cost of all the leaves of the recursion — the number of base-case calls. The theorem compares it against f(n), the cost at the top: | relation | case | result | |---|---|---| | f(n) polynomially smaller than n^(log_b a) | 1 | Θ(n^(log_b a)) — leaves dominate | | f(n) = Θ(n^(log_b a)) | 2 | Θ(n^(log_b a) · log n) — every level costs the same | | f(n) polynomially larger (plus a regularity condition) | 3 | Θ(f(n)) — the root dominates | For a = 1 and b = 2: log_2 1 = 0, so the watershed is n^0 = 1. And f(n) = Θ(1) = Θ(n^0). The two sides are equal, so this is **case 2**, and the result is Θ(n^0 · log n) = **Θ(log n)**. ## Why the answer is not Θ(n) The most common wrong answer is "you are halving n, and half of n is still proportional to n, so it is linear." That confuses *the size of the remaining input* with *the work performed*. Nothing here touches n/2 items; the call hands off a smaller region and does constant work itself. Sum the work down the chain: 1 + 1 + 1 + … once per level, and the levels stop when n/2^k reaches the base case, i.e. after about log₂ n levels. The sum is Θ(log n). The mirror-image wrong answer is "each call is O(1), so the whole thing is O(1)." f(n) is the cost of **one** call, never of the recursion. The recurrence exists precisely because you must add up all the calls. ## What a single parameter change does The same shape with a different a is a completely different function, which is why the theorem is worth internalising rather than memorising results: | recurrence | watershed | case | result | |---|---|---|---| | T(n) = T(n/2) + Θ(1) | n^0 = 1 | 2 | Θ(log n) | | T(n) = 2T(n/2) + Θ(1) | n^1 = n | 1 | Θ(n) | | T(n) = 2T(n/3) + Θ(1) | n^(log_3 2) ≈ n^0.63 | 1 | Θ(n^0.63) | Going from one recursive call to two turns a logarithm into a linear count, because the recursion tree now has about n leaves instead of one call per level. Going from halves to thirds while keeping two calls lands between the two. Nothing about f(n) changed in any of these rows — the exponent in a leaf-dominated recurrence is fixed by a and b alone. ## Details worth being precise about - **Floors and ceilings do not matter.** Real inputs give ⌊n/2⌋ or ⌈n/2⌉; the master theorem is stated so that the asymptotic answer is unaffected. Do not spend interview time on them. - **The base of the logarithm does not matter asymptotically.** log₂ n and log₁₀ n differ by a constant factor, so Θ(log n) needs no base. The base *does* set the constant, which is why dividing by 3 instead of 2 is a real but constant-factor win. - **Recursion depth is space.** A chain of Θ(log n) calls costs Θ(log n) stack frames unless the call is the last thing the routine does and the compiler eliminates it. The time answer does not report that; you must. - **Θ versus O.** If the per-call work is stated as O(1) rather than Θ(1) — an upper bound only — the honest conclusion is O(log n). Interviewers rarely press on this, but stating the bound you actually derived is a mark of care.

  • What changes if the recurrence becomes T(n) = 2T(n/2) + O(1)?
    The watershed becomes n^(log_2 2) = n, and constant per-call work is polynomially smaller than n, so it is case 1: Θ(n). Two calls per level means the recursion tree has about n leaves, and the leaves now carry essentially all the cost. Doubling the branching factor turned a logarithm into a linear count without changing f(n) at all.
  • Two recursive calls, each on a third of the input, with constant extra work — what does the master theorem give?
    That is T(n) = 2T(n/3) + Θ(1). The watershed is n^(log_3 2), roughly n^0.63, and Θ(1) is polynomially smaller, so case 1 gives Θ(n^(log_3 2)). It is sublinear but far worse than logarithmic — a useful reminder that the exponent comes from the pair (a, b) jointly, not from either one alone.
  • Does it matter that a real input gives a floor or a ceiling rather than an exact n/2?
    No. The master theorem is stated to cover ⌊n/b⌋ and ⌈n/b⌉ subproblem sizes, and the asymptotic answer is identical. Rounding shifts the recursion by at most one level, which is a constant. It is worth saying out loud once so the interviewer knows you considered it, then moving on.

Halving a stack of pages until one is left is a counting job, not a reading job: you never touch the pages you discard, so the cost is the number of splits, not the number of pages.

saying these in an interview costs you the question

  • Answers O(n) because half of n is still proportional to n
  • Claims constant work per call makes the whole recursion O(1)
  • Reads b as the number of elements removed per call
  • Thinks the logarithm's base changes the complexity class
  • Ignores that the call chain also costs stack space

context

open as a page

Which master-theorem case fits T(n)=2T(n/2)+O(n^2), and which fits T(n)=4T(n/2)+O(n)?

level: middleimportance: must knowfreq 55%

basics

~20 s

The first is case 3: watershed n^(log_2 2) = n loses to f(n) = n^2, so the root dominates. The second is case 1: watershed n^(log_2 4) = n^2 beats f(n) = n, so the leaves do. Both give Θ(n^2).

open as a page

Why can't the master theorem solve T(n) = T(n-1) + O(n), and what does?

level: middleimportance: should knowfreq 46%

basics

~20 s

The master theorem covers only T(n) = a·T(n/b) + f(n), where a subproblem is n divided by a constant b above 1. Subtracting one leaves no such b, so no case applies. Summing the per-call work gives Θ(n^2).

open as a page

For an arbitrary-precision multiply with T(n)=3T(n/2)+O(n), what does the master theorem give and when does it actually beat the quadratic method?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

It solves to Θ(n^(log_2 3)), about Θ(n^1.585), by case 1: the watershed beats the linear combine, so the leaves dominate. It outruns the quadratic schoolbook method only above a crossover size, because its constants, temporaries and call overhead are far larger.

open as a page