TExcelTAD

Exercises

Fifteen problems covering the whole course, from correctness proofs to probabilistic analysis. Try each one on paper first. Every problem has a short Answer to check yourself against and a full Solution; some solutions include a program that confirms the result by counting, which you can edit and run.

Tip: When a problem asks for a bound or a count, test the smallest cases by hand (n=1,2,3n = 1, 2, 3) before you generalise. Most wrong formulas are already wrong at n=2n = 2.

# Exercise Practises
1 An invariant for FIND-MAX loop invariants
2 Linear search that may fail invariants with an early exit
3 Counting 1-bits recursively induction on a recursive procedure
4 Three nested loops exact step counting
5 Bubble sort with an early exit best and worst case
6 A Θ\Theta proof with constants the definition of Θ\Theta
7 True or false OO, Ω\Omega, oo and exponentials
8 Order these functions comparing growth rates
9 From code to recurrence writing and solving a recurrence
10 Four master-theorem problems the master theorem
11 Substitution with a lower-order term the substitution method
12 How often does the maximum change? indicator random variables
13 A shuffle that looks fine random permutations
14 Colliding hash values the birthday paradox
15 Rolling every face the coupon collector

Exercise 1: An invariant for FIND-MAX

FIND-MAX from the Introduction sets best = a[0] and then, for i=1i = 1 to n−1n - 1, replaces best by a[i]a[i] when a[i]a[i] is larger. State a loop invariant and prove the procedure correct.

Answer

At the start of the iteration for ii, best =max⁡(a[0:i])= \max(a[0:i]).

Solution

Initialization: before i=1i = 1, best =a[0]=max⁡(a[0:1])= a[0] = \max(a[0:1]). Maintenance: if best =max⁡(a[0:i])= \max(a[0:i]), the body sets best to the larger of that and a[i]a[i], which is max⁡(a[0:i+1])\max(a[0:i+1]). Termination: the loop ends with i=ni = n, so best =max⁡(a[0:n])= \max(a[0:n]), the maximum of the whole array. The loop runs n−1n - 1 times, so it terminates.

Exercise 2: Linear search that may fail

LINEAR-SEARCH(a, x) checks a[0],a[1],…a[0], a[1], \ldots in turn, returns the first index ii with a[i]=xa[i] = x, and returns −1-1 if the loop finishes. Give an invariant that proves both possible results correct.

Answer

At the start of the iteration for ii: xx does not occur in a[0:i]a[0:i].

Solution

Initially a[0:0]a[0:0] is empty, so xx does not occur in it. If the iteration for ii does not return, then a[i]≠xa[i] \ne x, so xx does not occur in a[0:i+1]a[0:i+1] either. If it returns ii, then a[i]=xa[i] = x and by the invariant no earlier position holds xx, so ii is the first occurrence. If the loop ends normally, i=ni = n and the invariant says xx does not occur in a[0:n]a[0:n], so −1-1 is correct.

Exercise 3: Counting 1-bits recursively

ONES(n) returns 00 if n=0n = 0, and otherwise $(n \bmod 2) + $ ONES(⌊n/2⌋)(\lfloor n/2 \rfloor). Prove by induction that it returns the number of 1-bits in the binary representation of n≥0n \ge 0, and count its calls.

Answer

Correct by strong induction on nn; for n≥1n \ge 1 it makes ⌊lg⁡n⌋+2\lfloor \lg n \rfloor + 2 calls in total (one per bit, plus the final call with 00).

Solution

Base case: 00 has no 1-bits. For n≥1n \ge 1, the lowest bit of nn is n mod 2n \bmod 2, and the remaining bits form ⌊n/2⌋<n\lfloor n/2 \rfloor < n, which by the induction hypothesis ONES counts correctly. The sum is the number of 1-bits of nn. The arguments n,⌊n/2⌋,…,1n, \lfloor n/2 \rfloor, \ldots, 1 are one per binary digit, ⌊lg⁡n⌋+1\lfloor \lg n \rfloor + 1 of them, and then one more call with 00. See Proving Recursive Algorithms Correct by Induction.

