Skip to main content

Command Palette

Search for a command to run...

How to Approach a Coding Problem Before Writing Code

A simple problem-solving process that helped me stop jumping straight into code.

Updated
4 min readView as Markdown

Introduction

One of the biggest mistakes beginners make while solving programming problems is opening the editor and immediately starting to write code.

I've done this too.

I would read a problem, understand roughly what it was asking, and start coding.

Sometimes it worked.

Sometimes I got stuck.

And sometimes the logic was actually correct—I had simply misunderstood a small detail in the question.

That made me realize that solving a problem starts before writing the first line of code.

1. Read the Problem Carefully

The first step sounds obvious, but it's surprisingly easy to skip.

Before thinking about the solution, identify:

What exactly is given? What do I need to return or print? What are the constraints? Are there any special cases? What does the example actually demonstrate?

Don't assume what the question is asking.

Read it twice if necessary.

A few extra seconds here can save several minutes of debugging later.

2. Write Down the Input and Output

Try to simplify the problem.

For example:

Input: An array of numbers

Output: The largest number

Now the problem is much easier to reason about.

You're no longer staring at a paragraph of text.

You have a clear objective.

3. Think About the Logic Before the Syntax

Suppose we need to find the largest element.

Don't immediately think:

for (...)

First think:

Take the first element as the current maximum.

Check every other element.

If an element is larger, update the maximum.

At the end, the maximum is the answer.

Once the logic is clear, converting it into C++ becomes much easier.

4. Try a Small Example

Before coding, test the idea manually.

Consider:

[4, 9, 2, 7]

Start:

maximum = 4

Compare:

9 > 4 → maximum = 9 2 > 9 → no 7 > 9 → no

Answer:

9

If you can't explain your approach using a small example, you probably aren't ready to code it yet.

5. Consider Edge Cases

This is where many seemingly correct solutions fail.

Ask yourself:

What if there's only one element? What if all values are equal? What if numbers are negative? What if the input is empty? What happens at the first or last index?

You don't necessarily need a complicated solution.

You just need to know what your solution is assuming.

6. Then Write the Code

Only after understanding the problem should you start implementing.

At this point, the code becomes a translation of your reasoning.

For example:

int maximum = arr[0];

for (int i = 1; i < n; i++) { if (arr[i] > maximum) { maximum = arr[i]; } }

The important part isn't memorizing this code.

It's understanding why it works.

7. Test Before Moving On

Don't consider the problem finished just because the code compiles.

Test it.

Try:

Normal input

Small input

Duplicate values

Negative values

Edge cases

Then ask:

Does the output match what I expected?

If something fails, don't immediately rewrite everything.

Find where your assumption was wrong.

The Process I'm Trying to Build

Instead of:

Read → Code → Get Stuck → Randomly Change Code

I'm trying to follow:

Read

Understand

Break down

Create logic

Test with an example

Code

Test

Review

It's a small change, but it makes problem solving much more deliberate.

One Lesson I'm Learning

Programming isn't just about knowing syntax.

You can know C++.

You can know loops.

You can know arrays.

You can know strings.

But when you're given a completely new problem, your ability to break it down is what matters.

That's the skill I'm trying to build with every problem I solve.

Final Thoughts

I'm still learning and I still make mistakes.

Sometimes the problem isn't difficult.

Sometimes I simply didn't read it carefully enough.

And that's okay.

Every mistake gives me something specific to improve.

So before writing the next solution, I'm trying to remember one simple rule:

Understand the problem first. Write the code second.