Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Big-O and Complexity

    Quick Reference

    Glossary and cheat sheet for Big-O and Complexity — the page to reread before an interview.

    Extra: Reference5 min read

    Glossary#

    TermPlain meaningChapter
    input size (n)how big the input is1
    basic stepan operation whose cost doesn't depend on n1
    Big-Oupper bound on growth, ignoring constants and smaller terms2
    constant / log / linear / linearithmic / quadratic / exponential / factorial1 / log n / n / n log n / n² / 2ⁿ / n!2
    logarithm (log₂ n)how many halvings from n reach 13
    hidden loopa method call that itself loops (contains, substring, +)4
    time–space trade-offusing memory to save time (or the reverse)4
    worst / best / average casemax / min / expected steps for size n5
    Ω / Θlower bound / tight bound5
    space complexity / auxiliary spacememory growth / extra memory beyond input6
    in-placeO(1) auxiliary space6
    amortized costaverage per operation over a sequence7
    geometric growthcapacity multiplied by a factor on each resize7
    recursion tree / recurrencecalls drawn as a tree / cost written as a formula8
    memoizationcaching results of subproblems8
    divide and conquersplit, solve parts recursively, combine8
    Master Theoremgeneral solution for divide-and-conquer recurrences8
    constraintsstated limits on input size10
    JIT warm-up / JMHearly runs are slower / Java's benchmarking tool11

    Cheat Sheet#

    Ladder (n = 10⁶)

    Output
      O(1) 1 · O(log n) 20 · O(n) 10⁶ · O(n log n) 2×10⁷ · O(n²) 10¹² ✗ · O(2ⁿ) ✗ · O(n!) ✗
      doubling n: O(n) ×2 · O(n log n) a bit over ×2 · O(n²) ×4 · O(n³) ×8

    Rules

    Output
      sequence → add, keep the biggest     nested → multiply     i..n inner loop → n²/2 → O(n²)
      halve/double each round → log n     separate inputs → O(n + m) / O(n · m)
      count every call's own cost          early return ≠ better worst case

    Recurrences

    Output
      T(n/2)+1 → log n      T(n−1)+1 → n        T(n−1)+n → n²
      2T(n/2)+n → n log n   2T(n/2)+1 → n       2T(n−1)+1 → 2ⁿ        b branches, depth d → bᵈ

    Constraints → allowed complexity (≈10⁸ ops/s)

    Output
      ≤ 10: n! · ≤ 20: 2ⁿ · ≤ 500: n³ · ≤ 5,000: n² · ≤ 10⁶: n log n / n · ≤ 10⁸: n · huge: log n / 1

    Java costs to never forget

    Output
      ArrayList add-at-end amortized O(1) · add(0)/remove(0) O(n) · contains O(n)
      HashMap/HashSet O(1) avg · TreeMap/TreeSet O(log n) · PriorityQueue offer/poll O(log n), peek O(1)
      ArrayDeque both ends O(1) · String + / substring / equals O(n) · sort O(n log n)

    The answer template

    Output
      "Time O(__) because __ dominates. Space O(__) for __. (Average case because of hashing.)
       Trade-off: could get O(__) time with O(__) space by __."

    Try This Yourself#

    1. Reproduce Part 0. Write the three duplicate checkers and time them for n = 10k, 20k, 40k, 80k (run each a few times first to warm up the JIT). Check the ratios: ×4 for the pairs version, about ×2 for the others.

    2. Analyse without running. For each loop in Chapter 4's Check Yourself, add a counter, run it for n = 1,000 and 2,000, and confirm the growth matches your answer.

    3. Feel the explosion. Time naïve fib(n) for n = 30, 35, 40, 42. Predict each time from the previous before running. Then write the bottom-up O(1)-space version and time fib(90).

    4. Constraint drill. Open any 5 LeetCode problems, read only the constraints, and write down the complexity you'd target before reading the problem statement.