In C++, the program below checks ONES against std::popcount and counts the calls:

cpp · try it yourself
#include <bit>
#include <cstdio>
#include <initializer_list>

int ones(unsigned n, int& calls) {
    calls++;
    if (n == 0) return 0;
    return n % 2 + ones(n / 2, calls);
}

int main() {
    for (unsigned n : {1u, 2u, 7u, 8u, 100u, 1000u, 4294967295u}) {
        int calls = 0;
        int result = ones(n, calls);
        // std::popcount counts the 1-bits directly; std::bit_width(n) is floor(lg n) + 1
        std::printf("n = %10u: ONES = %2d, popcount = %2d, calls = %2d, floor(lg n) + 2 = %2d\n",
                    n, result, std::popcount(n), calls, (int)std::bit_width(n) + 1);
    }
}
n =          1: ONES =  1, popcount =  1, calls =  2, floor(lg n) + 2 =  2
n =          2: ONES =  1, popcount =  1, calls =  3, floor(lg n) + 2 =  3
n =          7: ONES =  3, popcount =  3, calls =  4, floor(lg n) + 2 =  4
n =          8: ONES =  1, popcount =  1, calls =  5, floor(lg n) + 2 =  5
n =        100: ONES =  3, popcount =  3, calls =  8, floor(lg n) + 2 =  8
n =       1000: ONES =  6, popcount =  6, calls = 11, floor(lg n) + 2 = 11
n = 4294967295: ONES = 32, popcount = 32, calls = 33, floor(lg n) + 2 = 33

Exercise 4: Three nested loops

How many times does the innermost line run, as an exact function of nn?

text
1  for i = 0 to n - 1
2      for j = i + 1 to n - 1
3          for k = j + 1 to n - 1
4              count = count + 1
Answer

(n3)=n(n−1)(n−2)6\binom{n}{3} = \frac{n(n-1)(n-2)}{6}, which is Θ(n3)\Theta(n^3).

Solution

Each execution of line 4 corresponds to exactly one triple i<j<ki < j < k of indices from {0,…,n−1}\{0, \ldots, n-1\}, and every such triple occurs once. The number of 3-element subsets of an nn-element set is (n3)\binom{n}{3}. The program confirms it.

python · try it yourself
from math import comb

def triple_count(n):
    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                count += 1
    return count

for n in [0, 1, 2, 3, 4, 10, 30]:
    print(f"n = {n:>2}: counted {triple_count(n):>5}, n(n-1)(n-2)/6 = {n * (n - 1) * (n - 2) // 6:>5}, C(n, 3) = {comb(n, 3)}")
n =  0: counted     0, n(n-1)(n-2)/6 =     0, C(n, 3) = 0
n =  1: counted     0, n(n-1)(n-2)/6 =     0, C(n, 3) = 0
n =  2: counted     0, n(n-1)(n-2)/6 =     0, C(n, 3) = 0
n =  3: counted     1, n(n-1)(n-2)/6 =     1, C(n, 3) = 1
n =  4: counted     4, n(n-1)(n-2)/6 =     4, C(n, 3) = 4
n = 10: counted   120, n(n-1)(n-2)/6 =   120, C(n, 3) = 120
n = 30: counted  4060, n(n-1)(n-2)/6 =  4060, C(n, 3) = 4060

Exercise 5: Bubble sort with an early exit

Bubble sort makes passes over the array, swapping adjacent out-of-order pairs; pass pp (for p=0,1,…p = 0, 1, \ldots) compares positions 00 to n−2−pn - 2 - p, and the algorithm stops after the first pass that makes no swap. Count the comparisons in the best and worst case.

Answer

