How to memoize/cache fun with varying arguments in JavaScript | Imp Question for top company Interw

preview_player
Показать описание


Memoizing a function is one of trickiest question that is generally asked in premium company interview. There are some questions which you will never be to answer, if you have not seen it before. This is one such kind. I have tried explaining it very much in detail in this video. Please watch the video till the end.

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

Thanks a lot Vasanth for sharing nice stuff. You're helping unseen ways. Looking forward to your next post and best of luck with it

Sangeeta
Автор

You bro doing very nobel work, respect ++

somyaranjandas
Автор

Great content. Please bring more interview question in depth like this. Thankyou

itumandal
Автор

Helpful. Please add more interview contents

AARP
Автор

Thanks vasant. Again great explaination 🔥

yashgupta-rsro
Автор

Thank you for making these videos on interview preparation.

deepakchandani
Автор

Well explained. Thank you, this really helped.

himabindubanoth
Автор

Hi Vasanth, This made me to think unique ways, Thanks a lot.
I have a question, what if we pass more number of arguments like add(10, 20, 30) this code wont work Right?
Can u please make a video such that it should be able to accept any number of arguments.

manishkagathi
Автор

Plz continue creating amazing content like this, and don't forget to share it in LinkedIn 😉🎉🎉
More power to you

ayushjain
Автор

Why is there need for adding function name while generating unique id? Doesn't the cache belong to each memoized function and hence only concatenation of arguments will be fine?

sawanpatodia
Автор

Hello sir,
I tried my own way to do this after watching this.

the entire code snippet is below.

// Write a function that will return cache o/p if present

// function add(a, b) - first time run 2nd time onwards from cache

function add(...args) {
sum = 0
for(let i of args){
sum = sum + i;
}
return sum;

}

function mul(...args) {
multiply = 0
for(let i of args){
multiply = multiply * i;
}
return multiply;

}

cache = {};
function memoize(fn, args){
key = fn.name + args.join("|");
if(cache[key]){
console.log("From cache");
return cache[key];
}
else{
console.log("Not from cache");
cache[key] = fn(...args);
return cache[key];
}
}

console.log(memoize(add, [121, 34, 4, 4]));
console.log(memoize(add, [1, 2, 35, 6]));
console.log(memoize(add, [1, 2, 35, 6]));
console.log(memoize(mul, [121, 34, 4, 4]));
console.log(memoize(mul, [1, 2, 35, 6]));
console.log(memoize(mul, [1, 2, 35, 6]));

// console.log(memoize(add, 10, 30));
// console.log(memoize(add, 10, 30));
// console.log(memoize(add, 10, 30));
// console.log(memoize(add, 10, 30));




Thank you for educating these types of best practices.

daveraj
Автор

Thanks for sharing, Although I was able to solve this question.
But instead of putting callback during initialization of memo function. I keep it at the calling time.
In this way, we no longer have to create another instance of the memo function for memoizing the function with its argument.

ayushbisht
Автор

Is it some extension that you are using for sticky function names...? Can you please share the name of extension?

adwaithks
Автор

What if order of arguments changes?

add(2, 3) === add(3, 2), but this will again calculate the results right?

gogulbharathisubbaraj
join shbcf.ru