Module 18 · Algorithms and Problem Solving — topic 18 of 18
Common problem-solving patterns
Reusable patterns for solving problems: brute force, hashing, prefix sums, greedy choices, divide and conquer, recursion with memoisation (dynamic programming), and how to recognise which pattern fits.
Learning objectives
- Name and apply common algorithmic patterns
- Recognise clues that suggest each pattern
- Compare brute-force and optimised solutions
Definition
Problem-solving patterns are general strategies that apply to many different problems. Recognising which pattern fits a new problem is often the key step towards an efficient solution.
Introduction
Experienced programmers rarely invent solutions from scratch. They recognise that a new problem looks like one they've solved before and apply a familiar pattern.
Why it matters
Learning a handful of patterns makes unfamiliar problems approachable and is the core of preparing for coding interviews and competitions.
Detailed explanation
Brute force tries every possibility. It's a correct baseline and fine for small inputs.
Hashing (sets and dictionaries) answers 'have I seen this?' or 'how many?' in O(1), replacing inner loops — clue: duplicates, pairs, counts.
Prefix sums precompute running totals so any range sum is one subtraction — clue: many range queries on fixed data.
Greedy algorithms make the locally best choice at each step (largest coin first). They are fast but only correct when the problem has the right structure.
Divide and conquer splits the problem, solves parts recursively and combines them — clue: the answer for the whole follows from answers for halves (merge sort, binary search).
Dynamic programming solves overlapping subproblems once and reuses the results, via memoisation (lru_cache) or a table — clue: a recursive solution that repeats the same calls.
Two pointers, sliding windows, stacks, queues and BFS (earlier lessons) are patterns too.
To choose: look at the constraints (input size), the structure (sorted? nested? ranges?) and the question (count, best, exists?).
Syntax
- Start with brute force.
- Look for clues in the problem.
- Pick the pattern that removes repeated work.
Code examples
Hashing: first repeated element
5 None
Prefix sums for range queries
[0, 3, 4, 8, 9, 14, 23, 25] 10 25
More examples
Greedy, dynamic programming and divide and conquer
{2000: 1, 500: 1, 200: 1, 50: 1, 20: 1, 10: 1, 5: 1, 1: 1}
89 165580141
23Step-by-step explanation
- Greedy change takes as many of the largest note as possible, which is optimal for the Indian rupee denominations.
- ways(40) would take hundreds of millions of calls without memoisation; with lru_cache each value is computed once.
- max_dc finds the maximum of each half and combines them — the shape of merge sort and many divide-and-conquer algorithms.
Expected output
Running “Greedy, dynamic programming and divide and conquer” prints exactly:
Practical applications
- Caching and dynamic programming in route planning and text diffing.
- Greedy scheduling and compression algorithms.
Best practices
- Write a brute-force reference first.
- Look for repeated work to eliminate.
- Verify greedy choices with counterexamples.
Common mistakes
Greedy where it fails
Avoid
Prefer
Greedy choices are only optimal for some coin systems; check before relying on them.
Important notes
- This completes the Algorithms module; next comes debugging, testing and writing maintainable code.
Key takeaways
- Brute force, hashing, prefix sums, greedy, divide and conquer, dynamic programming.
- Clues in the problem suggest the pattern.
- Remove repeated work to gain efficiency.
Try it yourself
- 1Solve 'minimum coins for an amount' with dynamic programming for any coin set.
- 2Use prefix sums to answer several 'total sales between day i and day j' queries.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
2 questions test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhat is printed?
- AdvancedWhat is printed?