DEV Community

Cover image for Algorithm Problems - Baby Steps in Providing Solutions Using JavaScript
NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on

Algorithm Problems - Baby Steps in Providing Solutions Using JavaScript

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:

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.

Solving Problems With Algorithm

Algorithm or Problem Solving interviews are almost unavoidable today with the current trends in technology.

Algorithm Interviews

In view of this reality, how can you achieve an algorithm to solve a problem?

Steps in Solving an Algorithm Problem

Steps in Problem Solving

  1. UNDERSTAND - Try to understand the problem and break it down into the smallest parts possible.
  2. STRATEGIZE - Find out what tools are available for you in your programming language (We are using JavaScript for the purpose of this article).
  3. 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

Applying the Steps for problem Solving

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;

Enter fullscreen mode Exit fullscreen mode

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("");

Enter fullscreen mode Exit fullscreen mode

Now we can reverse the array using the .reverse() like so:


let reversedWord = splittedWord.reverse();

Enter fullscreen mode Exit fullscreen mode

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("");

Enter fullscreen mode Exit fullscreen mode

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)
   }

Enter fullscreen mode Exit fullscreen mode

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)
   }
}

Enter fullscreen mode Exit fullscreen mode

Someone else can solve the problem with the following code:


function palindrome(word){
   return(word === [...word].reverse().join(""))
}

Enter fullscreen mode Exit fullscreen mode

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...

enjoy the series

25 Algorithm to Sharpen Your Problem Solving Skills

  1. Algorithm 101: 7 Ways to Reverse a String

  2. Algorithm 101: 13 Ways to Count Vowels in a String

  3. Algorithm 101: 6 Ways to Find the Most Recurring Character in a String

  4. Algorithm 101: 8 Ways to Capitalize a Sentence

  5. Algorithm 101: 6 Ways to Check if a Word is a Palindrome

  6. Algorithm 101: 3 Ways to Find Hamming Distance

  7. Algorithm 101: 6 Ways to Find the Longest Word in a Sentence

  8. Algorithm 101: 9 Ways to Search and Replace a Word

  9. Algorithm 101: 3 Ways to Check If Two Words are Anagrams

  10. Algorithm 101: 3 Ways to Create Pig Latin

  11. Algorithm 101: 3 Ways to Check if Two Sentences are Anagrams

  12. Algorithm 202: Array Chunking in 3 Ways

  13. Algorithm 202: Array Merging Without Duplicates in 4 Ways

  14. Algorithm 202: Falsy Bouncer in 4 Ways

  15. Algorithm 202: Where I Belong in 3 Ways

  16. Algorithm 101: 3 Ways to Reverse an Integer

  17. Algorithm 202: 3 Ways to Sum a Range of Values

  18. Algorithm 202 (My Interview Question): Grouping Anagrams in 3 Ways

  19. Algorithm 101: 4 Ways to FizzBuzz a Single Number

  20. Algorithm 101: 2 Ways to FizzBuzz a Range of Numbers

  21. Algorithm 101: 3 Ways to Get the Fibonacci Sequence

  22. Algorithm 101: 2 Ways to Find the Largest Product Yielded by 3 Integers

  23. Algorithm 101 (Interview Question): 2 Ways to Determine if 2 Words are Isomorphic

  24. Algorithm 202 (Interview Question): Matching Parenthesis in 2 Ways

  25. Algorithm 202: Sock Merchant

BONUS

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.

Check out some of the solutions on github

Top comments (6)

Collapse
 
spaceprgr profile image
spaceprgr

Thank you Njoku, for your very helpful post.

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

You are welcome 🤗

Happy to write more 😊

Collapse
 
ericdecol profile image
punk croc

very good

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

😊

Collapse
 
juanfabiorey profile image
juanfabiorey

Thank you Njoku! This is so usefull!

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

I am really happy you found it useful. Thank for reading. 😊