JavaScript Data Structure Interview Questions (and Answers)

Emma Delaney
3 min readMay 14, 2024

--

In this article, we cover some of the most frequently asked questions about JavaScript data structure, related to arrays, strings, and objects, that are frequently asked during interviews at Tier 1 companies.

1. Custom sorting program in JS via Bubble Sort?

let unSortArr = [4,-1,34,09,-9,103]

const sortArr = (inputArr) => {
for(let i=0; i<inputArr.length; i++)
{
for(let j=i+1; j<inputArr.length; j++)
{
let temp = inputArr[i];
if(inputArr[i] > inputArr[j])
{
inputArr[i] = inputArr[j];
inputArr[j] = temp;
}
}
}
return inputArr;
}

console.log(sortArr(unSortArr));

2. Write a program to test whether a string, word, or number is a palindrome?

Examples of palindromic words are: race car, sir.

const isPlaindrome = (inputChar) => {
let str = inputChar.toString();
let resultWord = '';
for(let i=str.length-1; i>=0; i--)
{
resultWord += str[i];
}
return (resultWord == str) ? true : false;
}
console.log(isPlaindrome('racecar'))
console.log(isPlaindrome('abc'))
console.log(isPlaindrome(121))

3. Write a program to check whether the value/target exists or not in an increasing array with time complexity O(log n)?

NOTE: For this you need to know the binary search algorithm.

const customInArray = (sortedArray, key) => {
let start = 0;
let end = sortedArray.length - 1;

while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (sortedArray[middle] === key) {
return true;
} else if (sortedArray[middle] < key) {
start = middle + 1;
} else {
end = middle - 1;
}
}
return false;
}

console.log(customInArray([1,3,5,6,9,14,29,57,89],29));

4. Write a program to find the total number of vowels in String?

const getVowelCount = (inputStr) => {
let totalVowelCount = 0;
for(let i=0; i<inputStr.length; i++)
{
let char = inputStr[i];
if(char == 'a' || char == 'e' || char == 'i' || char == 'o' || char == 'u')
totalVowelCount++;
}
return totalVowelCount;
}
console.log(getVowelCount('hello how are you today programiz website?'))

5. Write a program to generate factorials of an arbitrary number?

const getFactorial = (inputNum) => {
let result = 1;
for(let i=1; i<=inputNum; i++)
{
result *= i;
}
return result;
}
console.log(getFactorial(5));

6. Write a program to check whether a number is prime or not?

Prime numbers: those numbers that are divisible by themselves and only by 1.

const isPrime = (inputNum) => {
let result = true;
for(let i=2; i<inputNum; i++)
{
if(inputNum%i === 0)
result = false;
break;
}
return result;
}
console.log(isPrime(17));
console.log(isPrime(18));

7. Write a program to check whether the number is perfect or not?

Prime number: whose SUM of all the factors is equal to the expected value, the value of the factor itself.

const isPerfectNum = (inputNum) => {
let result = true;
let factSum = 0;
for(let i=1; i<inputNum; i++)
{
if(inputNum % i == 0)
factSum = factSum+i;
}
return (factSum == inputNum) ? true : false;
}
console.log(isPerfectNum(6));
console.log(isPerfectNum(10));
console.log(isPerfectNum(28));

8. Write a program to find duplicate numbers in an array of numbers?

const findDuplicateEle = (inputArr) => {
let duplicateEleArr = [];
let uniqueArr = [];
for(let i=0; i<inputArr.length; i++)
{
if(!uniqueArr.includes(inputArr[i]))
uniqueArr.push(inputArr[i])
else
duplicateEleArr.push(inputArr[i])
}
return duplicateEleArr;
}
console.log(findDuplicateEle([1,2,3,5,3,1,9]));

9. How to remove duplicates from an array of integers?

const removeDuplicateEle = (inputArr) => {
let uniqueArr = [];
for(let i=0; i<inputArr.length; i++)
{
if(!uniqueArr.includes(inputArr[i]))
uniqueArr.push(inputArr[i])
}
return uniqueArr;
}
console.log(removeDuplicateEle([1,2,3,5,3,1,9]));

10. We have group of people in the form of array and you have to group people basis upon age ?

let peopleArr = [
{name: 'A', age: 10},
{name: 'B', age: 17},
{name: 'C', age: 14},
{name: 'D', age: 10},
];

let resultObj = {};
for(let i=0; i<peopleArr.length; i++)
{
if(resultObj[peopleArr[i].age]){
resultObj[peopleArr[i].age].push(peopleArr[i].name);
}else{
resultObj[peopleArr[i].age] = [peopleArr[i].name];
}
}
console.log(resultObj)

--

--