Interesting JavaScript Problems and Solutions
In this post, we're going to solve some JavaScript problems you may be asked to solve in a technical interview or meet in real live projects.
(1)Two Sum Problem
Given an array of integers, return indices of the two numbers
such that they add up to a specific target.
You may assume that each input would have exactly one solution,
and you may not use the same element twice.
Example:
Given array = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Solution 1
Let's assume the input is:
- array=[1,3,10,11,14]
- goal=13
//O(n^2) time complexity
const twoSum = (array, goal) => {
let indexes = [];
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (arry[i] + array[j] === goal) {
indexes.push(i);
indexes.push(j);
}
}
}
return indexes;
};
This will return an array of [1,2].
It works but if you check it out you'll notice it's running
a loop inside of a loop to figure out which two numbers add up to the goal.
That gives us a time complexity of O(n^2) which is pretty slow
A more optimized solution
- array=[1,3,10,11,14]
- goal=13
//0(n) time complexity
const twoSum = (array, goal) => {
let mapOfNumbers = {};
let twoIndexes = [];
for (let i = 0; i < array.length; i++) {
mapOfNumbers[array[i]] = i;
}
for (let i = 0; i < array.length; i++) {
let target = goal - arr[i];
if (mapOfNumbers[target] !== null && mapOfNumbers[target] !== i) {
twoIndexes.push(i);
twoIndexes.push(mapOfNumbers[target]);
}
}
return twoIndexes;
};
(2) Search Insert position of K in a sorted array
Given a sorted array arr consisting of N distinct integers
and an integer K,the task is to find the index of K, if it's present in the array
arr.
Otherwise otherwise,find the index where K must be inserted to keep the array sorted
You may assume no duplicates in the array.
explanation
Input:
K=5,
const arr = [1, 3, 5, 6];
Output: 2
Explanation: Since 5 is found at
index 2 as arr[2]=5 the ouput is 5
Input: K=2,
const arr=[1,3,5,6]
Output:1
Explanation: Since 2 is not present
in the array but can be inserted at
index 1 to make the array sorted.
Solution
let findIndex = (arr, n, K) => {
//Traverse the array
for (let i = 0; i < n; i++) {
//If K is found
if (arr[i] == K) {
return i;
}
//If current array element exceeds K
else if (arr[i] > K) {
return i;
}
}
//if all elements are smaller than K
return n;
};
//Driver code
let arr = [1, 3, 5, 6];
let n = arr.length;
let K = 2;
console.log(findIndex(arr, n, K));
//Output: 1
Time Complexity:O(N)
Auxiliary Space:O(1)
3)Check whether a triangle is valid or not if the sides are given.
Explanation
Input: a=3, b=8, c=10
Output: Valid
Input: a=3,b=4,c=1
Output: Invalid
Aproach:
A triangle is valid if the sum of its sides is greater than the third side.
If three sides are a,b, and c, then three conditions should be met.
- a+b>c
- a+c>a
- a+c>b
Solution:
function checkValidity(a, b, c) {
if (a + b > c && a + c > b && b + c > a) {
return true;
} else {
return false;
}
}
//Test code
console.log(checkValidity(3, 4, 1));