Generalization Examples in CS Thinking

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 full concept explanation โ†’

How to Use These Examples

  • 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\texttt{sum(n)} = n(n+1)/2

First step

1
The loop sums 1+2+โ‹ฏ+n1 + 2 + \dots + n.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan โ€” every worked solution, all subjects

Example 2

medium
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 2n2n and `times3(n)` returns 3n3n. Generalize using a closure that returns a function.

Example 4

medium
From f(0)=1,f(1)=2,f(2)=4,f(3)=8f(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=31a_1=1, a_2=3, a_3=7, a_4=15, a_5=31.

Example 6

hard
A recursive function counts paths on a 2ร—n2 \times n grid. Generalize the recurrence so the same code works for an mร—nm \times 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-kk 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โˆ’2f_1(x) = x^2 - 2, f2(x)=x2โˆ’5f_2(x) = x^2 - 5, f3(x)=x2โˆ’10f_3(x) = x^2 - 10. Write a generalized Newton-step that handles 'square root of cc' for any cโ‰ฅ0c \ge 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)+4u(n) = u(n-1) + 4 with u(1)=2u(1) = 2, write a closed-form general rule for u(n)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 nn 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=41+3=4, 1+3+5=91+3+5=9, 1+3+5+7=161+3+5+7=16. What general rule fits the sum of the first nn odd numbers?

Example 2

easy
From f(1)=2f(1)=2, f(2)=4f(2)=4, f(3)=6f(3)=6, what single rule f(n)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 pp 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 ss?

Example 5

easy
A loop runs and prints 2,4,8,162,4,8,16. What general rule gives the kk-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 ss.

Example 7

easy
A function gives g(0)=1g(0)=1, g(1)=3g(1)=3, g(2)=5g(2)=5, g(3)=7g(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 xx.

Example 9

medium
A student claims 'every prime is odd' after seeing 3,5,7,113,5,7,11. Is this generalization valid? Give the counterexample.

Example 10

medium
Sums 1+2=31+2=3, 1+2+3=61+2+3=6, 1+2+3+4=101+2+3+4=10. Generalize the sum of the first nn integers and verify for n=4n=4.

Example 11

medium
A specific function reverses the list [1,2,3][1,2,3] to [3,2,1][3,2,1]. Generalize the rule for any list of length nn.

Example 12

medium
From 22=42^2=4, 23=82^3=8, 24=162^4=16 a student writes 2n2^n. What general rule covers the product 2aโ‹…2b2^a \cdot 2^b?

Example 13

medium
Tables: input 1->1, 2->4, 3->9, 4->16, 5->25. A student guesses output == input ร—5\times 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][1,2,3]\to[3,6,9] used factor 3. Generalize the output for factor cc and element xx.

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 LL.

Example 16

medium
Outputs for inputs 1,2,3,41,2,3,4 are 0,3,8,150,3,8,15. Find the general rule.

Example 17

medium
A specific check 'xโ‰ฅ18x \ge 18 AND xโ‰ค65x \le 65' was used for ages 18-65. Generalize this membership test for bounds aa and bb.

Example 18

challenge
A student forms the rule 'n2โˆ’n+41n^2-n+41 is always prime' after testing n=1,2,3n=1,2,3. Find the smallest nn that breaks it.

Example 19

challenge
Given f(1)=1,f(2)=2,f(3)=4,f(4)=8,f(5)=16f(1)=1, f(2)=2, f(3)=4, f(4)=8, f(5)=16, a student writes f(n)=2nโˆ’1f(n)=2^{n-1}. The actual rule counts regions formed by nn points on a circle joined by all chords, where f(6)=31f(6)=31. Why does 2nโˆ’12^{n-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 TT as a function of income xx for x>1000x>1000, and compute T(3000)T(3000).

Example 21

easy
A function `square_two()` returns 2โ‹…22 \cdot 2, and `square_three()` returns 3โ‹…33 \cdot 3. Write a single generalized function.

Example 22

easy
Code computes the area of a 3ร—43 \times 4 rectangle, then a 5ร—65 \times 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)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 nn 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+1n+1 from term nn.

Example 31

hard
From the sample inputs T(2)=4,T(4)=8,T(8)=16T(2)=4, T(4)=8, T(8)=16 for a function on powers of 22, 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)O(n^2). 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 nn pencils.

Example 37

easy
Sequence: 5, 10, 15, 20. What general rule gives the nn-th term (starting at n=1n=1)?

Example 38

easy
Cubes: side 1 has volume 1, side 2 has volume 8, side 3 has volume 27. Generalize for side ss.

Example 39

easy
From h(1)=4h(1)=4, h(2)=7h(2)=7, h(3)=10h(3)=10, find a general rule for h(n)h(n).

Example 40

easy
A printer makes 12 copies in 1 minute, 24 in 2 minutes, 36 in 3 minutes. Generalize for tt minutes.

Example 41

easy
From the pattern 2โ‹…1=22 \cdot 1 = 2, 2โ‹…2=42 \cdot 2 = 4, 2โ‹…3=62 \cdot 3 = 6, write a general rule for any positive whole number nn.

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 pp packs.

Example 43

easy
Outputs for inputs 0,1,2,30,1,2,3 are 5,6,7,85,6,7,8. Write a general rule.

Example 44

medium
From g(1)=2g(1)=2, g(2)=5g(2)=5, g(3)=10g(3)=10, g(4)=17g(4)=17, find a general rule.

Example 45

medium
A loop prints 3,6,12,243, 6, 12, 24. Generalize the kk-th printed value (start at k=1k=1).

Example 46

medium
Triangle with nn rows has 1, 3, 6, 10 dots for n=1,2,3,4n=1,2,3,4. Generalize for nn 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โ‰ฅ1n \ge 1 items.

Example 49

medium
Tables: input 1, 2, 3, 4 give output 1, 4, 9, 16. A student writes output =4โ‹…input= 4 \cdot \text{input}. Verify the guess and give the correct general rule.

Example 50

medium
A student rule says 'sum of nn ones is nn.' Generalize: write a rule for the sum of nn copies of any number cc.

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 ww and length LL.

Example 53

medium
From f(2)=7,f(3)=10,f(4)=13f(2)=7, f(3)=10, f(4)=13, write a rule and predict f(10)f(10).

Example 54

medium
A program adds 5 to every list element: [1,2,3]โ†’[6,7,8][1,2,3] \to [6,7,8]. Generalize to add kk to every element of a list of length nn.

Example 55

hard
From โˆ‘k=11k2=1\sum_{k=1}^{1} k^2 = 1, โˆ‘k=12k2=5\sum_{k=1}^{2} k^2 = 5, โˆ‘k=13k2=14\sum_{k=1}^{3} k^2 = 14, a student conjectures โˆ‘k=1nk2=n(n+1)(2n+1)/6\sum_{k=1}^{n} k^2 = n(n+1)(2n+1)/6. Verify for n=4n=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 aa when perimeter is 20.

Example 57

hard
A student notices the pattern 1=11 = 1, 1+8=91+8 = 9, 1+8+27=361+8+27 = 36, 1+8+27+64=1001+8+27+64 = 100. Generalize the sum of the first nn 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,4n=1,2,3,4 all give n2โ‰ฅnn^2 \ge n and concludes 'square is always at least the number itself.' For which real numbers nn does this fail?

Example 60

challenge
Conjecture: 'every odd number nโ‰ฅ3n \ge 3 is the sum of three primes.' Test for n=3,5,7,9,11n = 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 bb and rate rr (as a decimal), and compute the tip on a $120 bill at 20%.

Background Knowledge

These ideas may be useful before you work through the harder examples.

pattern recognition