Algorithm Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Algorithm.

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

A step-by-step set of instructions for solving a problem or accomplishing a specific task. An algorithm must be precise (every step is unambiguous), finite (it terminates after a bounded number of steps), and effective (each step can actually be carried out).

A recipe for solving problemsβ€”follow the steps, get the answer.

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: An algorithm must be precise, finite, and guaranteed to produce a result for valid inputs.

Common stuck point: Algorithms must work for all valid inputs, not just specific examples.

Sense of Study hint: When designing an algorithm, start by clearly defining the input and the desired output. Then break the solution into small, unambiguous steps that a computer could follow literally. Finally, trace through your steps with a few test inputs to verify correctness.

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

Worked Examples

Example 1

easy
Write an algorithm (as numbered steps) for making a cup of tea.

Answer

A 7-step algorithm from filling the kettle to adding milk, with each instruction clear and ordered.

First step

1
Step 1: Identify the key actions in order β€” boil water, prepare the cup, brew, and serve.

Full solution

  1. 2
    Step 2: Write clear, unambiguous steps: 1. Fill kettle with water. 2. Boil the kettle. 3. Place tea bag in cup. 4. Pour boiling water into cup. 5. Wait 3 minutes. 6. Remove tea bag. 7. Add milk if desired.
  2. 3
    Step 3: Verify the algorithm is complete (handles start to finish) and each step is precise enough for anyone to follow.
An algorithm is a step-by-step set of instructions to solve a problem or complete a task. Good algorithms are precise, unambiguous, and finite.

Example 2

medium
Here is an algorithm: 1. Set total = 0. 2. For each number in the list [3, 7, 2, 8]: add the number to total. 3. Output total. What does this algorithm compute?

Example 3

easy
Write a clear algorithm to find the smaller of two numbers AA and BB.

Example 4

medium
Trace this max-finder on [2,8,3,8,1][2,8,3,8,1]: keep a running best. What is the final best?

Example 5

medium
Design an algorithm to determine whether nn is even or odd. Outline the steps.

Example 6

hard
Two algorithms compute 1+2+β‹―+n1+2+\dots+n: A uses a loop with nn additions; B uses n(n+1)2\tfrac{n(n+1)}{2}. For n=100n=100, how many additions does each do (count only the additions inside the formula)?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
Write an algorithm to find the largest number in a list of three numbers: A, B, C.

Example 2

easy
A recipe says, 'Add sugar until it tastes right.' Explain why this is a poor algorithm step and rewrite it so it is precise.

Example 3

easy
Trace this algorithm: start with x=3x=3, then x=x+4x = x + 4, then x=xΓ—2x = x \times 2. What is the final value of xx?

Example 4

easy
Which property is REQUIRED of every algorithm: (a) it uses a loop, (b) it terminates after a bounded number of steps, (c) it is written in Python?

Example 5

easy
Is this a valid algorithm step? 'Pick whichever number looks nicest.' Answer yes or no.

Example 6

easy
An algorithm adds 1 to a counter each step starting from 0 and stops at 5. How many steps run?

Example 7

easy
Order these into a correct algorithm to make tea: (1) pour water, (2) boil water, (3) add tea bag. Which order?

Example 8

easy
Does an algorithm need to give the correct answer for ALL valid inputs, or just one example input?

Example 9

easy
Trace: s=0s=0; for each number in [2,5,3][2,5,3], do s=s+numbers = s + \text{number}. Final ss?

Example 10

easy
An algorithm receives an empty list and must output its average. What edge case must it handle?

Example 11

medium
Trace: a=10a=10, b=4b=4. Do t=at=a; a=ba=b; b=tb=t. What are aa and bb at the end?

Example 12

medium
An algorithm checks if nn is prime by testing divisors from 2 up to nβˆ’1n-1. For n=9n=9, at which divisor does it first find a factor?

Example 13

medium
Trace this algorithm to find the max of [4,9,2,9,7][4,9,2,9,7]: keep a running best. What is the final best?

Example 14

medium
An algorithm: read nn; if nn even output n/2n/2, else output 3n+13n+1. For n=7n=7, what is the output?

Example 15

