in the introduction he said he is proficient in javascript, react, nextjs, typescript and later he said he didn't expect these kind of questions Dude, these kind of questions are expected especially when you are with Chirag
We can use 2 pointers, i and j. Initially, i will point to arr1[0] and j will point to arr2[0]. We will then increment the i pointer every iteration, and as soon as the values at index i and j are equal, we will store the value in the result array and increment both the i and j pointers. The loop will end when either pointer goes out of bounds.
I watched it till end. Most of the ReactJs, frontend developer interviews , the first round is completely on the javascript. So, it needs a lot of practice in the initial stage to get command over it. The libraries and frameworks are doesn't matter. Yha, over all the interview is great!
Solutions for 1st question: 1. Using 2 pointers approach (as we know arrays are sorted) 2. Using set approach (Useful when array is not sorted) Example for 1st approach: function intersect(arr1, arr2){ const resArray = [] let arr1Index=0, arr2Index=0; while(arr1Index
Yes intersect is probably the thing you would use in day to day code as well and most of the job is about finding bugs in the code, this interview is very close to what you would do as a developer.
My solution: const intersect = (arr1, arr2) => { const result = []; for(let i = 0; i< arr1.length; i++){ if(arr2.includes(arr1[i])){ result.push(arr1[i]); //remove the item from the second arr const index = arr2.findIndex(item => item === arr1[i]); arr2.splice(index,1) } } return result }
It's not an optimize way, you can also use let idx = arr2.indexOf(arr[i] if( idx !== -1 ){ result.push(arr[i]); arr2[idx]="" } return result but it's better than you but it's also not optimize way, You have to use two pointer technic for optimization
in the introduction he said he is proficient in javascript, react, nextjs, typescript and later he said he didn't expect these kind of questions
Dude, these kind of questions are expected especially when you are with Chirag
We can use 2 pointers, i and j. Initially, i will point to arr1[0] and j will point to arr2[0].
We will then increment the i pointer every iteration, and as soon as the values at index i and j are equal, we will store the value in the result array and increment both the i and j pointers.
The loop will end when either pointer goes out of bounds.
my solution for first question:
function intersect(arr1, arr2) {
let arr = [];
let i = 0;
let j = 0;
let n = arr1.length;
let m = arr2.length;
while(i < n && j < m ) {
if(arr1[i] == arr2[j]) {
arr.push(arr1[i]);
i++;
j++;
} else if(arr1[i] < arr2[j]) {
i++;
} else {
j++;
}
}
return arr;
}
console.log(intersect([1, 2, 2, 3, 4, 4], [2,2, 4, 5, 5, 6, 2000]));
I dont think this much hints are provided at interviews, but good questions.
function intersect(arr1, arr2) {
return arr2.filter((val) => arr1.includes(val) )
}
as simple as this !
I watched it till end. Most of the ReactJs, frontend developer interviews , the first round is completely on the javascript. So, it needs a lot of practice in the initial stage to get command over it. The libraries and frameworks are doesn't matter. Yha, over all the interview is great!
hey chirag bhaiya you are very nice as an interviewer , this isn't we expect our interviewer to that nice
😂😂 interviewers should be nice only 😛
There is three method first one is tow pointer second one is using Hashmap third one is you can use tow loop there is many methods
Yes, absolutely correct but I am talking about the best approach which in more optimized way
Solutions for 1st question:
1. Using 2 pointers approach (as we know arrays are sorted)
2. Using set approach (Useful when array is not sorted)
Example for 1st approach:
function intersect(arr1, arr2){
const resArray = []
let arr1Index=0, arr2Index=0;
while(arr1Index
function intersection(arr1, arr2) {
const result = [];
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i] === arr2[j]) {
if (!result.includes(arr1[i])) {
result.push(arr1[i]);
}
}
}
}
return result;
}
This solution isn't correct. This will only give [2,4] as you are using loop inside loop
function intersect(arr1, arr2) {
const arr=[];
arr1.forEach((el,i)=>{
if(arr2.includes(el)){
arr.push(el)
arr2.splice(arr2.indexOf(el),1)
}
})
return arr
}
const result = intersect([1, 2, 2,3, 4, 4], [2,2, 4, 5, 5, 6, 2000])
console.log(result);
simple and easy
ar1 = [1, 2, 2, 3, 4, 4];
ar2 = [2, 2, 4, 5, 6, 200];
// output: 2 2 4
ans = [];
let i = 0;
let j = 0;
while (i < ar1.length && j < ar2.length) {
if (ar1[i] === ar2[j]) {
ans.push(ar1[i]);
i++;
j++;
} else {
if (ar1[i] < ar2[j]) {
i++;
} else {
j++;
}
}
}
console.log(ans)
Must watch podcast for freshers...the way parth got stuck you can too and this मोदी जी वाला part was epic 😂😅...
❤️
if anyone want find this all this quetions, where can we can get it?
I have a question arise , all the questions have been asked here , are these questions does we need on regular day to day work?
Yes intersect is probably the thing you would use in day to day code as well and most of the job is about finding bugs in the code, this interview is very close to what you would do as a developer.
let arr1 = [1,2,2,3,4,4];
let arr2 = [2,2,4,5,5,6];
let i=0; let j=0;
let n1 = arr1.length;
let n2 = arr2.length;
let res = [];
while(i
DSA walaa spotted
Here is my solution function intersect(arr1,arr2){
let j=0;
let k=0;
res=[];
while(j
Modi Ji ki entry was epic in this video 😅
😜
function intersect(arr1,arr2) {
let obj1 = {}
let obj2 = {}
let resultArray = []
for(let i = 0;i
Bechara Fresher
function intersect(arr1,arr2){
const resArr = [];
const tempArr = arr2;
for(let i=0; i
function intersect(arr1, arr2){
const intArr = []
let i = 0;
let j = 0;
while(arr1[i] && arr2[j]) {
if(arr1[i]==arr2[j]){
intArr.push(arr1[i])
i++;
j++
} else if(arr1[i]
Clearly rejected
In Mahabharat karna's wheel was stuck......here parth is got stuck 😂
My solution:
const intersect = (arr1, arr2) => {
const result = [];
for(let i = 0; i< arr1.length; i++){
if(arr2.includes(arr1[i])){
result.push(arr1[i]);
//remove the item from the second arr
const index = arr2.findIndex(item => item === arr1[i]);
arr2.splice(index,1)
}
}
return result
}
It's not an optimize way, you can also use
let idx = arr2.indexOf(arr[i]
if( idx !== -1 ){
result.push(arr[i]);
arr2[idx]=""
}
return result
but it's better than you but it's also not optimize way, You have to use two pointer technic for optimization