JavaScript Most Commonly Asked Data Structure Questions
Sanjay Kumar ????
Senior Consultant at EY India | Backend Development Specialist | Solution Architect | Artist | Spiritual
In this article we cover some of the most commonly asked JavaScript data structure questions related to arrays, strings and objects which commonly asked in 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 check if a string or word or number is palindrome ?
Examples of palindrome words are:?racecar, madam.
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 if value/target exists or not in ascending array in O(log n) time complexity ?
NOTE: For doing this you should 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 get total vowel count from 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 prints factorial of any 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 for check number is prime or not ?
Prime Numbers: Those numbers which is divisible by self and 1 only.
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 number is perfect number or not ?
Prime Number:?whose SUM of all factors equal to value expect value itself factor.
领英推荐
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 integer array ?
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 do you remove duplicates from an integer array ?
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)
One question for you, try to solve it by yourself:
Q: Write a program, Selena wants to save money for his first car. She puts money in the ABC bank every day.?She starts by putting in $1 on Monday, the first day. Every day from Tuesday to Sunday, she will put in $1 more than the day before. On every subsequent Monday, she will put in $1 more than the previous Monday.?Given n, return the total amount of money Selena will have in the ABC bank at the end of the nth day.?
You can share your answers to me for verify of your answers if you want.
Let's see some of the most commonly asked theoretical and conceptual questions related to JavaScript in below video:
Video by Education Funda
I hope this article will be helpful for you to improve your skills if yes so don't forget to share it and please share your feedback in comments.
Follow me for more content like this :)
Check Sanjay's Github
~ <> Happy Coding </>
SDE-2 || React|Node|Next|Mongo|AWS
1 年n =10; let total = 0; for(var i = 0; i < n ; i++){ ??let week = parseInt(i / 7) + 1; ??total += week + (i % 7) ??console.log(total, week, i % 7) } console.log(total)