Skip to content

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

    Java / Big-O and Complexity

    Growth Rates and Big-O

    Big-O is about large n. For n — 10, an O(n²) algorithm with small constants can beat an O(n log n) one. Interview inputs are usually

    Chapter 2 of 115 min readOften asked in interviews

    The Plain Picture#

    Delivery charges: Company A charges ₹100 + ₹2 per km. Company B charges ₹10 × km². For a 3 km trip, B is cheaper (₹90 vs ₹106). For a 100 km trip, A charges ₹300 and B charges ₹1,00,000. For big distances, only the shape of the formula matters — the ₹100 base fee and the "×2" become irrelevant compared with "km" versus "km²".

    The Real Names#

    Big-O notation describes how the number of steps grows as n grows, keeping only the part that dominates for large n:

    1. Drop constant factors: 2n → n; n/2 → n; 100n → n.

    2. Drop lower-order terms: n² + 5n + 20 → n²; n + log n → n.

    So 1 + 2n steps is O(n), read "order n" or "big O of n".

    The formal definition (worth knowing once): f(n) is O(g(n)) if there are constants c > 0 and n₀ such that f(n) ≤ c · g(n) for every n ≥ n₀. In plain words: beyond some input size, f never grows faster than a constant multiple of g. Big-O is an upper bound on growth.

    Under the Hood — the complexity ladder#

    Memorise this table. The right-hand columns are rough step counts, and the last column uses ~10⁸ simple steps per second (measured in Ch 10).

    Big-ONameTypical examplen = 1,000n = 1,000,00010⁶ items at 10⁸ steps/s
    O(1)constantarr[i], HashMap.get (average)11instant
    O(log n)logarithmicbinary search1020instant
    O(n)linearone loop over the array1,00010⁶10 ms
    O(n log n)linearithmicgood sorting (Arrays.sort)10,0002 × 10⁷0.2 s
    O(n²)quadraticall pairs (nested loops)10⁶10¹²~3 hours
    O(n³)cubicall triples10⁹10¹⁸~300 years
    O(2ⁿ)exponentialall subsets10³⁰¹—never
    O(n!)factorialall orderings (permutations)——never
      steps
        │                                          n!   2ⁿ
        │                                     │   ╱
        │                                     │  ╱        n²
        │                                     │ ╱      ╱
        │                                     │╱    ╱
        │                                    ╱│  ╱
        │                                  ╱  │╱             n log n
        │                               ╱   ╱│          ___───
        │                           ╱   ╱    │  ___───        n
        │                      ╱   ╱ ___───────────────────────
        │                 ╱  ___───                         log n
        │         ___───────────────────────────────────────────
        │  ════════════════════════════════════════════════════ 1
        └──────────────────────────────────────────────────────────▶ n

    Part 0's measurements match the ladder: the pairs solution is O(n²) (doubling n → 4× time), sorting is O(n log n), and the HashSet one is O(n) (doubling n → 2× time).

    Gotchas

    • Big-O is about large n. For n = 10, an O(n²) algorithm with small constants can beat an O(n log n) one. Interview inputs are usually large, so Big-O wins.

    • "O(2n)" and "O(n + 5)" aren't wrong, but they aren't simplified. Always say O(n).

    • Don't drop terms for different inputs. O(n + m) where n and m are separate sizes stays O(n + m) (Ch 4).

    Check yourself

    4 questions
    1

    Simplify: 3n² + 10n + 7; n/2 + 100; 5; n + n log n.

    Show answer

    O(n²); O(n); O(1); O(n log n).

    2

    What does "f(n) is O(g(n))" mean in plain words?

    Show answer

    For large enough inputs, f grows no faster than some constant times g.

    3

    If an O(n²) program takes 1 second for n = 10,000, roughly how long for n = 40,000?

    Show answer

    16 seconds (4× the input → 4² = 16× the time).

    4

    Rank from fastest-growing to slowest: n, 2ⁿ, log n, n², n log n, 1.

    Show answer

    2ⁿ > n² > n log n > n > log n > 1.