Basic JavaScript Algorithms part 1

Samanja Cartagena
4 min readJan 17, 2021

Algorithms are an important aspect of coding interviews. But why is that? Mastering algorithms is not a joke. It can make the entire interview process way more difficult than it has to be. Now what could be the purpose for interviewers to even ask these questions. I was under the impression that perhaps they want to discourage too many people from applying for the job and encourage only the well practised, well versed coders for the position. But the truth is they ask these questions for very important reasons. Under the hood the building blocks of all software programs are algorithms. The weather app collects data in Fahrenheit and converts it to Celsius using algorithms. Big companies provide services that are contingent on the optimisation of algorithms. For example, Dijkstra’s algorithms is used by Google Mapping services to calculate the shortest route towards a destination. Many e commerce companies use merge sort to fetch product information from the database. Search games use A star search algorithms very often. Many front end developers underestimate the importance of algorithms. Then they can be very shocked at being asked to implement algorithms in JavaScript.

Here are some of the most common JavaScript white board interview questions. While there are many solutions to these problems. I have provided my favorite ones.

  1. How do you reverse a string?
JavaScript algorithms
Reverse a string in JavaScript
Join Method JavaScript because the reverse() leaves it as char unless it is joined
JavaScript chaining

Here is an alternative way of reversing a string

JavaScript reverse a string

But the for loop can be more erroneous especially in whiteboards. So the better one is the .reverse() method. Not to mention that for loops are usually Big O(n) . It is always better to avoid for loops as an interview answer.

Another commonly asked question is about validating a palindrome. A palindrome is a word or phrase that reads the same spelled backwards. For example madam, mom or tacocat.

Validating a palindrome JavaScript

After going through all these reverse functions you must be thinking that reversing an integer is another possible interview question. Yes, it is and the algorithm for reversing an integer is connected to reversing a string.

Reversing an integer by reversing a string in JavaScript
Reversing a negative number in JavaScript

Enough of reversing strings and integers. What if we are told to capitalise letters. This is an example that capitalises the first letter of a sentence.

Capitalise the first letter of each words of a sentence in JavaScript
joining an array back into a sentence JavaScript

Here is a cleaner and an alternative method of capitalising the first letter of each word of a sentence. Like I mentioned before I am not fond of using for loops especially for white board interviews.

using map instead of a for loop in JavaScript

Another common question is returning the maximum number of characters present in a string. While there are old school methods of doing this. I like to use something called a Character Map.

using Character Map in JavaScript

While I have discussed the most commonly asked interview questions. I will talk about more advanced ones in the next blog.

Happy Coding

Samanja

--

--