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 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.
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
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.
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 A and B.
Example 4
medium
Trace this max-finder on [2,8,3,8,1]: keep a running best. What is the final best?
Example 5
medium
Design an algorithm to determine whether n is even or odd. Outline the steps.
Example 6
hard
Two algorithms compute 1+2+⋯+n: A uses a loop with n additions; B uses 2n(n+1). For n=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=3, then x=x+4, then x=x×2. What is the final value of x?
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=0; for each number in [2,5,3], do s=s+number. Final s?
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=10, b=4. Do t=a; a=b; b=t. What are a and b at the end?
Example 12
medium
An algorithm checks if n is prime by testing divisors from 2 up to n−1. For n=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]: keep a running best. What is the final best?
Example 14
medium
An algorithm: read n; if n even output n/2, else output 3n+1. For n=7, what is the output?Trace the algorithm for n
Example 15
medium
How many comparisons does linear search make to find 8 in [3,8,1,5] (searching left to right)?Linear search for 8 in [3, 8, 1, 5]
Example 16
medium
Trace: r=1; for i in 1..4: r=r×i. Final r?
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]: after the FIRST full pass (compare/swap adjacent left to right), what is the list?
Example 19
medium
Trace: x=20; if x≥10: x=x−5 else x=x+5; then x=x×2. Final x?Trace the algorithm for x
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 n times; B uses 2n(n+1). For n=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=2; then x=x+7. Final x?
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=0; for each number in [6,4,5], do s=s+number. Final s?
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 x by 3.' Answer yes or no.
Example 28
medium
An algorithm: read n; if n is even output n/2, else output 3n+1. For n=10, what is the output?Trace the algorithm for n
Example 29
medium
Trace: r=1; for i in 1..5: r=r×i. Final r?
Example 30
medium
How many comparisons does linear search make to find 5 in [2,4,6,5,8] (searching left to right)?Linear search for 5 in [2, 4, 6, 5, 8]
Example 31
medium
Trace: a=7, b=3. Do a=a+b; b=a−b; a=a−b. Final a, b?
Example 32
medium
An algorithm counts how many items in [5,2,8,1,7,4] are greater than 4. What does it output?
Example 33
medium
Trace: x=3; while x<20: x=x×2. Final x?
Example 34
medium
Bubble sort one pass on [4,2,5,1] (compare/swap adjacent, left to right). What is the list after the FIRST full pass?
Example 35
medium
Trace: x=12; if x≥10: x=x−5 else x=x+5; then x=x+1. Final x?Trace the algorithm for x
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=6, sum=0. While n>0: sum=sum+n; n=n−1. Final sum?
Example 38
hard
An algorithm tests whether n=21 is prime by trying divisors from 2 to n−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 x each step (integer division) starting from x=80 and stops when x reaches 0. How many steps run?
Example 41
hard
Trace Euclid's algorithm for gcd(48,18): while b=0: (a,b)←(b,amodb). 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?