Ch. 18 · Data Structures & Algorithms

Big-O Complexity and Input Growth

Big-O Complexity and Input Growth. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

Complexity describes how resource use grows with input size. State which variable represents input and distinguish worst-case bounds from amortized behavior.

Before you start

You should know arrays, loops and basic complexity notation. Start with a small input you can trace by hand. State what each variable means and which invariant is maintained, then use that invariant to explain correctness before discussing performance or writing a more compact solution.

The practical goal is to reason through this situation: A nested loop over two independent inputs costs O(nm), not automatically O(n squared). Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Define input variables

Use n and m for independently sized collections.

Step 2: Count actual iterations

Sequential work adds; nested work multiplies only when bounds justify it.

Step 3: State the model

Distinguish worst-case, expected and amortized claims.

Worked scenario

A nested loop over two independent inputs costs O(nm), not automatically O(n squared).

A loop visiting n users followed by m orders performs n+m visits. A loop visiting every order for every user performs nm visits. A triangular loop performs 1+2+…+n visits, still quadratic even though it does not execute exactly n squared iterations.

Common mistake

Counting loop syntax without following bounds produces incorrect estimates.

Verify the behavior

Double each input independently and predict how work changes for each loop shape.

Interview exercise

Analyze two separate sequential loops.

Answer and reasoning

Their work adds: O(n+m). Nested dependent loops require summing the actual iteration counts.

Continue learning

Compare the scenario with the Data Structures and Algorithms interview questions and test your understanding with the Data Structures and Algorithms MCQs. For terminology and implementation details, consult the reference material.

More in Data Structures & Algorithms

read ✓Data Structures & Algorithms · mid

Bit Manipulation Patterns for Interviews

Set, test and clear bits, exploit XOR properties, and count set bits with the Brian Kernighan trick.

~2 min readread →
read ✓Data Structures & Algorithms · hard

Backtracking and Safe Pruning Rules

Backtracking and Safe Pruning Rules. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
read ✓Data Structures & Algorithms · mid

Binary Search on an Answer Space

Binary Search on an Answer Space. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readread →
esc