Best case (already sorted): n−1n - 1 comparisons. Worst case (reversed): n(n−1)2\frac{n(n-1)}{2}.

Solution

On sorted input the first pass makes n−1n - 1 comparisons and no swaps, so the algorithm stops. On reversed input every pass swaps, so all n−1n - 1 passes run, with n−1,n−2,…,1n - 1, n - 2, \ldots, 1 comparisons: n(n−1)2\frac{n(n-1)}{2} in total. The best case is Θ(n)\Theta(n) and the worst Θ(n2)\Theta(n^2), like insertion sort; see Worst, Best and Average Case.

Exercise 6: A Theta proof with constants

Prove 3n2−10n+5=Θ(n2)3n^2 - 10n + 5 = \Theta(n^2) by giving explicit constants c1,c2>0c_1, c_2 > 0 and n0n_0.

Answer

c1=2c_1 = 2, c2=3c_2 = 3, n0=10n_0 = 10.

Solution

Upper bound: for n≥1n \ge 1, −10n+5<0-10n + 5 < 0, so 3n2−10n+5≤3n23n^2 - 10n + 5 \le 3n^2. Lower bound: 3n2−10n+5≥2n23n^2 - 10n + 5 \ge 2n^2 is equivalent to n2−10n+5≥0n^2 - 10n + 5 \ge 0, which holds for n≥10n \ge 10 (at n=10n = 10 the left side is 55, and it increases from there), but fails at n=9n = 9. So 2n2≤3n2−10n+5≤3n22n^2 \le 3n^2 - 10n + 5 \le 3n^2 for all n≥10n \ge 10. See O, Omega and Theta.

Exercise 7: True or false

(a) 2n+1=O(2n)2^{n+1} = O(2^n). (b) 22n=O(2n)2^{2n} = O(2^n). (c) lg⁡(n3)=Θ(lg⁡n)\lg(n^3) = \Theta(\lg n). (d) n=o(nlg⁡n)n = o(n \lg n). (e) n2=Ω(n2lg⁡n)n^2 = \Omega(n^2 \lg n).

Answer

(a) true, (b) false, (c) true, (d) true, (e) false.

Solution

(a) 2n+1=2⋅2n2^{n+1} = 2 \cdot 2^n, a constant multiple. (b) 22n/2n=2n→∞2^{2n} / 2^n = 2^n \to \infty, so no constant bounds the ratio. (c) lg⁡(n3)=3lg⁡n\lg(n^3) = 3 \lg n. (d) n/(nlg⁡n)=1/lg⁡n→0n / (n \lg n) = 1 / \lg n \to 0. (e) n2/(n2lg⁡n)=1/lg⁡n→0n^2 / (n^2 \lg n) = 1/\lg n \to 0, so n2n^2 is o(n2lg⁡n)o(n^2 \lg n), which rules out Ω\Omega. See Little o, Little omega and Limits.

Exercise 8: Order these functions

Sort by order of growth, slowest first: nlg⁡nn \lg n, 2n2^n, n\sqrt{n}, (lg⁡n)5(\lg n)^5, n!n!, lg⁡∗n\lg^* n, n1.01n^{1.01}, n/lg⁡nn / \lg n, nlg⁡lg⁡nn^{\lg \lg n}.

Answer

lg⁡∗n\lg^* n, (lg⁡n)5(\lg n)^5, n\sqrt{n}, n/lg⁡nn / \lg n, nlg⁡nn \lg n, n1.01n^{1.01}, nlg⁡lg⁡nn^{\lg \lg n}, 2n2^n, n!n!.

Solution

