Java Notes
Master time and space complexity — Big O notation, analyzing algorithms, common complexity classes, amortized analysis, and practical comparison with Java examples.
What is Time Complexity?
Time complexity describes how the runtime of an algorithm grows as the input size increases. It's not about exact time — it's about the rate of growth.
| Input size | n = 1,000,000 |
| O(1) | 1 operation (instant) |
| O(log n) | 20 operations (instant) |
| O(n) | 1,000,000 ops (milliseconds) |
| O(n log n) | 20,000,000 ops (seconds) |
| O(n²) | 1,000,000,000,000 (hours/days) |
| O(2^n) | impossible (heat death of universe) |
Analyzing Code — Rules
Rule 1: Drop Constants
Rule 2: Drop Lower-Order Terms
Rule 3: Different Inputs → Different Variables
// O(a + b), NOT O(n)
void processTwo(int[] arrA, int[] arrB) {
for (int x : arrA) { } // O(a)
for (int y : arrB) { } // O(b)
} // Total: O(a + b)
// O(a * b), NOT O(n²)
void crossProduct(int[] arrA, int[] arrB) {
for (int x : arrA) { // O(a)
for (int y : arrB) { } // × O(b)
}
} // Total: O(a * b)Common Patterns
O(1) — Constant
O(log n) — Logarithmic
O(n) — Linear
O(n log n) — Linearithmic
// Merge sort, quicksort (average), heap sort
void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid); // T(n/2)
mergeSort(arr, mid + 1, right); // T(n/2)
merge(arr, left, mid, right); // O(n)
}
}
// Recurrence: T(n) = 2T(n/2) + O(n) → O(n log n)O(n²) — Quadratic
Space Complexity
Java Collections Complexity
| Operation | ArrayList | LinkedList | HashMap | TreeMap | HashSet |
|---|---|---|---|---|---|
| Access/Get | O(1) | O(n) | O(1)* | O(log n) | — |
| Search | O(n) | O(n) | O(1)* | O(log n) | O(1)* |
| Insert (end) | O(1)* | O(1) | O(1)* | O(log n) | O(1)* |
| Insert (mid) | O(n) | O(1)† | — | — | — |
| Delete | O(n) | O(1)† | O(1)* | O(log n) | O(1)* |
| Sort | O(n log n) | O(n log n) | — | — | — |
*amortized, †with iterator at position
Amortized Analysis
// ArrayList.add() — usually O(1), occasionally O(n) when resizing
// But AMORTIZED over n operations: O(1) per operation
// Example: ArrayList grows by doubling
// Start: capacity 1
// Add 1 element: no resize, 1 operation
// Add 2nd: resize to 2, copy 1 + insert 1 = 2 ops
// Add 3rd: resize to 4, copy 2 + insert 1 = 3 ops
// Add 5th: resize to 8, copy 4 + insert 1 = 5 ops
// ...
// Total for n inserts ≈ 3n operations → O(1) amortized per insertPractical Comparison (Benchmark)
Interview Questions
Q1: What is the difference between O(n) and O(n²)?
Answer: O(n) means runtime grows linearly (double input = double time). O(n²) means runtime grows quadratically (double input = 4× time). For n=1000: O(n)=1000 operations, O(n²)=1,000,000 operations.
Q2: What is the time complexity of HashMap.get() and why?
Answer: O(1) amortized. It computes hashCode, finds the bucket directly (O(1)), then checks equals in the bucket (usually O(1) if few collisions). Worst case with all collisions: O(n) for LinkedList chains, O(log n) for tree bins (Java 8+).
Q3: What is amortized time complexity?
Answer: The average time per operation over a sequence of operations. ArrayList.add() is O(1) amortized — most adds are O(1) but occasional resizing is O(n). Over n operations, total work is O(n), so per-operation average is O(1).
Q4: Why do we drop constants in Big O?
Answer: Big O describes growth rate, not exact time. O(2n) and O(n) grow at the same rate — both are linear. Constants depend on hardware and are meaningless for comparing algorithmic efficiency across different machines.
Q5: What is the time complexity of Arrays.sort() in Java?
Answer: O(n log n). For objects it uses TimSort (merge sort variant). For primitives it uses Dual-Pivot Quicksort. Both are O(n log n) average and worst case (TimSort) or O(n²) worst case (Quicksort, but extremely unlikely with pivot selection).
Q6: How do you determine if your algorithm is efficient enough?
Answer: Rule of thumb: ~10⁸ simple operations per second. For n=10⁵, O(n²)=10¹⁰ (too slow), O(n log n)=~10⁶ (fast). Match your algorithm's complexity to the constraint: n≤10⁶ needs O(n log n) or better; n≤10³ allows O(n²).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Time Complexity.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, dsa, time, complexity
Related Java Master Course Topics