[2021] Top 10 Data Structures Algorithms (DSA) Interview Questions and Answers
1. How do you validate the Bracket expressions are valid ?
We can use stack to valildate the valid bracket expressions.
For example:
- [{}], {}, {()}, [({})] – Examples for valid bracket expressions.
- [}, []], ][, ({)} – Examples for invalid bracket expressions.
Try yourself the for the Solution with stack’s basic push and pop, if you are not able to bring the solution, then checkout the sample program here.
2. How do you split the array for the given size ?
Basically we want to divide the array into multiple pieces based on the given size. Inputs and outputs are below:
- Inputs: Array, split count
- Outputs: Divide the above given input array based on the split count into multiple parts.
Here is the javascript program to achieve the same results:
let inputArray = [1,2,3,4,5,6,7,8,9]; let splitCount = 3; let outputArray = []; for (let i=0;i<inputArray.length;i+=splitCount){ let tempArray = []; for(let j=i;j<splitCount+i;j++){ tempArray.push(inputArray[j]); } outputArray.push(tempArray); } console.log(outputArray);
3. write a program for Pig Latin logic
Rules/requirements for pig latin is below,
- If the word starting with vowel then we have to append “way” to that word.
Eg: ice – iceway - If the word starting with consonents then we have to remove the consonent letters and append the removed word along with ay at the end.
Eg: java – avajay
Solution is going to be with simple if and for loop only, Try yourself first, if not able to get the solution, then do checkout here.
4. Write a program for fizzbuzz logic
Fizzbuss rules:
- If the number divisible by 3 then have to display Fizz
- If the number divisible by 5 then have to display Buzz
- If the number divisible by 15 then have to display FizzBuzz.
- If the number not divisible by 3,5 and 15 then have to display that number.
Solution is going to be with simple if and loops only, Try yourself first, if not able to get the solution, then do checkout here.
5. Write a program for Palindrome logic
Logic:
Given Number/String should be equal with the Reversed of the given Number/String.
Examples:
- 101 – Palindrome Number (Given and Reverse of the given is equal (101 and 101 only)).
- 100 – Not a Palindrome Number (Given is 100 and Reverse of the given is 001, both are not equal, so its not a palindrome Number).
- Level – Palindrome String. (Given and Reverse of the given is equal (Level and leveL)).
- Link – Not a Palindrome String. (Given is Link and Reverse of the given is kniL, both are not equal, so its not a palindrome string).
You need to basically derive the two different logics for both numbers and strings. Try yourself first, then check the solution code here.
6. How do you print all the possible combinations of a 3 digit number / character ?
Not worrying about the best approach, then its just a matter of nested for loops. You can find the solution here.
7. Given number is prime number or not.
Here is the prime number check logic:
Logic:
- The number should not be divisible except its own and 1. Which means the given number’s Mod should not be zero (0) with any number.
- So we have to put one loop to check whether any one of the number from 2 with the input given number’s Mod is zero, if then it is not a prime number, else its a prime number.
Try yourself for the solution first, then check the solution here.
8. Write a program to print the pyramid numbers
It is again a matter of few loops and I can also share you the logic overview below, try with the same / check the solution here
- Define the number of rows to print the pyramid.
- Use nested loop to print the rows and columns.
- First outer loop takes the count from the above defined variable.
- Second inner loop takes the count from the outer loop.
- Use print() method instead of println() to get the desired pyramid output.
- Empty println() outside the inner loop to move to next new line.
9. Reverse a string using stack (not with the reverse() inbuilt method)
The idea is to push everything into stack first, then pop everything and append to get the reverse string.
Try yourself first, then checkout this solution here.
10. Sum of the first 5 prime numbers
You can find the prime number finding logic here.
Do additional logic to filter only first few either with simple temp variable / add the first few to list and sum the result of the list.
11. How do you identify the first repeated vowels in the given string ?
Try the solution yourself and put your solution in the comments below to get published here, all the solutions will be published here.
Input: Ngdeveloper is the programming blog Output Expected: e Input: saveji is best coupons website Output Expected: i
12. Rotate the string with the given times right size
Try the solution yourself and put your solution in the comments below to get published here, all the solutions will be published here.
Input: Ngdeveloper, 3 Output: perNgdevelo
13. How do you count the vowels in the given string using Recursions ?
This is easy problem for one who is having good understanding about the stack and recursions. If you are one among them, then try the solution and paste it in our below comment box, if not reached the solution then do checkout our solution here.
Hi I have read a lot from this blog thank you for sharing this information. We provide all the essential topics in Data structure and algorithms system design program Full stack development like, Data science, Python, AI and Machine Learning, Tableau, etc. for more information just log in to our website best data structures and algorithms system design