The iterated logarithm is the slowest-growing function here. Any power of lg⁡n\lg n is o(nϵ)o(n^\epsilon) for every ϵ>0\epsilon > 0, so (lg⁡n)5=o(n)(\lg n)^5 = o(\sqrt{n}), and for the same reason nlg⁡n=o(n1.01)n \lg n = o(n^{1.01}). Writing nlg⁡lg⁡n=2lg⁡n⋅lg⁡lg⁡nn^{\lg \lg n} = 2^{\lg n \cdot \lg \lg n} shows it is superpolynomial but o(2n)o(2^n), since lg⁡nlg⁡lg⁡n=o(n)\lg n \lg \lg n = o(n). Finally 2n=o(n!)2^n = o(n!). See Comparing Growth Rates.

Exercise 9: From code to recurrence

The procedure below does nn units of work and then recurses on three halves of its input (the first half, the second half and the middle half). Write its recurrence and solve it.

text
WORK(a)                            // n = length of a
1  if n <= 1
2      return
3  scan all n elements once        // n units of work
4  h = floor(n / 2), q = floor(n / 4)
5  WORK(a[0:h])
6  WORK(a[h:n])
7  WORK(a[q:q + h])
Answer

T(n)=3T(n/2)+nT(n) = 3T(n/2) + n, so T(n)=Θ(nlg⁡3)≈Θ(n1.585)T(n) = \Theta(n^{\lg 3}) \approx \Theta(n^{1.585}). Charging one unit for a call that stops at once, T(1)=1T(1) = 1, and for nn a power of 2 the exact solution is T(n)=3nlg⁡3−2nT(n) = 3n^{\lg 3} - 2n.

Solution

Three subproblems of size n/2n/2 and linear extra work give a=3a = 3, b=2b = 2, f(n)=nf(n) = n. Since nlog⁡23≈n1.585n^{\log_2 3} \approx n^{1.585} grows polynomially faster than f(n)=nf(n) = n, case 1 of the master theorem gives Θ(nlg⁡3)\Theta(n^{\lg 3}). For n=2kn = 2^k, nlg⁡3=3kn^{\lg 3} = 3^k, and the exact formula 3⋅3k−2⋅2k3 \cdot 3^k - 2 \cdot 2^k satisfies the recurrence: 3(3⋅3k−1−2⋅2k−1)+2k=3⋅3k−2⋅2k3(3 \cdot 3^{k-1} - 2 \cdot 2^{k-1}) + 2^k = 3 \cdot 3^k - 2 \cdot 2^k. The program counts the work.

python · try it yourself
def work(a):
    n = len(a)
    if n <= 1:
        return 1
    total = n                              # one pass over the input
    h, q = n // 2, n // 4
    total += work(a[:h]) + work(a[h:]) + work(a[q:q + h])
    return total

for k in range(0, 11, 2):
    n = 2 ** k
    print(f"n = {n:>5}: counted {work(list(range(n))):>7}, formula 3*3^k - 2n = {3 * 3**k - 2 * n:>7}")
n =     1: counted       1, formula 3*3^k - 2n =       1
n =     4: counted      19, formula 3*3^k - 2n =      19
n =    16: counted     211, formula 3*3^k - 2n =     211
n =    64: counted    2059, formula 3*3^k - 2n =    2059
n =   256: counted   19171, formula 3*3^k - 2n =   19171
n =  1024: counted  175099, formula 3*3^k - 2n =  175099

In C++, the three sub-arrays can be std::span views, so the recursion copies nothing:

cpp · try it yourself
#include <cstdio>
#include <numeric>
#include <span>
#include <vector>

// a span is a view of part of an array: the three recursive calls copy nothing
long work(std::span<const int> a) {
    long n = (long)a.size();
    if (n <= 1) return 1;
    long total = n;                                  // one pass over the input
    std::size_t h = n / 2, q = n / 4;
    total += work(a.first(h)) + work(a.subspan(h)) + work(a.subspan(q, h));
    return total;
}

