Live
Auto strategy optimizer — AI improves your edge while you sleep
guideintermediate18 min

Thinking Algorithmically

Develop problem-solving skills: break down problems, write pseudocode, and design solutions.

Last updated: Jan 28, 2026

An algorithm is a step-by-step procedure for solving a problem. Before writing code, you need to figure out the steps. This skill—breaking problems into solvable pieces—is the core of programming.

Problem Decomposition

Big problems are scary. Small problems are manageable. The trick is breaking the big one into small ones.

Example: Build a program that finds the most common word in a text file.

  1. Read the file contents
  2. Split the text into words
  3. Count how many times each word appears
  4. Find the word with the highest count
  5. Return that word

Now each step is a small, solvable problem. You can tackle them one at a time.

Pseudocode

Pseudocode is a way to plan your algorithm in plain English before writing real code:

text
FUNCTION find_most_common_word(filename):
    READ contents of filename
    SPLIT contents into list of words
    CREATE empty dictionary for word counts

    FOR EACH word in words:
        IF word is in dictionary:
            INCREMENT its count
        ELSE:
            SET its count to 1

    FIND the word with highest count
    RETURN that word

Now translating to Python is straightforward:

python
def find_most_common_word(filename):
    with open(filename, "r") as file:
        contents = file.read()

    words = contents.lower().split()
    word_counts = {}

    for word in words:
        if word in word_counts:
            word_counts[word] += 1
        else:
            word_counts[word] = 1

    most_common = max(word_counts, key=word_counts.get)
    return most_common

Common Algorithm Patterns

Accumulator Pattern

Build up a result by processing items one at a time:

python
# Sum all numbers
total = 0  # Accumulator
for num in numbers:
    total += num

# Build a string
result = ""
for word in words:
    result += word + " "

# Collect matching items
evens = []
for num in numbers:
    if num % 2 == 0:
        evens.append(num)

Search Pattern

Look through data to find something:

python
# Find first match
def find_first_negative(numbers):
    for num in numbers:
        if num < 0:
            return num
    return None  # Not found

# Find all matches
def find_all_negatives(numbers):
    result = []
    for num in numbers:
        if num < 0:
            result.append(num)
    return result

# Check if any match
def has_negative(numbers):
    for num in numbers:
        if num < 0:
            return True
    return False

Transform Pattern

Convert each item to something else:

python
# Square each number
def square_all(numbers):
    result = []
    for num in numbers:
        result.append(num ** 2)
    return result

# Or with list comprehension
def square_all(numbers):
    return [num ** 2 for num in numbers]

Working Through a Problem

Let's solve: "Find all pairs of numbers in a list that sum to a target value."

python
# Step 1: Understand the problem
# Input: [1, 2, 3, 4, 5], target = 6
# Output: [(1, 5), (2, 4)]  # pairs that sum to 6

# Step 2: Think of examples
# [1, 2, 3], target = 4 -> [(1, 3)]
# [1, 1, 1], target = 2 -> [(1, 1), (1, 1), (1, 1)]? Or just [(1, 1)]?
# Clarify: do we count duplicates? Let's say yes.

# Step 3: Plan the algorithm
# For each number, check if (target - number) exists in the rest
# If yes, add the pair to results

# Step 4: Write pseudocode
# FOR i from 0 to length-1:
#     FOR j from i+1 to length:
#         IF numbers[i] + numbers[j] == target:
#             ADD (numbers[i], numbers[j]) to results

# Step 5: Implement
def find_pairs(numbers, target):
    pairs = []
    for i in range(len(numbers)):
        for j in range(i + 1, len(numbers)):
            if numbers[i] + numbers[j] == target:
                pairs.append((numbers[i], numbers[j]))
    return pairs

# Step 6: Test
print(find_pairs([1, 2, 3, 4, 5], 6))  # [(1, 5), (2, 4)]
print(find_pairs([1, 2, 3], 4))        # [(1, 3)]
print(find_pairs([1, 1, 1], 2))        # [(1, 1), (1, 1), (1, 1)]

Practice Problems

  1. Find the second largest number in a list (without using sort)
  2. Check if a list is sorted in ascending order
  3. Find all duplicate values in a list
  4. Implement binary search on a sorted list
  5. Generate all permutations of a string

Tags

algorithmsproblem-solvingpseudocodedecomposition
Related documentation