DEV Community

Vigowebs
Vigowebs

Posted on

5

Commonly Asked Coding Problems in JavaScript Interviews

JavaScript is changing every year. There are always new libraries, new frameworks and new things around it. Every year new developers are coming to learn this language and the jobs are increasing so as the interviews.

This post contains some practical and commonly asked coding problems that can be found in any technical interviews. So be not surprised while facing them.

Remove duplicate element from an array

Using Array.filter method, check each element's index is equal to the indexOf value of the array.

function removeDuplicate(arr) {
return arr.filter((element, index, array) => array.indexOf(element) === index);
}

Or in ES6 using set:

function removeDuplicate(arr) {
return Array.from(new Set(arr))
}

Reverse a string without native methods

Given a string, print the reverse of the string (ex: javascript becomes tpircsavaj).

Without native methods:

function reverse(str){
var rtnStr = [];
if(!str || typeof str != 'string' || str.length < 2 ) return str;
for(var i = str.length-1; i>=0;i--){
rtnStr.push(str[i]);
}
return rtnStr.join('');
}

Using recursion:

function reverse (str) {
if (str === "") {
return "";
} else {
return reverse(str.substr(1)) + str.charAt(0);
}
}

Find the missing number

Given a unsorted array of numbers 1 to 100 excluding one number, find the missing number.

The sum of a linear series of n numbers is equal to n*(n+1)/2.

function missingNumber(arr){
var n = arr.length+1,
sum = 0,
expectedSum = n* (n+1)/2;
sum = arr.reduce((total, num) => total + num);
return expectedSum - sum;
}
missingNumber([5, 2, 6, 1, 3]);
// 4

Permutations of a string

Get all permutations of a string

function permut(string) {
if (string.length < 2) return string; // This is our break condition
var permutations = []; // This array will hold our permutations
for (var i=0; i<string.length; i++) {
var char = string[i];
// Cause we don't want any duplicates:
if (string.indexOf(char) != i) // if char was used already
continue; // skip it this time
var remainingString = string.slice(0,i) + string.slice(i+1,string.length);
for (var subPermutation of permut(remainingString))
permutations.push(char + subPermutation)
}
return permutations;
}
let permutations = permut('xyz');
// ["xyz", "xzy", "yxz", "yzx", "zxy", "zyx"]

Check sum of two

From a unsorted array, check whether there are any two numbers that will sum up to a given number.

function sumFinder(arr, sum){
var len = arr.length;
for(var i =0; i<len-1; i++){
for(var j = i+1;j<len; j++){
if (arr[i] + arr[j] == sum)
return true;
}
}
return false;
}
sumFinder([6,4,3,2,1,7], 9);
// true
sumFinder([6,4,3,2,1,7], 2);
// false
view raw checkSum1.js hosted with ❤ by GitHub

Another way of doing, have an object where we will store the difference of sum and element. And then when we get to a new element and if we find the difference is the object, then we have a pair that sums up to the desired sum.

function sumFinder(arr, sum){
var differ = {},
len = arr.length,
substract;
for(var i =0; i<len; i++){
substract = sum - arr[i];
if(differ[substract])
return true;
else
differ[arr[i]] = true;
}
return false;
}
sumFinder([6,4,3,2,1,7], 9);
// true
sumFinder([6,4,3,2,1,7], 2);
// false
view raw checksum2.js hosted with ❤ by GitHub

Brackets match

For the given string, determine if the strings of brackets in the input is valid or invalid by these criteria.

"([)]" // false
"()" // true

The solution is

function isBalanced(str) {
var i, ch;
var bracketsMap = new Map();
bracketsMap.set(']', '[');
bracketsMap.set('}', '{');
bracketsMap.set(')', '(');
// Use the spread operator to transform a map into a 2D key-value Array.
var closingBrackets = [...bracketsMap.keys()];
var openingBrackets = [...bracketsMap.values()];
var temp = [];
var len = str.length;
for (i = 0; i < len; i++) {
ch = str[i];
if (openingBrackets.indexOf(ch) > -1) {
temp.push(ch);
} else if (closingBrackets.indexOf(ch) > -1) {
var expectedBracket = bracketsMap.get(ch);
if (temp.length === 0 || (temp.pop() !== expectedBracket)) {
return false;
}
} else {
// Ignore the characters which do not match valid Brackets symbol
continue;
}
}
return (temp.length === 0);
}

This post contains only a handful of examples from our recently published app JS Code Samples. This app contains many examples varying from variable scope to coding problems like these. You can download the app from the below link.

Play Store link

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs