Problem Solving with Algorithms and Pseudocode

Learning Objectives: By the end of this lesson, you will be able to:

  • Apply all core algorithmic concepts to solve more complex problems
  • Design and write complete pseudocode solutions for real-world scenarios
  • Trace and debug algorithms step-by-step

1. Welcome and Introduction (5 minutes)

Icebreaker Activity

Think of a time you solved a tricky problem (like finding a lost item or fixing something broken). What steps did you take? Did you break the problem into smaller parts?

Lesson Overview

  • Duration: 90 minutes
  • Prerequisites: Day 1–5 lessons (algorithms, pseudocode, loops, selection, functions)
  • Materials Needed: Pen and paper or a text editor for writing pseudocode

2. Learning Objectives and Session Outline (5 minutes)

What We’ll Cover Today:

  1. Applying all algorithmic concepts together
  2. Designing algorithms for real-world problems
  3. Writing and tracing complete pseudocode
  4. Hands-on practice and exercises
  5. Debugging and troubleshooting

Success Criteria:

By the end of this session, you should be able to design, write, and trace pseudocode for multi-step problems.

3. Foundation Knowledge (15-20 minutes)

Core Concepts

Problem Decomposition

Break complex problems into smaller, logical pieces. Use functions and modularity to organize your solution.

Algorithm Tracing

Manually step through your pseudocode with sample inputs. Track variable values and logic flow to ensure correctness.

Key Terminology

  • Decomposition: Breaking a problem into smaller parts
  • Tracing: Stepping through an algorithm line by line
  • Debugging: Finding and fixing errors in logic

4. Detailed Explanation and Examples (25-30 minutes)

Example 1: Finding the Smallest Number in a List

Problem

Given a list of numbers, find the smallest value.

Pseudocode

FUNCTION findSmallest(listOfNumbers)
  IF LENGTH(listOfNumbers) == 0 THEN
    RETURN "List is empty"
  ENDIF
  SET smallest TO listOfNumbers[0]
  FOR i FROM 1 TO LENGTH(listOfNumbers) - 1 DO
    IF listOfNumbers[i] < smallest THEN
      SET smallest TO listOfNumbers[i]
    ENDIF
  ENDFOR
  RETURN smallest
ENDFUNCTION

Tracing Example

  • Input: [8, 3, 5, 2, 9]
  • Step through each iteration, updating smallest as needed

Example 2: Simple Vending Machine Logic

Problem

Simulate a vending machine that dispenses items if enough money is inserted and the item is in stock.

Pseudocode

START
  INPUT itemSelected
  INPUT moneyInserted
  IF itemSelected is not in stock THEN
    OUTPUT "Item out of stock."
  ELSE IF moneyInserted < price of itemSelected THEN
    OUTPUT "Not enough money."
  ELSE
    DISPENSE itemSelected
    CALCULATE change = moneyInserted - price of itemSelected
    IF change > 0 THEN
      DISPENSE change
    ENDIF
    OUTPUT "Thank you!"
  ENDIF
END

Tracing Example

  • Input: itemSelected = "Chips", moneyInserted = 2000, price = 1500, in stock = true
  • Step through each condition and output

Common Misconceptions

  1. Misconception: You can skip tracing if the logic looks correct. Reality: Tracing often reveals hidden errors or edge cases.
  2. Misconception: All possible scenarios are covered by default. Reality: Always check for missing branches or unhandled cases.

5. Live Coding Demonstration (20-25 minutes)

Example 3: Tracing and Debugging

// Incorrect
FUNCTION findSmallest(listOfNumbers)
  SET smallest TO 0
  FOR i FROM 0 TO LENGTH(listOfNumbers) - 1 DO
    IF listOfNumbers[i] < smallest THEN
      SET smallest TO listOfNumbers[i]
    ENDIF
  ENDFOR
  RETURN smallest
ENDFUNCTION

// Corrected version
FUNCTION findSmallest(listOfNumbers)
  IF LENGTH(listOfNumbers) == 0 THEN
    RETURN "List is empty"
  ENDIF
  SET smallest TO listOfNumbers[0]
  FOR i FROM 1 TO LENGTH(listOfNumbers) - 1 DO
    IF listOfNumbers[i] < smallest THEN
      SET smallest TO listOfNumbers[i]
    ENDIF
  ENDFOR
  RETURN smallest
ENDFUNCTION

6. Hands-On Practice (30-40 minutes)

Exercise 1: Basic Practice (10 minutes)

Objective: Write pseudocode to find the largest number in a list.

Instructions:

  1. Handle empty lists
  2. Use a loop to check each item
  3. Return the largest value

Starter Code:

FUNCTION findLargest(listOfNumbers)
  // Your steps here
ENDFUNCTION

Solution:

FUNCTION findLargest(listOfNumbers)
  IF LENGTH(listOfNumbers) == 0 THEN
    RETURN "List is empty"
  ENDIF
  SET largest TO listOfNumbers[0]
  FOR i FROM 1 TO LENGTH(listOfNumbers) - 1 DO
    IF listOfNumbers[i] > largest THEN
      SET largest TO listOfNumbers[i]
    ENDIF
  ENDFOR
  RETURN largest
ENDFUNCTION

Exercise 2: Intermediate Challenge (15 minutes)

Objective: Write pseudocode for a simple ATM withdrawal process.

Requirements:

  • Input: account balance, withdrawal amount
  • Check if enough funds are available
  • Output new balance or error message

Hints:

  • Use IF/ELSE to check balance
  • Update balance only if withdrawal is possible

Exercise 3: Creative Application (15 minutes)

Objective: Design an algorithm for a library book checkout system.

Challenge:

  • Input: book title, user ID
  • Check if book is available
  • If available, check if user has overdue books
  • If all checks pass, allow checkout and update records

Extension Ideas:

  • Add a function to list all books checked out by a user
  • Handle edge cases (e.g., book not found, user not registered)

7. Common Pitfalls and Troubleshooting (10 minutes)

Frequent Mistakes

  1. Problem: Not handling empty lists or invalid input Cause: Assuming input is always valid Solution: Always check for edge cases
  2. Problem: Not tracing algorithms with real data Cause: Skipping manual walkthroughs Solution: Trace with sample inputs to catch errors

Debugging Tips

  • Step through your pseudocode line by line
  • Track variable values at each step
  • Test with different and edge-case inputs

8. Best Practices and Professional Tips (5 minutes)

Industry Standards

  • Always decompose problems into smaller parts
  • Trace and test your algorithms before implementation
  • Document your logic and assumptions

Next Steps in Your Learning Journey

  • Practice designing algorithms for real-world problems
  • Learn about algorithm efficiency and optimization
  • Explore how to implement your pseudocode in a programming language

9. Summary and Review (5 minutes)

Key Takeaways

  1. Decomposition and tracing are essential for solving complex problems
  2. Always check for edge cases and test your logic
  3. Pseudocode is a powerful tool for planning and debugging

Quick Review Questions

  1. Why is tracing an algorithm important?
  2. How do you handle empty or invalid input in your pseudocode?
  3. What is the benefit of breaking a problem into smaller parts?

For Instructors

Teaching Tips

  • Use real-world scenarios for practice
  • Encourage students to trace and debug their pseudocode
  • Discuss the importance of edge cases and thorough testing

Assessment Ideas

  • Have students design and trace algorithms for new problems
  • Peer review: students check each other’s logic and edge case handling
  • Short quiz on decomposition, tracing, and debugging

Adaptation Notes

  • For advanced students, introduce more complex, multi-step problems
  • For online delivery, use collaborative tracing exercises
  • Adjust timing based on student engagement and questions