int main() {
    long pow3 = 1;                                   // 3^k
    for (int k = 0; k <= 10; k += 2, pow3 *= 9) {
        long n = 1L << k;
        std::vector<int> a(n);
        std::iota(a.begin(), a.end(), 0);
        std::printf("n = %5ld: counted %7ld, formula 3*3^k - 2n = %7ld\n",
                    n, work(a), 3 * pow3 - 2 * n);
    }
}
n =     1: counted       1, formula 3*3^k - 2n =       1
n =     4: counted      19, formula 3*3^k - 2n =      19
n =    16: counted     211, formula 3*3^k - 2n =     211
n =    64: counted    2059, formula 3*3^k - 2n =    2059
n =   256: counted   19171, formula 3*3^k - 2n =   19171
n =  1024: counted  175099, formula 3*3^k - 2n =  175099

Exercise 10: Four master-theorem problems

Solve: (a) T(n)=4T(n/2)+n2T(n) = 4T(n/2) + n^2, (b) T(n)=2T(n/4)+nT(n) = 2T(n/4) + \sqrt{n}, (c) T(n)=7T(n/3)+n2T(n) = 7T(n/3) + n^2, (d) T(n)=2T(n/2)+nlg⁡nT(n) = 2T(n/2) + n \lg n.

Answer

(a) Θ(n2lg⁡n)\Theta(n^2 \lg n), (b) Θ(nlg⁡n)\Theta(\sqrt{n} \lg n), (c) Θ(n2)\Theta(n^2), (d) Θ(nlg⁡2n)\Theta(n \lg^2 n).

Solution

(a) nlog⁡24=n2=f(n)n^{\log_2 4} = n^2 = f(n): case 2. (b) nlog⁡42=n1/2=f(n)n^{\log_4 2} = n^{1/2} = f(n): case 2. (c) nlog⁡37≈n1.771n^{\log_3 7} \approx n^{1.771} is polynomially smaller than n2n^2, and the regularity condition holds because 7(n/3)2=79n27 (n/3)^2 = \frac{7}{9} n^2 with 79<1\frac{7}{9} < 1: case 3. (d) nlog⁡22=nn^{\log_2 2} = n and f(n)=nlg⁡n=Θ(nlog⁡balg⁡1n)f(n) = n \lg n = \Theta(n^{\log_b a} \lg^1 n): the extended case 2 with k=1k = 1 gives Θ(nlg⁡2n)\Theta(n \lg^2 n). Note that (d) is not case 3, because nlg⁡nn \lg n is not polynomially larger than nn.

Exercise 11: Substitution with a lower-order term

For nn a power of 2, T(1)=1T(1) = 1 and T(n)=4T(n/2)+nT(n) = 4T(n/2) + n. Show that the guess T(n)≤cn2T(n) \le cn^2 fails in the inductive step, and fix it.

Answer

Use T(n)≤cn2−dnT(n) \le cn^2 - dn with d≥1d \ge 1; c=2c = 2, d=1d = 1 works, and in fact T(n)=2n2−nT(n) = 2n^2 - n exactly.

Solution

With T(n/2)≤c(n/2)2T(n/2) \le c(n/2)^2 the step gives T(n)≤cn2+nT(n) \le cn^2 + n, which is not ≤cn2\le cn^2 for any cc: the induction does not close, even though the bound is true. Subtract a lower-order term: assuming T(n/2)≤c(n/2)2−d(n/2)T(n/2) \le c(n/2)^2 - d(n/2),

T(n)≤4(cn24−dn2)+n=cn2−2dn+n=cn2−dn−(d−1)n≤cn2−dnT(n) \le 4\left(\frac{cn^2}{4} - \frac{dn}{2}\right) + n = cn^2 - 2dn + n = cn^2 - dn - (d - 1)n \le cn^2 - dn

whenever d≥1d \ge 1. The base case needs 1≤c−d1 \le c - d, so c=2c = 2, d=1d = 1. See The Substitution Method.

Exercise 12: How often does the maximum change?

Run FIND-MAX on a uniformly random permutation of nn distinct values. What is the expected number of times the line best = a[i] executes?

Answer

