In this blog, i will share the DSA based most asked interview questions. If you are planning to target product based company these questions for you.
In development, For a single problem, there can be multiple solutions. There is no boundaries to stuck with one method.
But when it comes to time and complexity then you have to choose a better solution that checks all the required boxes.
01 Problem - Checking pair that sum zero from an array
const inputArray= [-5,-4,-3,-2,0,2,4,6,8]
function getSumPairZero(array){
for(let number of array)
{
for(j=1;j<array.length;j++){
if (number + array[j]===0){
return [number, array[j]]
}
}
}
}
const result= getSumPairZero(inputArray);
console.log(result) // [4,-4]
02 Problem - String Anagram ( "hello"--->"llheo")
*Note:-
1:- if string 01 and String 01 letters aren't equal then we could not find an anagram.
2:- Not only letters, but characters should also be the same.
function isAnagram(str1,str2){
if(str1.length!=str2.length){
return false;
}
let count={}
for (let letter of str1){
count[letter]= (count[letter] ||0 ) +1 ;
}
for ( let items of str2){
if(!count[items]){
return false
}
count[items]-=1;
}
return true
}
const output=isAnagram("hello","llheo");
console.log(output);
// true
03 Problem - Find unique array
const a = [1,2,3,4,4,4,5,5,6,6,7,8,7];
const b=[];
for (let val of a){
if(!b.includes(val)){
b.push(val);
}
}
console.log(b);