5 JavaScript interview questions and solutions
Edward Odhiambo
Full-Stack Engineer | JavaScript, React, NextJS, Redux, Rails & Databases | Docker, Kubernetes and AWS
In this guide, we will go through some examples of interview questions that are popular and that you can find in most coding challenge websites.
Let's dive into them straight away.
Two sum problem
In this problem, you are given an array and target value. The challenge requires you to find two numbers in the array that when added will give you the target value.
Let's look at how we can solve this problem.
const solve = (arr, target) => {
let answ = []
for (let i = 0; i < arr.length; i ++) {
for (let j = 0; j < arr.length; j ++) {
if (arr[i] + arr[j] === target) {
answ.push(arr[i], arr[j])
}
}
}
cosole.log(answ)
}
solve([2, 4, 1, 7, 8, 9], 12)
// sln [4, 8]
Here, we have used to for loops to tackle the problem. The first one allows us to access each value from the array and as we loop through the values in the second loop, we look whether the value at j position plus the value at i position adds up to our target.
In this case, our result will "[4, 8]"
Product values problem
In this problem, you are given an array of numbers and you are required to generate a new array containing products of the values in that array except the value at that position.
For example,
const solve = (arr) => {
let sln = []
for (let i = 0; i < arr.length; i ++) {
let vals = arr.filter((val) => arr.indexOf(val) !== i)
let product = vals.reduce((a, b) => a * b)
sln.push(product)
}
console.log(sln);
}
solve([1, 2, 3, 4])
In this solution, we loop through the array and then filter out values in the array whose position does not match the current position in the loop. This gives us values other than those in the current ith position of the loop.
We can get the product of the values with the reduce function then push the result to the sln array.
Palindrome problem
In this problem, you are told to implement a function that checks whether a string is a palindrome which in this case means a string that can be read the same way backward and forward.
An example of this is like "dad" and "mum".
Let's implement the function.
const solve = (str) => {
let back = str.split("").reverse().join("")
return str === back
}
solve("dad")
In the solution above, we first of all split the string into an array so that we can access the reverse method which reverses the order of values. We can then join the values to form a new string.
We now check whether "str" is equal to the new string "back". This will return a boolean whether "true" of "false".
领英推荐
Stack implementation
A Stack is a popular data structure that is mostly used can be asked at times in interviews. They will usually ask you to implement one.
We will implement it as a JavaScript class. We will implement two methods, push: which adds a value to the top of the stack and peek which returns the last value in the stack.
class Stack {
constructor () {
this.data = {}
this.size = 0
}
push (item) {
this.data[this.size] = item
this.size += 1
}
peek () {
return this.data[this.size - 1]
}
}
Once we have created the stack, we can now use it.
const stack = new Stack()
stack.push(23)
stack.push(34)
console.log(stack.peek());
Pushing a value to the stack adds it to the top position in the stack while peek gives us the last added value.
The fibonacci problem
This problem requires you to return the value at a given position of the fibonacci sequence. We will implement this using recursion.
const calculate = (target) => {
if (target <= 1) {
return 1
}
else {
return calculate(target -1) + calculate(target - 2);
}
}
console.log(calculate(20));
This function will give us the value at position 20 of the fibonacci sequence. This function can take long depending on the position that you specifiy.
Let’s Connect