Solving Coding Problems Was Hard Until I Used this Process
Have you ever stared at a coding problem until your eyes glaze over?
Your brain spins as your confidence drains until you just give up.
At it’s core — the job function of a software developer is to solve problems. Your salary typically grows at the rate you’re able to solve increasingly complex problems.
A junior developer solves a styling problem on the landing page of a website.
The mid-level developer figures out how to integrate a 3rd party library into the app.
A senior level developer proposes a plan to create a new feature for users to save their progress as they fill out a form.
The staff engineer works with multiple teams to create a 1 year plan to develop a custom AI tool for the customer service team.
And here you are, struggling to figure out how to write a function that finds the largest number in an array.
It’s OK.
We all start here.
Instead of waiting for inspiration or memorization to help you figure out how to solve tough problems, let’s break down a step-by-step approach that can be applied to all levels of coding problems.
Step 1: Use your primary coding language: English
Code is an implementation detail.
Before you write a single line of code, re-phrase the problem in the language you probably know best: English.
Do NOT use coding specific terms at all.
At this point, you want to understand what is being asked so you can break the problem down further.
For example, let’s say you’re attempting a LeetCode problem like 2 sum (also.. why are you doing this?)
2 sum is a very common interview question where, given an array of numbers and a target number, you need to find 2 numbers in the array that sum up to the target.
const arr = [2,4,6,7]
const target = 9
function twoSum(arr, target){
// TODO...
}
console.log(twoSum(arr, target)) // should return [2,7] because they sum to 9 ... duh
To get clearer on what it is we need to build, write some sample inputs and expected outputs.
For example, if the target is 9 and you are looking at 2 then what number MUST you find?
- Is that number in the array?
- What if you are looking at the number 6?
- What number is needed now?
- Is THAT number in the array?
You might write out a comment like this:
I need to find a number that when added to the current number I’m looking at, will equal the target. If that number exists, I’m done, if not I need to look at the next number in the array
This should give you some hints as to what to write.
Step 2: Sanity-Check
Congrats — you have something that resembles a set of instructions from step 1.
Before you translate your words to code, do a sanity-check.
Developers are an odd bunch. We over-think and doubt ourselves. To make sure we have made correct assumptions, we need to explain our logic out loud to see if it makes sense. Over the years we’ve gone from explaining code to rubber ducks (hence the term rubber-ducking) to asking our AI overlords.
Sometimes, we even ask other humans.
Crazy, I know.
If you want to work with actual humans to change careers into software, join me at Parsity — a mentorship program for career changers.
If you’re a self taught developer or just don’t have a human in arm’s reach — then use an AI tool like Chat GPT to get feedback on your approach.
Again — NO code at this point.
Copy and paste your comments into Chat GPT to see if you’re off base or on track.
Step 3: Finally, we code
You know what to do but you might not know how to code it.
Start with what you do know.
Look up the rest.
You’ve done the heavy lifting at this point.
There’s a reason why this section is the shortest — writing code is an implementation detail. This is the reason why the CTO can lead a massive tech organization without knowing how to write a line of ReactJS code.
If you are earlier in your career or just not that good at coding yet, then you absolutely need to focus on becoming proficient in your chosen coding language. Try your best not to use any AI tools and instead go old school:
Google search your coding language
+ english description of the problem
Example:
js + how to check if an array includes a certain value
Step 4: Break the code before it breaks
Happy path code is the hallmark of the junior developer.
The happy path is when you create a function and only test it with arguments that return an expected output.
Assume your users have thumbs for fingers and want to embarrass you by ruining your code.
What happens if the input grows 10x?
What if someone passes in a string instead of a number?
At the very least, consider a few variations of the input you might receive.
Now let’s get tactical:
- Create some sample inputs and write what you think will be the output next to them
- Add a debugger in your code to step through the processes line by line
- Run your code with your samples to see if you get the correct output
- Did it work? Why?
- Did it not work? Why?
const arr = [2,4,6,7]
const target = 9
function twoSum(arr, target){
for(let i=0; i<arr.length; i++){
const diff = target - arr[i]
debugger; // check what's the diff maybe?
if(arr.includes(diff)){
return [arr[i], diff]
}
}
// what happens if we get here?
}
/*
Sample Data
------------------------------
arr | target | output
------------------------------
[2,4,6,7] | 9 | [2,7]
[1,100,3] | 101 | [1,100]
[0,0,0,0] | 9 | null
*/
Step 5: There’s a pattern for that
Nearly every problem you’ve encountered has been solved countless times over.
You don’t need to re-invent the wheel.
It’s important to search for patterns to leverage which is easier than ever thanks to tools like Chat-GPT.
Paste your solution into your favorite AI tool and ask if there’s a name for the pattern you’ve used or if there’s a better way to solve it using a common pattern.
Easier said than done
If solving problems was simple, well then they wouldn’t really be problems would they?
Deep, I know.
That being said, this framework I’ve shared can work for you, but you need to mold it to create a strategy that makes sense for the problems YOU are solving.
The main takeaway here is that your ability to think through a problem is at least as important as knowing how to to write out a solution in your specific coding language.
Good luck!