# 3 Different Technical Interview Questions — with JS solutions

Researching for an upcoming technical interview on Glassdoor or whatever is a great way to see the type and level of difficulty for questions that may get brought up during your interview. I had a tech interview recently so here are three problems, two are the ones others got during their interviews and the one I got during mine. The solutions I am putting are not the best-optimized versions but they are what I came up with when given myself a half-hour time constraint.

Question 1: The Palidrome

Given a string tell whether it is a palindrome or not. Luckily I recently went through Stanford’s free Python course https://codeinplace.stanford.edu/ which had a Python solution to the problem. The only difference is that Python has an isalpha() which will check if a character is an alpha or another symbol. We don’t have that in JS so we need to do some regex.

My Solution:

function checkPalidrome(palidrome){const cleaned = palidrome.trim().toLowerCase().replace(/[\W_]/g, '')const reversed = cleaned.split('').reverse().join('')if (cleaned === reversed){return "true"}else{return"false"}}

A better solution:

`function palindrome(str) { str = str.toLowerCase() .replace(/[\W_]/g,"") .split(""); `**return** str.join() === str.reverse().join(); }

In my solution I wanted to account for the input having any side spaces so I trimmed it, I wanted to make everything the same case then I wanted to take out any non-alpha characters. That is the cleaned constant, if it was multiple words it is now one smushed thing of letters containing only lowercase alphas.

Then I took the cleaned constant split it by the letter, reversed it, then rejoined it together.

Then I check if the cleaned-up string and the reversed string are the same.

So this works with racecar, with boob, with Go hang a salami, I’m a lasagna hog.

2: Given an array sort it by positive and negative numbers

Okay so here we have an array like:

`let numbers = [0,1,3,5,8,4,42,66,39,25,8,7,10,0,-2,-3,-5,-7,-7,-7,89,26,33,12,12,14,15,2,3,-68]`

We need to separate it by odds and evens. We are going to do a sort then but in JS by default that sports things by strings, we need to add the compare function to get it to handle numbers.

My Solution:

`numbers.sort(function(a, b) {`

return Math.abs(a % 2) - Math.abs(b % 2) || a - b;

});

Math.abs gets you the absolute value of a number (if positive or zero returns the number if negative returns the positive number). This lets us deal with the negatives in the array. The percent sign is the remainder operator in JS so if you have a statement like 10 % 2 the result will be zero, 10 % 7 would be 3. What the compare function does if the result of the equation is negative in this instance a is sorted before b, if positive b is sorted before a and if the result is zero there is no change. In the case of 66 and 39, (66 % 2 ) is 0 and (39 % 2) is 1 so this would be -1 so the comparison would know to slot the even numbers first then the odd numbers. Running that function gives us:

`[-68, -2, 0, 0, 2, 4, 8, 8, 10, 12, 12, 14, 26, 42, 66, -7, -7, -7, -5, -3, 1, 3, 3, 5, 7, 15, 25, 33, 39, 89]`

Success!

Question 3 Coin Change Problem

Now for the question, I got in an actual interview. It seems like the thing to do is take a problem from cracking the coding and stuff like that and just inverting it. The normal version seems to be this https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/. Instead, though they wanted the maximum number of coins needed to less than or equal to the total. So an array of [1,5,1] and a max of 8 the answer would be 3, with a max of 5 it would be 2.

My Solution:

function maxChange(arr, max) {

sorted = arr.sort((a, b) => { return a - b })

let total = 0

let count = 0

if (max === 0 ){

return 0

}

for (let i = 0; i < arr.length; i++){

if (total <= max) {

if (total + sorted[i] >= max) {

return count

}

total += sorted[i]

count++

}

}

return count

}console.log(maxChange([5,1,1,1,1,10,25], 13))

We once again want to normalize the array so we sort it to get all like coins together. Get some variables going, then take out one of the edge cases. If the max given is zero then we will return zero as the answer. Now we loop through the array, if the total is less than the max given, we then check if the total plus the current value is greater than or equal to the total, if it is we return the count if it is not we add the number we are currently on in the loop to the total and add one to the count, then it moves on.