Lesson 39/48 · 🎯 Problem Solving
🎯 Problem SolvingLesson 39/48
Phase 6 · Problem Solving20 min
The 6-Step Problem-Solving Framework
Understand → Examples → Brute Force → Optimize → Code → Test
Most programmers stare at a problem and immediately start typing. This is wrong. The best programmers follow a framework that prevents wasted effort and catches bugs before they happen.
The 6 Steps:
1. Understand, Restate the problem in your own words. What are the inputs? What should the output be?2. Examples, Write 3 examples by hand (including edge cases: empty input, single item, negative numbers)3. Brute Force, Find ANY working solution. Don't optimize yet. Just make it work.4. Optimize, Only after Step 3 works: can you do it faster or with less code?5. Code, Now write the actual code following your plan6. Test, Run your examples from Step 2. Add edge cases.
1. Understand, Restate the problem in your own words. What are the inputs? What should the output be?2. Examples, Write 3 examples by hand (including edge cases: empty input, single item, negative numbers)3. Brute Force, Find ANY working solution. Don't optimize yet. Just make it work.4. Optimize, Only after Step 3 works: can you do it faster or with less code?5. Code, Now write the actual code following your plan6. Test, Run your examples from Step 2. Add edge cases.
The most common mistake: Skipping to Step 5 (Code) immediately. You'll write faster code by planning slower. Experienced engineers spend 60% of time in Steps 1-3.
Framework applied: Find most common elementpython
# Step 1: Understand
# Input: a list of items. Output: the item that appears most often.
# Step 2: Examples
# [1, 2, 2, 3] → 2
# ["a", "b", "a"] → "a"
# [1] → 1 (edge case: single item)
# Step 3: Brute Force
def most_common(items):
if not items:
return None
counts = {}
for item in items:
counts[item] = counts.get(item, 0) + 1
return max(counts, key=counts.get)
# Step 6: Test
print(most_common([1, 2, 2, 3])) # 2
print(most_common(["a", "b", "a"])) # a
print(most_common([1])) # 1🌍In the Real World...
Every system design interview, every code review, every production incident, you will use this framework. Engineers who skip steps 1-3 spend 10x longer debugging.
Practice Exercises
0/3 solvedExercise 1 of 3easy
⏱ 00:00Apply the Framework: Find Max Difference
Apply the 6-step framework to find the maximum difference between any two numbers in a list.
Given: nums = [3, 10, 2, 8, 1]Max difference = 10 - 1 = 9
Expected output:
Given: nums = [3, 10, 2, 8, 1]Max difference = 10 - 1 = 9
Expected output:
Max difference: 9solution.py
1 / 3
Exercise 2 of 3easy
⏱ 00:00Frequency Counter
Count how many times each word appears in a sentence.
Given: sentence = "the cat sat on the mat the cat"Expected output:
Given: sentence = "the cat sat on the mat the cat"Expected output:
the: 3cat: 2sat: 1on: 1mat: 1solution.py
2 / 3
Exercise 3 of 3medium
⏱ 00:00Is It a Palindrome?
A palindrome reads the same forwards and backwards.
- "racecar" → True- "hello" → False- "madam" → True
Write a function
Expected output:
- "racecar" → True- "hello" → False- "madam" → True
Write a function
is_palindrome(s) and test it.Expected output:
TrueFalseTruesolution.py
3 / 3
Solve all 3 exercises to unlock completion