I solved the Google L5 javascript Interview question

preview_player
Показать описание
JavaScript Interview Question - 14 | In this video, we will see how to solve a JavaScript problem that was asked in Google's interview for L4/L5 level.

We have to implement a function that retries the failing promises for a given count of time

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 for SDE1, SDE2, and SDE3 roles.

This is the first preview question from my ebook "JavaScript Interview Guide". If you are preparing for Interviews and looking for a solutions book, Get my Ebook that has 120+ solved JavaScript questions.

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

Hi Prashant, solved it on my own. It is pretty same as the useFetch hook of react-query.

Below is my code -

const retryFetch = async (asyncFn, retries = 3, delay = 50, finalError = 'Request Finally Failed') => {
let pendingRetries = retries

// Make first Request
try{
let res = await asyncFn();
return res;
}catch(error){
console.log("Function Error", error)
}

// Start Retries
while(pendingRetries > 0){
try{
let res = await new Promise((resolve, reject) => {
setTimeout(async() => {
try{
let res = await asyncFn();
resolve(res);
}catch(error){
reject(error)
}
}, delay)
})
return res;
}catch(error){
pendingRetries -= 1
console.log("Function Error : ", error)
}
}

throw Error(finalError);
}

let maxTest = 3
let test = 0


const promise = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(test < maxTest){
reject('foo')
test += 1
}else{
resolve('Success')
}
}, 3000);
});
}

try{
const res = await retryFetch(promise, 3, 2000)
console.log(res)
}catch(error){
console.log(error)
}

akash-kumar
Автор

sir, companies like Google, microsoft, Amazon doesn't they ask DSA? because every other youtuber has such videos only like do DSA, leetcode, DP, Graph.
This questions which you are solving in your videos are they for any specific UI position?
if yes than do they hire freshers or experienced folks only? also please explain how can we prepare for such roles and do we have to prepare for DSA as well if we are targeting such specific roles?
Because most of the youtubers are telling the DSA path only to get into such MNC's.

shreyansh
Автор

Sir why you returned the promise in test promise function in a function please explain I understand the full code but as a beginner I am having trouble in understanding this

theawakened
Автор

Hey, I'm folllowing your JS study material and came across this problem :
Execute promises with the Priority, is this solution okay
const promises = [
{ status: 'resolve', priority: 4 },
{ status: 'reject', priority: 1 },
{ status: 'resolve', priority: 2 },
{ status: 'reject', priority: 3 }
];
function promiseWithPriprity(promises) {
promises = promises.sort((a, b) => a.priority - b.priority)
return new Promise((resolve, reject) => {
promises.forEach(element => {
if (element.status === 'resolve')
resolve(element)
});
reject('All promises rejected')
})
}

promiseWithPriprity(promises)
.then(res => console.log(res))
.catch(err => console.log(err))

ukbqhxne