Hn−1=12+13+⋯+1nH_n - 1 = \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n}, which is about ln⁡n\ln n.

Solution

Let XiX_i be the indicator that a[i]a[i] is larger than everything before it, for i=1,…,n−1i = 1, \ldots, n - 1. In a random permutation each of the first i+1i + 1 values is equally likely to be the largest of them, so E[Xi]=1i+1\mathrm{E}[X_i] = \frac{1}{i+1}. By linearity, the expected count is ∑i=1n−11i+1=Hn−1\sum_{i=1}^{n-1} \frac{1}{i+1} = H_n - 1. This is the hiring problem in disguise. The program checks it over every permutation.

python · try it yourself
from itertools import permutations
from fractions import Fraction

def updates(a):
    best, count = a[0], 0
    for x in a[1:]:
        if x > best:
            best, count = x, count + 1
    return count

for n in range(1, 8):
    perms = list(permutations(range(n)))
    avg = Fraction(sum(updates(p) for p in perms), len(perms))
    h = sum(Fraction(1, k) for k in range(1, n + 1))
    print(f"n = {n}: average {str(avg):>7}   H_n - 1 = {str(h - 1):>7}")
n = 1: average       0   H_n - 1 =       0
n = 2: average     1/2   H_n - 1 =     1/2
n = 3: average     5/6   H_n - 1 =     5/6
n = 4: average   13/12   H_n - 1 =   13/12
n = 5: average   77/60   H_n - 1 =   77/60
n = 6: average   29/20   H_n - 1 =   29/20
n = 7: average 223/140   H_n - 1 = 223/140

Exercise 13: A shuffle that looks fine

A programmer shuffles by swapping a[i]a[i] with a[j]a[j] for a uniformly random jj in {0,…,n−1}\{0, \ldots, n-1\}, for each i=0,…,n−1i = 0, \ldots, n - 1. Is every permutation equally likely? Decide without simulation.

Answer

No. The procedure makes nnn^n equally likely sequences of choices, and for n≥3n \ge 3 the number nnn^n is not divisible by n!n!, so the n!n! permutations cannot each receive the same share.

Solution

For n=3n = 3 there are 2727 choice sequences but 66 permutations, and 27/627 / 6 is not an integer. Some permutation must get more sequences than another. Restricting jj to {i,…,n−1}\{i, \ldots, n-1\} gives n!n! sequences, one per permutation, which is the correct Fisher-Yates shuffle from Random Permutations. The program counts the outcomes of all 27 sequences exactly.

python · try it yourself
from itertools import product
from collections import Counter

n = 3
outcomes = Counter()
for choices in product(range(n), repeat=n):
    a = list("ABC")
    for i, j in enumerate(choices):
        a[i], a[j] = a[j], a[i]
    outcomes["".join(a)] += 1
for perm, count in sorted(outcomes.items()):
    print(perm, f"{count}/27")
ABC 4/27
ACB 5/27
BAC 5/27
BCA 5/27
CAB 4/27
CBA 4/27

In C++, each of the 27 choice sequences is read off the base-3 digits of a number from 0 to 26:

cpp · try it yourself
#include <cstdio>
#include <map>
#include <string>
#include <utility>

int main() {
    const int n = 3;
    std::map<std::string, int> outcomes;
    // every choice sequence (j_0, j_1, j_2) with each j in 0..2: 27 of them
    for (int code = 0; code < 27; code++) {
        std::string a = "ABC";
        int c = code;
        for (int i = 0; i < n; i++) {
            int j = c % n;                 // the i-th digit of code in base 3
            c /= n;
            std::swap(a[i], a[j]);
        }
        outcomes[a]++;
    }
    for (const auto& [perm, count] : outcomes)
        std::printf("%s %d/27\n", perm.c_str(), count);
}
ABC 4/27
ACB 5/27
BAC 5/27
BCA 5/27
CAB 4/27
CBA 4/27

