Computational Thinking · Week 4 — Nested iteration
1148 words
6 min read
2026-08-16T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
nested loops, binning, birthday pattern — concepts, pattern families, and traps for Quiz 2 week 4. # Week 4 — nested iteration > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

Week 4 — nested iteration
Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.
Part of the Quiz 2 prep system%20%C2%B7%20%5BWeeks%201%E2%80%938%20index%5D(.%2Fmay-2026-ct-quiz-2-weeks-1-8-prep) · Pattern atlas · Formula chains.
Week map
Outer index → inner index → pair generation → bin counts
Classify → Represent → Execute → Trap-check
- Recognize: Ask: How many unordered pairs from n items?
- Procedure: For i in 0..n-1, j in 0..n-1 gives n² pairs. Restrict j > i gives n(n-1)/2 unique unordered pairs.
- Variations / traps: Watch for: Double-counting pairs when inner not restricted.
Formula chain (compressed)
nested loops → binning counts → birthday collision pattern.
- Nested iteration —
for each i: for each j:— pairs / grid - Binning —
bucket[index] += 1— histogram / frequency - Collision check —
seen before?— duplicate detection - Inner break —
exit inner only— flag + break scope - Complexity —
outer × inner— count body executions
Open interactive formula desk · Week 4 tab.
Deep study
Computational Thinking · Week 4 — Nested iteration
Nested loops visit pairs or grid cells. Week 4 patterns: pair counting, duplicate detection, and binning tallies.
Week map
Outer loop → inner loop completes per outer step → index pairs (i,j) → unique unordered pairs → bin array → birthday collision pattern.
Nested loop notation
- Outer index
i→ often row or first item position. - Inner index
j→ column or second item; may start ati+1for unique pairs. - Iteration count → outer
ntimes innerm→n × mbody executions if full grid.
Mini-grid
i in 0..2, j in 0..2 (both 0,1,2): 3×3 = 9 pairs including (0,0),(1,1),(2,2).Unique unordered pairs
From
n items, compare each pair once without double-count:textfor i from 0 to n-1: for j from i+1 to n-1: compare item[i] with item[j]
Count: 2n(n−1).
Mini-example:
n=4 → pairs (0,1),(0,2),(0,3),(1,2),(1,3),(2,3) → six pairs.Trap: Inner
j from 0 to n-1 counts (0,1) and (1,0) separately — 12 pairs for n=4.Binning
Fixed buckets
bin[0..B-1]. For each value v, compute bucket index, increment bin[k].Example: scores 0–100 in bins width 10 → index
k = v // 10 (watch overflow at 100).List
[23, 45, 17, 39, 45] with bins 0-9,10-19,…:- 23→bin2, 45→bin4, 17→bin1, 39→bin3, 45→bin4
- bin4 count 2.
Birthday / duplicate pattern
Nested loops compare pairs for equality. If equal, “shared birthday” or duplicate found.
textfound ← False for i ... for j from i+1 ... if item[i] == item[j]: found ← True
Self-pair
i=j usually skipped when inner starts at i+1.Pattern families
Easy — Count loop executions
range(n)×range(m)body count.- Last values of
iandjafter nestedfor. - Grid row-major order listing of
(i,j).
Medium — Binning tallies
- Initialize bin array size from domain.
- Map value to index; increment correct bin.
- Boundary: value exactly on bin edge.
Hard — Duplicate / pair logic
- Unique pair enumeration without double count.
- Count pairs with sum equal target.
- Early exit flags vs counting all matches.
Worked mini-examples
Example 1 — Full grid count.
textcount ← 0 for i from 1 to 3: for j from 1 to 2: count ← count + 1 # 3 * 2 = 6
Example 2 — Unique pairs.
n=5, inner j = i+1 .. n-1. Pairs: 4+3+2+1 = 10.Example 3 — Duplicate.
List
[3,1,4,1,5]. Compare unique pairs; (1,3) positions values 4 and 1 — no; (3,4) values 1 and 1 — match once.Example 4 — Bins width 5.
Values
[7, 12, 3, 18, 12]. Index v//5: 7→1, 12→2, 3→0, 18→3, 12→2.Bins [1,1,2,1,0,...] for indices 0..3 at least.
Example 5 — Pair sum.
List
[2,5,3]. Pairs with sum 7: (2,5) and (5,2) if full grid — 2 if ordered; 1 if unique unordered.Traps
- Double-counting pairs when inner should start at
i+1. - Bin index off-by-one at boundaries (0-based vs 1-based bins).
- Infinite inner loop if
jnever advances toward stop. - Row/column order swapped in grid interpretation.
- Assuming
n²always — restricted inner changes count.
Diagnostic (try yourself)
- How many times does the body run?
textfor i from 0 to 4: for j from 0 to 2: # body
-
For
n=6, how many unique unordered pairs(i,j)withj > i? -
Values
[14, 6, 21, 9, 14]binned byindex = value // 10. List the five bin indices. -
List
[1,2,3,2]. How many unique pairs have equal values? -
Why use
j = i+1instead ofj = 0when detecting duplicates once per pair?
ChatGPT prep archive
Archived import for extra depth — complements the notes above, not official IITM material.
Core concepts
- Nested iteration: all inner runs per outer step; models pairs (i,j) or grid cells.
- Binning: count how many items fall in each category bucket.
- Birthday pattern: compare pairs for match—inner starts after outer to avoid duplicate pairs.
- Complexity intuition: nested loops often O(n²) for n items.
Notation & vocabulary
| Pattern | Inner loop start |
|---|---|
| all pairs | j = i+1 or full grid |
| grid cell | column index 0..cols-1 |
Pattern families
Easy — Pair count
For i in 0..n-1, j in 0..n-1 gives n² pairs. Restrict j > i gives n(n-1)/2 unique unordered pairs.
Medium — Binning tallies
Initialize bin counts. For each item, determine bin index, increment that bin. Bins array size fixed by range.
Hard — Duplicate detection
Compare each pair once using nested loops with inner ahead of outer. Flag when equal. Avoid comparing item with itself unless i=j intended.
Drill these on the pattern atlas — filter to week 4.
Traps
- Double-counting pairs when inner not restricted.
- Bin index off-by-one at boundaries.
- Infinite loop if inner never advances.
- Confusing row/column loop order in grid.
Retrieval prompts
- How many unordered pairs from n items?
- Why start inner at i+1 for unique pairs?
- What is binning used for?
Practice loop
- Read Deep study (if present) or core concepts once.
- Recite the formula chain without looking.
- Open one easy pattern on the interactive atlas for week 4.
- Attempt without solutions; mark studied after an honest try.
- Say one trap aloud before closing the tab.