DSA Patterns Made Simple · Start Here: How to Solve Coding Problems
How to Solve Any Coding Problem in 5 Steps
A simple routine for the moment you read a new problem: understand it, try examples, write the slow version, find the pattern, then test. It works in interviews and in everyday work.
The problem
You open a problem, read it once, and your mind goes blank. Or you start typing right away, get halfway, and realise you misunderstood what was asked. Both happen to almost everyone, and both come from the same cause: no routine for the first five minutes.
Strong problem solvers aren't faster typists. They follow the same short routine every time, so a new problem never feels like a blank page.
Real-world analogy
A doctor doesn't guess a treatment the moment you walk in. They ask questions, check a few things, rule out the obvious, and only then prescribe. The routine is what makes them reliable on a busy day. Your routine for coding problems does the same job.
The idea
Use these five steps, in order, on every problem:
- Understand. Say the problem back in your own words. What goes in, what comes out? Ask about edge cases: empty input, duplicates, negative numbers, very large input.
- Try examples by hand. Work two or three small examples on paper, including one tricky one. You are looking for how you solved it, because the code will do the same thing.
- Write the simple version first. The "brute force" answer, even if it is slow. It proves you understand the problem and gives you something to compare against.
- Find the pattern. Ask: "what work is my simple version repeating?" The answer usually points to a pattern from this course: remembering what you've seen (hash map), two pointers, a sliding window, and so on.
- Test it. Run your examples through the code, out loud if you're in an interview. Then check the edge cases from step 1.
In an interview, talk through every step. Interviewers grade your thinking, not just the final code. A clear simple solution plus "here is how I'd speed it up" often scores better than a silent, half-finished clever one.
The code
Here is the routine on a real problem: "Given a list of numbers and a target, return the positions of two numbers that add up to the target."
Step 3 gives the simple version: try every pair. Step 4 asks what it repeats: for each number, it searches the whole list again for its partner. If we remember the numbers we've already seen, we can find the partner instantly. Both versions are below, with a quick test that compares them.
def two_sum_simple(nums, target):
# Step 3: try every pair. Clear and correct, but slow for big lists.
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
return []
def two_sum_fast(nums, target):
# Step 4: remember each number's position, then look up its partner.
seen = {}
for i, n in enumerate(nums):
partner = target - n
if partner in seen:
return [seen[partner], i]
seen[n] = i
return []
# Step 5: test with normal and tricky cases
tests = [([2, 7, 11, 15], 9), ([3, 2, 4], 6), ([3, 3], 6), ([1, 2], 10)]
for nums, target in tests:
a, b = two_sum_simple(nums, target), two_sum_fast(nums, target)
print(nums, target, "->", b, "same" if a == b else "DIFFERENT")
Output
[2, 7, 11, 15] 9 -> [0, 1] same [3, 2, 4] 6 -> [1, 2] same [3, 3] 6 -> [0, 1] same [1, 2] 10 -> [] same
function twoSumSimple(nums, target) {
// Step 3: try every pair. Clear and correct, but slow for big lists.
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) return [i, j];
}
}
return [];
}
function twoSumFast(nums, target) {
// Step 4: remember each number's position, then look up its partner.
const seen = new Map();
for (let i = 0; i < nums.length; i++) {
const partner = target - nums[i];
if (seen.has(partner)) return [seen.get(partner), i];
seen.set(nums[i], i);
}
return [];
}
// Step 5: test with normal and tricky cases
const show = (v) => JSON.stringify(v).replaceAll(",", ", ");
const tests = [[[2, 7, 11, 15], 9], [[3, 2, 4], 6], [[3, 3], 6], [[1, 2], 10]];
for (const [nums, target] of tests) {
const a = twoSumSimple(nums, target), b = twoSumFast(nums, target);
console.log(show(nums), target, "->", show(b), show(a) === show(b) ? "same" : "DIFFERENT");
}
Output
[2, 7, 11, 15] 9 -> [0, 1] same [3, 2, 4] 6 -> [1, 2] same [3, 3] 6 -> [0, 1] same [1, 2] 10 -> [] same
Notice the tricky cases: [3, 3] checks that a number can't pair with itself, and [1, 2] with target 10 checks the "no answer" path.
Common mistakes
- Coding before understanding. Five minutes on examples saves twenty minutes of rewriting.
- Skipping the simple version. Jumping straight to a clever idea often leads to a buggy one. The simple version is also your test oracle.
- Testing only the happy path. Empty input, one element, duplicates and "no answer" catch most bugs.
- Staying silent in interviews. Say what you're trying and why. If you get stuck, the interviewer can only help if they know where you are.
Key points
- Use the same five steps every time: understand, examples, simple version, find the pattern, test.
- The pattern usually comes from asking "what is my simple version repeating?"
- A correct simple solution is a real result, and a great starting point.
- Test edge cases on purpose: empty, single item, duplicates, no answer.
Practice questions
What are the five steps for approaching a new coding problem?
Answer
Understand the problem (inputs, outputs, edge cases), work small examples by hand, write the simple (brute-force) version, find the pattern by asking what the simple version repeats, then test with normal and edge cases.
Why write the brute-force solution first?
Answer
It proves you understand the problem, gives you a correct answer to fall back on, shows exactly what work is being repeated (which points to the faster pattern), and can be used to check the faster version's results.
Name four edge cases worth testing on almost any array problem.
Answer
Empty input, a single element, duplicates, and the case where there is no valid answer. Negative numbers and very large inputs are also worth asking about.
Open in the app to mark lessons complete, take notes and track your progress.