Nested loops imply quadratic explosions
Beginner
Asymptotics & empirical timing
Created by Pavel
· 29.04.2026 at 19:08 UTC
A double loop over indices (i, j) with i, j < n runs Θ(n²) iterations if the inner body is O(1). That pattern appears in brute-force duplicate detection, naive all-pairs distance matrices before vectorisation, and “for each user, for each item” prototypes that never scale.
Pandas merge on non-indexed keys and accidental Cartesian products can explode row counts the same way—logical nested structure, not just literal for loops. Before optimising inner arithmetic, ask whether the pairing strategy is fundamentally quadratic.
When the problem truly needs all pairs, you either pay quadratic time, use sampling, or restructure (spatial data structures, blocking, matrix algebra).
Time complexity primer: [1].
Sources
University approvals: 0
Tasks
Card Info
- Topic: Asymptotics & empirical timing
- Difficulty: Beginner
- Completed: 0 users
Creator
Pavel