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.

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.

Worked Examples

Example 1

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

Solution

  1. 1
    Step 1: Identify the key actions in order β€” boil water, prepare the cup, brew, and serve.
  2. 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. 3
    Step 3: Verify the algorithm is complete (handles start to finish) and each step is precise enough for anyone to follow.

Answer

A 7-step algorithm from filling the kettle to adding milk, with each instruction clear and ordered.
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?

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.