From Confusion to Solution

Kelly Becker
The Startup
Published in
5 min readOct 5, 2020

The 5 step problem solving strategy to help you approach any algorithm.

Photo by Markus Winkler on Unsplash

Oftentimes when first presented with a problem set and asked to implement an algorithmic solution, it can feel a lot like having a 5,000 piece blank puzzle dumped in front of you with a limited amount of time to somehow make all of those pieces fit together into one finished product.
The situation can seem incredibly daunting, especially during a stressful situation like a timed challenge or an interview. But with the use of a few strategies, problems can be broken down into manageable and understandable steps that help us implement a working solution.
In this blog post we will walk through each of the 5 steps and then use them to solve an actual problem.

How to Approach a Problem

  1. Understand the Problem
    👍 Make sure you can restate the problem in your own words
    👍 Know what the input values are including their type
    👍 What output is expected to come from your solution
    👍 Know what labels you will give to important pieces of data
  2. Explore Concrete Examples
    👍 Start with a simple example to help solidify your approach
    👍 Think of a more complex example
    👍 Explore examples with invalid or empty inputs, how will you adjust. your approach
  3. Break it Down
    👍 Use pseudocode to help you walk through what steps you will take to solve the problem and explicitly state those steps. This will help you think through what code you will write and work out any misunderstandings.
  4. Solve and Simplify
    👍 If you can…Go ahead and Solve it!
    👍 If you can’t…Simplify and solve just the core difficulty
    👍 Write a simplified solution
    👍 Add some difficulty back in
  5. Take a Step Back and Refactor
    👍 Congratulations! You solved it! Appreciate what you have accomplished and enjoy your success
    👍 Now improve your solution, if possible
    👍 Ask yourself questions like…can I improve the performance? or do I understand what my code is doing from a glance?

Let’s Put These Steps to Good Use!

The Problem
A sentence S is given, composed of words separated by spaces.
We would like to convert the sentence to “Goat Latin” (a made-up language similar to Pig Latin.)

The rules of Goat Latin are as follows:

  • If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
    For example, the word 'apple' becomes 'applema'.
  • If a word begins with a consonant, remove the first letter and append it to the end, then add "ma".
    For example, the word "goat" becomes "oatgma".
  • Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
    For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.

Return the final sentence representing the conversion from S to Goat Latin.

Do I Understand the Problem?
Restate the problem in my own words:
I need to loop through this sentence changing each word based on whether it starts with a consonant or vowel. I then need to add additional letter a’s to the end of each word * what index it is at. At the end I return the input sentence reformatted in GoatLatin.
Input Values: String of words separated by spaces
Questions
1.
Do I need to account for capital and lowercase letters?
Possible Labels Needed:
1.
I will need an empty string to add my ‘answer to. This will be returned at the end.
2.
To iterate through each word I will need to split the string on each space to form an iterable array of words.

Explore Concrete Examples:
The provided description gives us pretty concrete examples for how to manipulate each word. It is a good idea to consider what to do if the input value is not a string of words with spaces. Say perhaps an empty string. Here is where you can clarify what needs to be returned in the case of invalid input.
In our case if the input is an empty string, we will return null.

Break it Down!
1.
Check if the input is a valid string.
2.
If it is, create a variable called words that splits the string on each space (this will be an array of strings).
3.
Create a variable called answer that is initialized as an empty string.
4. Iterate through words (use a for loop) checking the first letter of every word.
5. If the first letter is a vowel, add ‘ma to the end of the word. Add the word to answer.
6. Else (the first letter is not a vowel) remove the first letter, add it to the end of the word, then add ‘ma’. Add the word to answer.
7. At the end of each loop through words add lowercase ‘a’ to the end of the word in the amount of variable i + 1.
8. Return answer

LET’S SOLVE THIS!

const toGoatLatin = function(s) {
if(s === '') return null;

let words = s.split(' ');
let answer = "";
for(let i = 0; i < words.length; i++){
if(/^[aeiouAEIOU]\w+/.test(words[i])){
answer += words[i] + 'ma';
} else {
let firstLetter = words[i].charAt(0)
words[i] = words[i].substring(1);
answer += words[i] + firstLetter + 'ma';
}
let additional = i + 1;
answer += 'a'.repeat(additional) + ' ';
}
return answer.trim()
};

Important Things to Remember
1.
Solve one part of the problem before moving onto the next. Example figure out how to add ‘ma’ it the word starts with a vowel before moving forward.
2. Don’t be afraid to ask clarifying questions as they come to you. You will inevitably have some as you start to write code.
3. Planning is crucial and incredibly helpful when tackling a problem, especially when you have the added stress of an interview setting.

Happy Coding Everyone! 😁

The tips in this blog were taken from a book by George Polya called How To Solve It.

--

--

Kelly Becker
The Startup

Solutions Engineer | Former Healthcare Clinician. You can often find me biking around the city in my sparkle helmet or pretending I’m a Top Chef