# Breaking Down JavaScript Solutions To Common Algorithmic Questions (Part 1)

Have you ever struggled to develop an algorithm solution on a technical interview? In this short tutorial, we’ll break down three top algorithm coding questions, starting with the brute force method (step-by-step but not necessarily performant) and transitioning to a more optimized, elegant solution.
If you find another solution, feel free to post it in the comments!

Reverse a string

Given a string, return the reverse of it.
Brute Force Solution
We can use the string.substring() method to take each letter of the str parameter, and append it to a new string. The substring method takes one required parameter and one optional parameter.
The first parameter is the index from which you want the substring to start. This is inclusive meaning if you write myString.substring(1), the output will include the first character.
The second (optional) parameter is the ending index. This parameter is not inclusive. This means that your substring will include all characters until this index plus every remaining character to the right of that index.
Another string method we could use in the brute force method would be the string.charAt() method. The charAt method takes one parameter: the index of the character you want to return.
Let’s write two brute-force algorithms for returning the reverse of a string.
// Method 1: Substring
function reverseString(str) {
let reversedString = ”;

/* Loop through this process for each character in the str parameter
In order to get the reverse, we’ll initialize i to str.length
Add each character, starting from the end, to the new string.
*/
for (let i = str.length; i > 0; i–) {
reversedString += str.substring(i, i-1);
}
return reversedString;
}

// Method 2: CharAt
function reverseString(str) {
let reversedString = ”;

/* Loop through this process for each character in the str parameter
In order to get the reverse, we’ll initialize i to str.length – 1
while i is greater than or equal to 0.
Add each character, starting from the end, to the new string.
*/
for (let i = str.length-1; i >= 0; i–) {
reversedString += str.charAt(i);

}
return reversedString;
}

Optimized Solution
One of the quickest inline ways to solve this problem is to split each character in the string into an array index, reverse the items within the array, and turn the items in each index into a concatenated string.
We’ll use the following methods:

string.split() method which splits each character into an array index.

array.reverse() method which reverses an array in place.

array.join() method which concatenates all array values into a string.

You can chain these three functions together for an elegant, inline solution.
function reverseString(str) {
return str.split(”).reverse().join(”);
}

Longest Word

Return the length of the longest word in the provided sentence.
Brute Force Solution
For the first attempt, you can use the string.split(‘ ‘) method to break the individual words within a sentence into array indeces. This tutorial will not account for punctuation, however you could solve for this with a regular expression.
Next, we can iterate through each index of the array and count the number of letters in each word. We can keep track of the longest word value in a variable. If the current word value is larger than the maximum word value currently saved, replace it! Then, just return the variable containing the longest word.
You can loop through the array using a for-loop or the array.forEach() method. I prefer the latter, but I’ve included both below.
// Solution with for-loop
function findLongestWordLength(str) {
let maxVal = 0;

const wordArr = str.split(‘ ‘);

for(let i = 0; i < wordArr.length; i++) { let word = wordArr[i]; if (word.length > maxVal) {
maxVal = word.length;
}
}
return maxVal;
}

// Solution with array.forEach method
function findLongestWordLength(str) {
let maxVal = 0;

const wordArr = str.split(‘ ‘);

wordArr.forEach(word => {
if (word.length > maxVal) {
maxVal = word.length;
}
});
return maxVal;
}

Optimized Solution
To optimize this solution, we will still use the string.split() method to separate each word into an array index.
Next, we’re going to use the array.map() method to carry out some type of expression on the value within each array index. This will return a completely new array, so we’ll save that to a new variable.
For each item in the array, return the length of the string and save it in a new array called arrOfLengths.
Finally, we can use the Math.max(…spreadOperator) method with a spread operator in order to return the integer value for the longest string in a sentence.
function findLongestWordLength(str) {
const arrOfWords = str.split(‘ ‘);
const arrOfLengths = arrOfWords.map(item => item.length);

return Math.max(…arrOfLengths);
}

Array Of Largest Sub-Array Values

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
[1,2,3,4]
[5,18,0,12]
[3,5,12,5]
[28,9,2,34]

Should return => [4,18,12,34]

Brute Force Solution
For the first pass through, we can start with a nested for-loop.
For each item in the outer array, go through its sub array and find the largest value, then push that into a new array.
// For loop
function largestOfFour(arr) {
let arrayOfMaxValues = [];
for (let i = 0; i < arr.length; i++) {
let subArr = arr[i];
let maxSubArrVal = 0;
for (let j = 0; j < subArr.length; j++) {
let currentValue = subArr[j];
if (currentValue > maxSubArrVal) {
maxSubArrVal = currentValue;
}
}
arrayOfMaxValues.push(maxSubArrVal);
}
return arrayOfMaxValues;
}

// For each method
function largestOfFour(arr) {
let arrayOfMaxValues = [];
arr.forEach(subArr => {
let maxSubArrVal = 0;
subArr.forEach(item => {
if (item > maxSubArrVal) {
maxSubArrVal = item;
}
});
arrayOfMaxValues.push(maxSubArrVal);
});
return arrayOfMaxValues;
}

Optimized Solution
We can use the Math.max(…spreadOperator) method with the array.map() method to loop over each item in the outer array, return the max value from within the sub-array, and return that newly created array directly.
function largestOfFour(arr) {
return arr.map(subArr => Math.max(…subArr));
}

I plan to turn this into a series of posts, so if you enjoyed it make sure to follow me for updates!