3 Steps For Solving Any Coding Problem

So you’ve hopped on over to CodeWars or CodeKata (or any number of other programming practice websites) and you’ve decided to tackle one of the problems there. You read the instructions and they seem simple enough. So you start writing code and things go sideways. You write some code to solve part of the problem, but then quickly get lost. What do you do next?

This is a problem that my students encounter ALL the time, and one that many engineers encounter periodically. The problem comes from taking a high-level idea and having to drill down into low-level implementation (which I call ‘going from the “30,000 ft view” to the “10 ft view”’). It’s easy to forget the steps you had in mind for solving a problem after you’ve spent an hour trying to debug a for loop.

Luckily, there’s a simple 3-step technique that will not only make you a pro at solving these kinds of problems, but has the added benefit of making your code better documented and more readable.

In this article, I’ll use the term “30,000 foot view” to represent the high-level problem and “10 foot view” to represent the implementation-level details of solving a problem (ie: the exact code that you’ll need to write).

Step 1: Understand the “30,000 ft” View

Take some time to truly understand exactly what the question prompt is asking. What is the problem you’re really trying to solve?

Some problems make this obvious:

Create a function that takes, as input, a string of variable length and returns, as output, the reverse of that string.

In the problem statement above, it’s very clearly defined exactly what the problem expects of you.

Other problem statements are less clear:

Sort a deck of cards. 2 is the lowest rank and K is the highest. The suits are ordered from low to high as follows: clubs, spades, diamonds, hearts.

When you’re faced with a relatively vague problem statement like the above, it can help to re-write the problem statement to make the exact task you need to complete a little more clear:

Create a function that takes, as input, an array of 52 card objects and returns, as output, an array of those same card objects in sorted order. Each card object contains 2 properties: A rank and a suit. The rank is the numeric value of the card, with 2 being the smallest and K being the largest (with a value of 13). The suit represents the grouping of the cards, clubs being the lowest suit, then spades, then diamonds, then hearts. “Sorted Order” means that the cards within a suit are ordered by rank, and the cards in the lowest suit (clubs) appear at the beginning of the array while the cards in the highest suit (hearts) appear at the end of the array.

Rewriting the problem statement will help you reinforce your understanding of the problem, and also make the process of creating an algorithm for the problem much easier. It’s also good practice for the future, when your problems will not be defined nearly as easily or clearly.

Step 2: Document Your Algorithm

Even better, we can use code comments to put these algorithms directly into our code.

Let’s take a look at our “reverse” algorithm from above with the following problem statement:

Create a function that takes, as input, a string of variable length and returns, as output, the reverse of that string.

We’ll start this code by writing the function signature with the inputs and outputs, but the rest we’ll write out in plain english. Note: the following code is written in Javascript, but the technique applies to other languages as well:

Now, we’ll start documenting our algorithm in comments directly within the code. I’m going to use a naive algorithm here just to demonstrate the technique:

You’ll notice here that I’ve indented my comments to show that a step will occur for every iteration through the loop. This also works well for conditionals / “if” statements to indicate which steps will occur in the condition and which will occur outside of the condition.

Step 3: Implement The Algorithm In-Place

The comments make the code very easy to understand, and at the same time have spelled out exactly how I’m going to solve this problem.

That’s it! A simple 3-step technique that can be used to solve just about any programming problem.

Entrepreneur. Engineer. Educator.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store