medium
How many comparisons does linear search make to find 8 in [3,8,1,5][3,8,1,5] (searching left to right)?

Example 16

medium
Trace: r=1r=1; for ii in 1..41..4: r=rΓ—ir = r \times i. Final rr?

Example 17

medium
An algorithm reverses the digits of 504. What is the output, and what edge case do trailing-zero numbers raise?

Example 18

medium
Bubble sort on [3,1,2][3,1,2]: after the FIRST full pass (compare/swap adjacent left to right), what is the list?

Example 19

medium
Trace: x=20x=20; if xβ‰₯10x \ge 10: x=xβˆ’5x = x - 5 else x=x+5x = x + 5; then x=xΓ—2x = x \times 2. Final xx?

Example 20

challenge
An algorithm doubles a value each step starting at 1 and stops when it exceeds 100. How many steps run, and what is the final value?

Example 21

challenge
Two algorithms compute the same sum: A loops nn times; B uses n(n+1)2\frac{n(n+1)}{2}. For n=1000n=1000, how many additions does B do versus A's 999?

Example 22

challenge
Design check: an algorithm to find the median of a list. Which step ordering is correct: (A) sort then pick middle, (B) pick middle then sort?

Example 23

easy
Trace: x=2x=2; then x=x+7x = x + 7. Final xx?

Example 24

easy
Which property is REQUIRED of every algorithm: (a) it uses recursion, (b) every step is unambiguous, (c) it is written in English?

Example 25

easy
Trace: s=0s=0; for each number in [6,4,5][6,4,5], do s=s+numbers = s + \text{number}. Final ss?

Example 26

easy
Order these into a correct algorithm to wash hands: (1) wet hands, (2) rinse, (3) apply soap, (4) scrub. Which order?

Example 27

easy
Is this a valid algorithm step? 'Multiply xx by 3.' Answer yes or no.

Example 28

medium
An algorithm: read nn; if nn is even output n/2n/2, else output 3n+13n+1. For n=10n=10, what is the output?

Example 29

medium
Trace: r=1r=1; for ii in 1..51..5: r=rΓ—ir = r \times i. Final rr?

Example 30

medium
How many comparisons does linear search make to find 5 in [2,4,6,5,8][2,4,6,5,8] (searching left to right)?

Example 31

medium
Trace: a=7a=7, b=3b=3. Do a=a+ba = a + b; b=aβˆ’bb = a - b; a=aβˆ’ba = a - b. Final aa, bb?

Example 32

medium
An algorithm counts how many items in [5,2,8,1,7,4][5,2,8,1,7,4] are greater than 4. What does it output?

Example 33

medium
Trace: x=3x=3; while x<20x < 20: x=xΓ—2x = x \times 2. Final xx?

Example 34

medium
Bubble sort one pass on [4,2,5,1][4,2,5,1] (compare/swap adjacent, left to right). What is the list after the FIRST full pass?

Example 35

medium
Trace: x=12x=12; if xβ‰₯10x \ge 10: x=xβˆ’5x = x - 5 else x=x+5x = x + 5; then x=x+1x = x + 1. Final xx?

Example 36

medium
An algorithm computes the average of a list. Why must it handle the empty-list edge case?

Example 37

medium
Trace: n=6n=6, sum=0sum=0. While n>0n>0: sum=sum+nsum = sum + n; n=nβˆ’1n = n - 1. Final sumsum?

Example 38

hard
An algorithm tests whether n=21n=21 is prime by trying divisors from 2 to nβˆ’1n-1. At which divisor does it first find a factor?

Example 39

hard
Binary search needs at most how many comparisons on a sorted list of length 16?

Example 40

hard
An algorithm halves xx each step (integer division) starting from x=80x=80 and stops when xx reaches 0. How many steps run?

Example 41

hard
Trace Euclid's algorithm for gcd⁑(48,18)\gcd(48, 18): while bβ‰ 0b \ne 0: (a,b)←(b,aβ€Šmodβ€Šb)(a,b) \leftarrow (b, a \bmod b). What is the result?

Example 42

challenge
An algorithm doubles a value each step starting at 1 and stops when the value first exceeds 1000. What is the final value, and how many doubling steps run?