Start with the recap, study the fully worked examples, then use the practice problems to
check your understanding of Generalization.
This page combines explanation, solved examples, and follow-up practice so you can move
from recognition to confident problem-solving in CS Thinking.
Concept Recap
Generalization is the process of taking a pattern that appears in several examples and turning it into a rule or method that works in many cases. In computational thinking, it helps students move from one solved example to a reusable strategy.
Solve one case carefully, notice what stays the same, then write one rule that fits many cases.
Read the first worked example with the solution open so the structure is clear.
Try the practice problems before revealing each solution.
Use the related concepts and background knowledge badges if you feel stuck.
What to Focus On
Core idea:Generalization turns repeated examples into one reusable idea.
Common stuck point:A good general rule must fit all intended cases, not just the first two examples you notice.
Sense of Study hint:List several examples side by side. Mark what changes and what stays the same. Then write a rule using variables or placeholders so the idea works for any valid input.
Worked Examples
Example 1
easy
From the pseudocode `for i in [1..n]: total += i`, what closed-form generalization computes the same sum without a loop?
Answer
sum(n)=n(n+1)/2
First step
1
The loop sums 1+2+⋯+n.
See the full worked solution + why-it-works coaching
Setup·Key insight·Why it works·Common pitfall·Connection
A linear search returns the index of a target in a list. Generalize it so the caller can search by an arbitrary predicate.
Example 3
medium
A function `times2(n)` returns 2n and `times3(n)` returns 3n. Generalize using a closure that returns a function.
Example 4
medium
From f(0)=1,f(1)=2,f(2)=4,f(3)=8, what general rule fits, and how would you verify it in code?
Example 5
hard
Identify the pattern and write a recurrence then closed form: a1=1,a2=3,a3=7,a4=15,a5=31.
Example 6
hard
A recursive function counts paths on a 2×n grid. Generalize the recurrence so the same code works for an m×n grid.
Example 7
hard
A web framework has hand-written handlers for `/user/1/posts`, `/user/2/posts`, etc. Generalize to a single route pattern and handler.
Example 8
hard
A `top_k_by_score(students)` selects the top-k students by `student.score`. Generalize so any list of items can be ranked by any numeric attribute.
Example 9
medium
Three functions each compute the volume of a cube, cylinder, and sphere. What two-step generalization makes them interchangeable for downstream code?
Example 10
challenge
Recognize the family: f1(x)=x2−2, f2(x)=x2−5, f3(x)=x2−10. Write a generalized Newton-step that handles 'square root of c' for any c≥0.
Example 11
challenge
Map, filter, and reduce are sometimes presented as three primitives. What single more general operation subsumes them, and which Haskell function realizes it?
Example 12
medium
From u(n)=u(n−1)+4 with u(1)=2, write a closed-form general rule for u(n).
Example 13
hard
From a specific check 'string of length 5 has 5 indices, 0 to 4' generalize to any string of length n and state the valid index range.
Practice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
easy
A student notices 1+3=4, 1+3+5=9, 1+3+5+7=16. What general rule fits the sum of the first n odd numbers?
Example 2
easy
From f(1)=2, f(2)=4, f(3)=6, what single rule f(n) generalizes these outputs?
Example 3
easy
A recipe for 2 people uses 4 eggs; for 3 people, 6 eggs; for 5 people, 10 eggs. Generalize the eggs needed for p people.
Example 4
easy
Squares: side 1 area 1, side 2 area 4, side 3 area 9. What general rule gives the area for side s?
Example 5
easy
A loop runs and prints 2,4,8,16. What general rule gives the k-th printed value?
Example 6
easy
Perimeters of equilateral triangles: side 2 gives 6, side 5 gives 15, side 7 gives 21. Generalize for side s.
Example 7
easy
A function gives g(0)=1, g(1)=3, g(2)=5, g(3)=7. What general rule fits?
Example 8
easy
To greet any user, the messages were 'Hi Ann', 'Hi Bob', 'Hi Cy'. Generalize the message for a name x.
Example 9
medium
A student claims 'every prime is odd' after seeing 3,5,7,11. Is this generalization valid? Give the counterexample.
Example 10
medium
Sums 1+2=3, 1+2+3=6, 1+2+3+4=10. Generalize the sum of the first n integers and verify for n=4.
Example 11
medium
A specific function reverses the list [1,2,3] to [3,2,1]. Generalize the rule for any list of length n.
Example 12
medium
From 22=4, 23=8, 24=16 a student writes 2n. What general rule covers the product 2a⋅2b?
Example 13
medium
Tables: input 1->1, 2->4, 3->9, 4->16, 5->25. A student guesses output = input ×5. Test it and give the correct rule.
Example 14
medium
A program multiplies a list by a constant: [1,2,3]→[3,6,9] used factor 3. Generalize the output for factor c and element x.
Example 15
medium
Areas of rectangles with width 2: length 3 area 6, length 5 area 10, length 8 area 16. Generalize the area for length L.
Example 16
medium
Outputs for inputs 1,2,3,4 are 0,3,8,15. Find the general rule.
Example 17
medium
A specific check 'x≥18 AND x≤65' was used for ages 18-65. Generalize this membership test for bounds a and b.
Example 18
challenge
A student forms the rule 'n2−n+41 is always prime' after testing n=1,2,3. Find the smallest n that breaks it.
Example 19
challenge
Given f(1)=1,f(2)=2,f(3)=4,f(4)=8,f(5)=16, a student writes f(n)=2n−1. The actual rule counts regions formed by n points on a circle joined by all chords, where f(6)=31. Why does 2n−1 fail, and what does this show?
Example 20
challenge
Design a general rule: a tax is $0 up to $1000, then 10% on the amount above $1000. Express tax T as a function of income x for x>1000, and compute T(3000).
Example 21
easy
A function `square_two()` returns 2⋅2, and `square_three()` returns 3⋅3. Write a single generalized function.
Example 22
easy
Code computes the area of a 3×4 rectangle, then a 5×6 rectangle. Generalize to any rectangle.
Example 23
easy
A program prints `Hello, Alice!` then `Hello, Bob!` then `Hello, Carol!`. Generalize using a list and a loop.
Example 24
medium
From `T(1)=1, T(2)=3, T(3)=6, T(4)=10`, what closed-form rule for T(n) generalizes the pattern?
Example 25
medium
A `sum_ints(list)` adds integers. A `sum_floats(list)` adds floats. In a generic-typed language, what is the single generalized signature?
Example 26
medium
Three functions iterate a list and respectively sum, find max, and count. What single higher-order function generalizes all three?
Example 27
medium
A logger writes to a file. The same code needs to optionally write to a network socket. What kind of generalization fits?
Example 28
medium
A factorial function works for `int`. To support arbitrarily large n without overflow, what kind of generalization helps?
Example 29
hard
Two sorting functions are written for `List<Int>` and `List<Date>`. What single generalization eliminates the duplication?
Example 30
hard
Pattern recognition: `[1, 11, 21, 1211, 111221, ...]`. State the generalized rule that produces term n+1 from term n.
Example 31
hard
From the sample inputs T(2)=4,T(4)=8,T(8)=16 for a function on powers of 2, what is a reasonable generalization, and what's the risk?
Example 32
medium
A function `discount_10pct(price)` returns `price * 0.9`. Generalize to any percentage.
Example 33
medium
A hard-coded SQL query selects users where `country = 'US'`. Generalize safely to any country without SQL injection.
Example 34
challenge
A hard-coded depth-first search uses a stack. A breadth-first search uses a queue. What single algorithm generalizes both?
Example 35
hard
A bubble sort, an insertion sort, and a selection sort all sort lists in O(n2). What's the danger of over-generalizing them into 'they're all the same'?
Example 36
easy
Pencils cost $2 for 1, $4 for 2, $6 for 3. Generalize the cost for n pencils.
Example 37
easy
Sequence: 5, 10, 15, 20. What general rule gives the n-th term (starting at n=1)?
Example 38
easy
Cubes: side 1 has volume 1, side 2 has volume 8, side 3 has volume 27. Generalize for side s.
Example 39
easy
From h(1)=4, h(2)=7, h(3)=10, find a general rule for h(n).
Example 40
easy
A printer makes 12 copies in 1 minute, 24 in 2 minutes, 36 in 3 minutes. Generalize for t minutes.
Example 41
easy
From the pattern 2⋅1=2, 2⋅2=4, 2⋅3=6, write a general rule for any positive whole number n.
Example 42
easy
A snack pack has 4 cookies. With 1 pack you have 4 cookies, with 2 packs 8, with 3 packs 12. Generalize for p packs.
Example 43
easy
Outputs for inputs 0,1,2,3 are 5,6,7,8. Write a general rule.
Example 44
medium
From g(1)=2, g(2)=5, g(3)=10, g(4)=17, find a general rule.
Example 45
medium
A loop prints 3,6,12,24. Generalize the k-th printed value (start at k=1).
Example 46
medium
Triangle with n rows has 1, 3, 6, 10 dots for n=1,2,3,4. Generalize for n rows.
Example 47
medium
A student tests one positive number and concludes 'multiplying any two numbers gives a positive answer.' Give a counterexample and a corrected general rule.
Example 48
medium
Cost to ship: $3 for the first item, $1 per item after. Generalize total cost for n≥1 items.
Example 49
medium
Tables: input 1, 2, 3, 4 give output 1, 4, 9, 16. A student writes output =4⋅input. Verify the guess and give the correct general rule.
Example 50
medium
A student rule says 'sum of n ones is n.' Generalize: write a rule for the sum of n copies of any number c.
Example 51
medium
From a check 'is the year a leap year?' that handled only years divisible by 4, generalize the rule to include the century exception.
Example 52
medium
Rectangles with width 3 have area 3, 6, 9, 12 for lengths 1, 2, 3, 4. Generalize the area for any width w and length L.
Example 53
medium
From f(2)=7,f(3)=10,f(4)=13, write a rule and predict f(10).
Example 54
medium
A program adds 5 to every list element: [1,2,3]→[6,7,8]. Generalize to add k to every element of a list of length n.
Example 55
hard
From ∑k=11k2=1, ∑k=12k2=5, ∑k=13k2=14, a student conjectures ∑k=1nk2=n(n+1)(2n+1)/6. Verify for n=4.
Example 56
hard
From the data 'rectangles with perimeter 20': 4x6 has area 24, 5x5 has area 25, 3x7 has area 21. Generalize: write area as a function of one side a when perimeter is 20.
Example 57
hard
A student notices the pattern 1=1, 1+8=9, 1+8+27=36, 1+8+27+64=100. Generalize the sum of the first n cubes.
Example 58
hard
Outputs for inputs 1, 2, 3, 4, 5 are 2, 6, 12, 20, 30. Find a general rule.
Example 59
hard
A student sees that n=1,2,3,4 all give n2≥n and concludes 'square is always at least the number itself.' For which real numbers n does this fail?
Example 60
challenge
Conjecture: 'every odd number n≥3 is the sum of three primes.' Test for n=3,5,7,9,11 and state which case shows the conjecture must allow primes to repeat.
Example 61
challenge
A specific calculation: tip on a $50 bill at 18% is $9. Generalize the tip as a function of bill amount b and rate r (as a decimal), and compute the tip on a $120 bill at 20%.