Module 18 · Algorithms and Problem Solving — topic 2 of 18
Problem-solving methodology
A repeatable process for solving programming problems: understand the problem with examples, plan a solution, break it into steps, implement, test edge cases, then review and improve.
Learning objectives
- Apply a step-by-step process to new problems
- Use examples and edge cases to clarify requirements
- Start with a simple solution and improve it
Definition
Problem-solving methodology is a structured approach to turning a problem description into a working, tested program: understand, plan, implement, test and refine.
Introduction
Staring at a blank editor is the hardest part of programming. A method turns 'I have no idea' into a series of small, manageable steps.
Why it matters
Interview questions, coursework and real projects all reward a calm, systematic approach far more than clever tricks.
Detailed explanation
Understand: restate the problem in your own words. What is the input, what is the output, what are the constraints?
Work through examples by hand, including edge cases: empty input, a single item, duplicates, negative numbers, very large inputs.
Plan: describe the steps in plain language or pseudocode before writing code. Ask whether you've solved a similar problem before.
Decompose: split the problem into small functions (parse the input, compute, format the output) that can be written and tested separately.
Start with a straightforward (brute-force) solution that is clearly correct, even if slow.
Test: turn your examples into checks and run them. A failing test pinpoints which step is wrong.
Refine: once it works, look for a more efficient approach, simplify the code and improve names.
If stuck, solve a smaller version of the problem, draw a diagram, or print intermediate values.
Syntax
- Understand before coding.
- Brute force first, then improve.
- Turn examples into tests.
Code examples
Examples become tests
'Racecar' True 'A man, a plan, a canal: Panama' True 'hello' True '' True
Decomposing into steps
80.0
More examples
Brute force, then a better solution
[3, 1, 4, 1, 5] 4 4 [7, 7, 7] None None [2, 9] 2 2 [-1, -5, -3] -3 -3
Step-by-step explanation
- The sorting version is obviously correct and serves as a reference.
- The scanning version does one pass without sorting, tracking the two largest distinct values.
- Running both on edge cases (all equal, two items, negatives) shows they agree.
Expected output
Running “Brute force, then a better solution” prints exactly:
Practical applications
- Technical interviews follow exactly this process.
- Breaking a feature request into tasks before coding.
Best practices
- Write examples and edge cases first.
- Get a correct solution before an optimal one.
- Compare a new solution against a simple reference.
Common mistakes
Coding before understanding
Avoid
Prefer
Misunderstood requirements waste more time than any bug.
Important notes
- George Pólya's book 'How to Solve It' describes a similar four-step method for mathematics.
Key takeaways
- Understand, plan, implement, test, refine.
- Edge cases clarify the problem.
- Simple first, then improve.
Try it yourself
- 1Apply the method to: return the most frequent word in a sentence, ignoring case.
- 2Write a brute-force and an improved solution for finding the first repeated character in a string.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
16 questions cover the “Algorithms and Problem Solving” module. Answers and explanations are revealed after you submit.
- AdvancedWhat is printed?
- AdvancedWhat is the average time complexity of x in my_set?
- AdvancedA loop over a list checks item in other_list for each item. With both lists of length n, the complexity is…