How hard can be this JavaScript Interview Question from UBER?

preview_player
Показать описание
JavaScript Interview Question - 68 | In this video, we will see how to solve a medium-difficulty problem asked in a frontend engineer interview to SDE1, SDE2, and SDE3.

We have to implement a function named mapLimit in JavaScript that takes an array of async tasks (promises) and a callback function as input, executes a limited number of tasks in batches, and then after each promise is settled invokes the callback function with the result of passed and failed results.

You can expect this frontend system design/coding question in Rippling, Uber, Flipkart, Atlassian, Meta, Google, Microsoft, Dropbox, TCS, Infosys, Wipro, Cognizant, Capgemini, Accenture, Nvidia, Nutanix, and other product-based organizations' interviews.

Loved the question? I have 120+ solved problems in my ebook "JavaScript Interview Guide". If you are preparing for Interviews and looking for a solutions book, Get my E-book.

Social links
Рекомендации по теме
Комментарии
Автор

A little bit longer but more understandable solution, also it aways give ordered output according on the input list

const chop = (input, limit) => {
let res = []
let i = 0
let k = 0
while (i < input.length) {
let temp = []
while (k < limit) {
if (i == input.length) {
break
}
temp.push(input[i])
k++
i++
}
res.push(temp)
k = 0
}
return res
}


function getNamebyId(id, callback) {
const rRt = Math.floor(Math.random() * 100) + 200;

setTimeout(() => {
callback('User' + id)
}, rRt)

}

const performOp = async (choppedInput, iterateeFn) => {

try {
let resultList = [];

for (let i = 0; i < choppedInput.length; i++) {
for (let j = 0; j < choppedInput[i].length; j++) {
const result = await new Promise((resolve) => {
iterateeFn(choppedInput[i][j], resolve);
});

resultList.push(result);
}
}

return resultList;
} catch (error) {
throw error;
}
}


async function mapLimit(inputs, limit, iterateeFn, callback) {
const choppedInput = chop(inputs, limit)
const res = await performOp(choppedInput, iterateeFn)
callback(res)
}

mapLimit([5, 3, 2, 4, 1], 2, getNamebyId, (allResults) => {
console.log("output", allResults)
})

sagarmusic
Автор

I would like to take personal guidance from you prashanth bhai for FE-2 roles

chandrasekharmandapalli
Автор

I think you shouldn't use any helper functions like reduce as they are not allowed in interviews most of the time.

aakashsrivastava
Автор

Greetings, I have seen you async sequence video where you finally mentioned sequence can be done in async await way also, if we could do the same for this problem it will avoid the complexity of reduce right, I tried but I'm lacking when again durning the async parallel inside that await part, do you have another video for during this with async await way please?

harishrm
Автор

Why are you batching here ? Max limit of operations is 2 right. So if you are batching, you are waiting both 1 & 2 to be completed to run 3. 3 can be run right away when either 1 or 2 is completed right ?

justlifethings
Автор

Hi Prashant nice video✓
When you saw this question first time did you able to solve it on your own ? Or Did you take any reference ?

vijaynavale
Автор

Bhaiya do you looking for video editor?

kotadiyachaitanya
Автор

Would following approach be acceptable? This works correctly.

class A{
constructor(numOfProcesses, cb, limit){
this.limit = limit;
this.waitingList = [];
this.size = 0;
this.numOfProcesses = numOfProcesses;
this.responses = [];
this.cb = cb;
}
subscribe(fn){
if(this.size===this.limit) this.waitingList.push(fn);
else{
this.size++;
this.execute(fn);
}
}
async execute(fn){
const res = await fn();
this.responses.push(res);

return
}else{
this.size--;
}
if(this.responses.length === this.numOfProcesses){
this.cb(this.responses);
}
}
};
async function getNameById(id){
const time = 3000
return new Promise(res => setTimeout(()=>{
return res(`User${id}`)
}, time))
}
function mapLimit(inputs, limit, iterateeFn, cb){
const obj = new A(inputs.length, cb, limit);
inputs.forEach((input)=>{
obj.subscribe(iterateeFn.bind(this, input))
})
}
mapLimit([1, 2, 3, 4, 5], 1, getNameById, (answer)=>{
console.log(answer)
})

aayushgupta