Quiz 2

Computational Thinking · Week 4 — Nested iteration

1148 words
6 min read
2026-08-16T00:00:00.000Z
Python Week 1: the first filter for runtime behavior
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.
  1. Nested iterationfor each i: for each j: — pairs / grid
  2. Binningbucket[index] += 1 — histogram / frequency
  3. Collision checkseen before? — duplicate detection
  4. Inner breakexit inner only — flag + break scope
  5. Complexityouter × inner — count body executions

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 at i+1 for unique pairs.
  • Iteration count → outer n times inner mn × m body 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:
text
for i from 0 to n-1:
    for j from i+1 to n-1:
        compare item[i] with item[j]
Count: n(n1)2\frac{n(n-1)}{2}.
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.
text
found ← 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 i and j after nested for.
  • 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.
text
count ← 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 j never advances toward stop.
  • Row/column order swapped in grid interpretation.
  • Assuming always — restricted inner changes count.

Diagnostic (try yourself)

  1. How many times does the body run?
text
for i from 0 to 4:
    for j from 0 to 2:
        # body
  1. For n=6, how many unique unordered pairs (i,j) with j > i?
  2. Values [14, 6, 21, 9, 14] binned by index = value // 10. List the five bin indices.
  3. List [1,2,3,2]. How many unique pairs have equal values?
  4. Why use j = i+1 instead of j = 0 when 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

PatternInner loop start
all pairsj = i+1 or full grid
grid cellcolumn 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

  1. How many unordered pairs from n items?
  2. Why start inner at i+1 for unique pairs?
  3. What is binning used for?

Practice loop

  1. Read Deep study (if present) or core concepts once.
  2. Recite the formula chain without looking.
  3. Open one easy pattern on the interactive atlas for week 4.
  4. Attempt without solutions; mark studied after an honest try.
  5. Say one trap aloud before closing the tab.
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.