Exercise 14: Colliding hash values

A hash function produces uniformly random 32-bit values. About how many values must you generate before the probability that two are equal reaches 12\frac{1}{2}?

Answer

About 2ln⁡2⋅232≈77 163\sqrt{2 \ln 2 \cdot 2^{32}} \approx 77\,163; the exact threshold is 77 16477\,164 values.

Solution

With N=232N = 2^{32} possible values and kk draws, the probability of no collision is ∏i=0k−1(1−i/N)≈e−k(k−1)/(2N)\prod_{i=0}^{k-1} (1 - i/N) \approx e^{-k(k-1)/(2N)}. Setting this to 12\frac{1}{2} gives k≈2Nln⁡2≈1.1774Nk \approx \sqrt{2N \ln 2} \approx 1.1774 \sqrt{N}, about 77,163. Only around 2162^{16} values, not 2312^{31}, are needed. See The Birthday Paradox.

Exercise 15: Rolling every face

What is the expected number of rolls of a fair six-sided die until every face has appeared at least once?

Answer

6H6=6(1+12+⋯+16)=14.76 H_6 = 6\left(1 + \frac{1}{2} + \cdots + \frac{1}{6}\right) = 14.7.

Solution

Split the rolls into phases: phase kk starts when kk faces have been seen and ends at the next new face. A roll gives a new face with probability 6−k6\frac{6-k}{6}, so phase kk lasts 66−k\frac{6}{6-k} rolls on average (a geometric distribution). Summing for k=0,…,5k = 0, \ldots, 5 gives 6(16+15+⋯+1)=6H6=147106\left(\frac{1}{6} + \frac{1}{5} + \cdots + 1\right) = 6 H_6 = \frac{147}{10}. A seeded simulation agrees.

python · try it yourself
import random
from fractions import Fraction

exact = 6 * sum(Fraction(1, k) for k in range(1, 7))
rng = random.Random(7)
trials, total = 100_000, 0
for _ in range(trials):
    seen, rolls = set(), 0
    while len(seen) < 6:
        seen.add(rng.randint(1, 6))
        rolls += 1
    total += rolls
print(f"exact: {exact} = {float(exact)}")
print(f"simulated over {trials:,} trials: {total / trials:.3f}")
exact: 147/10 = 14.7
simulated over 100,000 trials: 14.701

The same argument gives nHn≈nln⁡nn H_n \approx n \ln n for nn faces; see Balls into Bins.

Test yourself

Which constants prove 3n2−10n+5=Θ(n2)3n^2 - 10n + 5 = \Theta(n^2)?

  1. c1=3c_1 = 3, c2=3c_2 = 3, n0=1n_0 = 1
  2. c1=2c_1 = 2, c2=3c_2 = 3, n0=10n_0 = 10
  3. c1=2c_1 = 2, c2=3c_2 = 3, n0=1n_0 = 1
  4. No constants exist

Why is T(n)=2T(n/2)+nlg⁡nT(n) = 2T(n/2) + n \lg n not covered by case 3 of the master theorem?

  1. Because a=ba = b
  2. Because nlg⁡nn \lg n is larger than nn but not by a polynomial factor nϵn^{\epsilon}
  3. Because the regularity condition always fails
  4. Because f(n)f(n) must be a polynomial

In the naive shuffle with jj uniform in {0,…,n−1}\{0, \ldots, n-1\}, why can the result not be uniform for n=3n = 3?

  1. Because it makes too few swaps
  2. Because 33=273^3 = 27 equally likely choice sequences cannot be split evenly among 3!=63! = 6 permutations
  3. Because the random generator is seeded
  4. Because a swap of an element with itself is not allowed

What is the expected number of rolls of a fair die until all six faces have appeared?

  1. 6
  2. 6ln⁡6≈10.756 \ln 6 \approx 10.75
  3. 6H6=14.76 H_6 = 14.7
  4. 36