Algorithm Problems - Baby Steps in Providing Solutions Using JavaScript
NJOKU SAMSON EBERE
Posted on July 19, 2021
Introduction
Recruiters who are interested in the problem solving skills of their candidate, present them problems and ask them to write an algorithm for that problem. By so doing, recruiters are able to separate those who just copy and paste codes from those who really understand the syntax and how it works. But What is an Algorithm?
JUMP TO:
- Steps in Solving an Algorithm Problem
- 25 Algorithm to Sharpen Your Problem Solving Skills
- Bonus
- Conclusion
Algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem, based on conducting a sequence of specified actions. A computer program can be viewed as an elaborate algorithm. In mathematics and computer science, an algorithm usually means a small procedure that solves a recurrent problem - whatis.
From the definition above, we can deduce that algorithm is used to solve problems and for one to build an effective algorithm, the person must have a good understanding of the problem being solved.
Algorithm or Problem Solving interviews are almost unavoidable today with the current trends in technology.
In view of this reality, how can you achieve an algorithm to solve a problem?
Steps in Solving an Algorithm Problem
- UNDERSTAND - Try to understand the problem and break it down into the smallest parts possible.
- STRATEGIZE - Find out what tools are available for you in your programming language (We are using JavaScript for the purpose of this article).
- IMPLEMENT - Solve each of the small problems with those tools and combine the solutions to form one solution to the whole problem.
For Example, if you are presented with a word palindrome problem, how do you go about writing the algorithm to that problem?
Let's apply the steps above
STEP A: UNDERSTAND
The big problem here is to check if the word we are given is the same if reversed.
However, for us to make that happen, we have to provide solutions to smaller problems like:
- How to keep a reference to the original word we are given.
- How to reverse the word.
- But for us to reverse the word we need to split the word into characters.
- If we eventually reverse it, we need to join the characters
- Finally, we need to compare the new word to the old one
STEP B: STRATEGIZE
Some of the tools We have at our displosal include the following:
- variables
- .split()
- spread operator
- if...else... conditional statement
- ternary operator
- .reverse()
- .join()
STEP C: IMPLEMENT
Having broken down this problem into smaller bits, we have a way to keep a copy of the original word given to us using the variable
tool provided for us by JavaScript
const orginalWord = word;
Since we cannot just reverse a string, we need to split the word and make it an array using the .split("")
method
let splittedWord = word.split("");
Now we can reverse the array using the .reverse()
like so:
let reversedWord = splittedWord.reverse();
It is now reversed. But we need to return it to a string without anything separating the characters. So we can use the .join("")
to do that:
let newWord = reversedWord.join("");
Finally, let's compare the word we were given initially, and the one we have now reversed
if(orginalWord === newWord){
console.log(true)
}else{
console.log(false)
}
Final Code
function palindrome(word){
const orginalWord = word;
let splittedWord = word.split("");
let reversedWord = splittedWord.reverse();
let newWord = reversedWord.join("");
if(orginalWord === newWord){
return(true)
}else{
return(false)
}
}
Someone else can solve the problem with the following code:
function palindrome(word){
return(word === [...word].reverse().join(""))
}
Notice how easy it is to Conquer any problem if you divide it. So basically, algorithm is all about how you can divide and conquer!
Practice, they say breeds perfection. So with that in mind, I am presenting you with the following series to help you get better in your problem solving skills. The series was made a while ago when I decided to embrace algorithm. That decision has improved my programming skills generally and I am sure that it will enhance yours too as well as you chances of winning at technical interviews.
So please, enjoy the series...
25 Algorithm to Sharpen Your Problem Solving Skills
Algorithm 101: 6 Ways to Find the Most Recurring Character in a String
Algorithm 101: 6 Ways to Find the Longest Word in a Sentence
Algorithm 101: 3 Ways to Check if Two Sentences are Anagrams
Algorithm 202 (My Interview Question): Grouping Anagrams in 3 Ways
Algorithm 101: 2 Ways to Find the Largest Product Yielded by 3 Integers
Algorithm 101 (Interview Question): 2 Ways to Determine if 2 Words are Isomorphic
Algorithm 202 (Interview Question): Matching Parenthesis in 2 Ways
BONUS
How Many Tutorials Before I become a Pro? - Newbie Asks
3 Things to Help You Land Your First Software Engineering Job
Conclusion
With the forgoing, you can see that algorithm isn't as difficult as it looks. Basically, it is all about solving day to day problems. However without practicing, it will not be easy to solve problems easily.
So I encourage you to follow and practice with the algorithm series presented above for you.
Posted on July 19, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.