JavaScript Most Commonly Asked Data Structure Questions
JavaScript - Education Funda (By Sanjay Kumar)

JavaScript Most Commonly Asked Data Structure Questions

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 </>

Manish Kumar

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)

要查看或添加评论,请登录

Sanjay Kumar ????的更多文章

  • Node.js, RabbitMQ and Docker Realtime App

    Node.js, RabbitMQ and Docker Realtime App

    Let's directly jump into how you can make your first RabbitMQ (Popular Message Broker) application in node.js.

  • Create and Deploy Your AWS SAM Application

    Create and Deploy Your AWS SAM Application

    Let's see How to Create and Deploy Your First Lambda Function on AWS SAM (Serverless Application Modal) You should have…

  • JavaScript Output Based Interview Questions

    JavaScript Output Based Interview Questions

    You mostly seen in JavaScript Interviews there interviewers must asks output based questions in which candidate stuck…

  • Let's See Top 10 React JS Interview Questions

    Let's See Top 10 React JS Interview Questions

    ReactJS is an open-source, component-based front end library responsible only for the view layer of the application. It…

  • Let's Understand Some JavaScript Importance

    Let's Understand Some JavaScript Importance

    If we talk about JavaScript in 2023, it is the most popular programming language as more then 90 % of websites in the…

    1 条评论
  • Top 10 Junior PHP Developer Interview Questions

    Top 10 Junior PHP Developer Interview Questions

    Are you preparing for your Junior PHP Developer Interview if Yes so this article is for you. PHP is a general-purpose…

  • Some of the Best IT companies in Noida

    Some of the Best IT companies in Noida

    Hello Everyone, In this video you will get the information about best it companies in Noida and it is really helpful…

    6 条评论
  • Become ‘Artist’ Is My Passion

    Become ‘Artist’ Is My Passion

    I don’t know from where I was started my career and where I am write now. I think right now I am just finding stability…

  • Dance Choreo India (DCI)

    Dance Choreo India (DCI)

    Dance Choreo India (DCI) is started and founded by 'Sanjay Kumar' in july 2018 with his friend 'Aakash Rastogi' who was…

  • Education Funda

    Education Funda

    Hello Everyone, "Education Funda" is a YouTube channel of mine. In this channel you 'll get videos related to…

社区洞察

其他会员也浏览了