In order to understand how to implement an Algorithm, we first need to conceptually understand what an Algorithm is. An Algorithm is a series of steps that you expect will arrive at a specific solution. Writing a program does not equal expressing code, that idea ignores and neglects the entire idea of writing code to solve a problem.
Especially in a job interview, they’ll ask you a problem that can be interpreted in many ways. They are looking for how you respond to those problems. Don’t assume you know what the interviewer/your problem is meaning. It’s almost inevitable that you’ll go down the wrong path, but you need to get good at pivoting back on the right path. Figuring out that you have spent most of the morning wasting your time is what programming is about.
Don’t put rigorous constraints on yourself. When people are thinking about the problem, often times they write steps, they then have a big list of steps that doesn’t necessarily coincide with the steps of an Algorithm. It’s OK to write down loose ideas of your plan, but acknowledge that you’re still just thinking about the problem. The list of ideas that you created aren’t the new 10 commandments to solving your problem.
Write a Test that Would Verify a Solution: If you’re doing TDD, which you should be, you’re going to sort of invest in writing a test that does say what validates a correct solution. Skip this step if you’re not doing TDD. In reality, you’ll probably skip this step when you do a rough draft, then you should come back to it later.
Come up with a sequence of steps that you can explain to a very obedient preschooler. Don’t use pronouns and be very specific. There should be no room for confusion for what these steps really mean.
Optionally, verify each step in the process manually/mentally for some simple input. Be careful to not confuse what SHOULD happen with what is ACTUALLY happening. Pretend like you have no idea what the pseudocode is doing when you run through it.
This step, believe it or not, is deterministic. The process of translating from one definitely clear solution in english can be easily translated to a correct programming language.
Punch Line: If you find yourself diving right into code and it doesn’t work, you’re probably skipping these early steps.