Skip to content

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.

Beginner 10 min read·Lesson 262 of 306 in Python Programming

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

Text
# 1. Understand: input, output, constraints
# 2. Examples and edge cases
# 3. Plan in pseudocode
# 4. Implement small functions
# 5. Test
# 6. Refine
  • Understand before coding.
  • Brute force first, then improve.
  • Turn examples into tests.

Code examples

Examples become tests

Python
def is_palindrome(text):
cleaned = [c.lower() for c in text if c.isalnum()]
return cleaned == cleaned[::-1]
tests = {"Racecar": True, "A man, a plan, a canal: Panama": True, "hello": False, "": True}
for text, expected in tests.items():
print(repr(text), is_palindrome(text) == expected)
Output
'Racecar' True
'A man, a plan, a canal: Panama' True
'hello' True
'' True

Decomposing into steps

Python
def parse(lines):
return [int(x) for x in lines if x.strip().isdigit()]
def average(nums):
return sum(nums) / len(nums) if nums else 0
print(average(parse(["90", "x", " 70 ", "", "80"])))
Output
80.0

More examples

Brute force, then a better solution

Python
def second_largest_sort(nums):
unique = sorted(set(nums))
return unique[-2] if len(unique) >= 2 else None
def second_largest_scan(nums):
first = second = None
for n in nums:
if first is None or n > first:
first, second = n, first
elif n != first and (second is None or n > second):
second = n
return second
for case in [[3, 1, 4, 1, 5], [7, 7, 7], [2, 9], [-1, -5, -3]]:
print(case, second_largest_sort(case), second_largest_scan(case))
Output
[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:

TextOutput
[3, 1, 4, 1, 5] 4 4
[7, 7, 7] None None
[2, 9] 2 2
[-1, -5, -3] -3 -3

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

# start typing immediately and guess the requirements

Prefer

# write three input/output examples first

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

  1. 1
    Apply the method to: return the most frequent word in a sentence, ignoring case.
  2. 2
    Write 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…
Practise this module