@pradeepj2580

memoizeOne function will not work here if we call memoizeOne with different callbacks

Eg: if we call memoizeOne with add , and also we call memoizeOne with sub results will be inconsistent

because cache map is global one it will be shared by both add and sub callback memoizeOne calls

So we need create cache map for each memoizeOne call and return arrow function from memoizeOne

@johnhammond3605

She is very confident and relaxed..... that's the hardest thing to do in an interview

@yuvarajgeethavel7153

If you put the questions in the description section of the video, it will super helpful to practice along, Please consider it from next videos !! Btw great choice of questions!!

@nikhilm103

The problem is that similar questions are being asked since about 3-4 years now. I understand that they do a good job on checking the conceptual knowledge of the candidate and reinventing the wheel might not be possible every time but majority of the candidates I’ve seen just learn the solutions by heart and sometimes can’t even deal with a follow up question. People should understand that this is not an exam where you would just empty your pockets when you see a question you’ve already solved or practiced and call it easy. Ofc the interviewer will be more experienced and can read between the lines. We can always try to focus on the concepts as opposed to just going through interview questions.

@vishalpanchal2343

Overall that's a good interview. 
Optimized solutions  are-

Q 2.  We can use closures here as mentioned by him will looks like - 

function memorizeOne(fn){
    const cache = {};
    return function (...args){
        const key = JSON.stringify(args);        // Creating unique keys because objects are reference type
        if (key in cache) {
            console.log("Using memoized result");
            return cache[key];
        } else {
            console.log("Calculating result");
            const result = fn(...args);
            cache[key] = result;
            return result;
        }
    }
}

const add = (a, b) => a + b;

const memorize = memorizeOne(add);

console.log(memorize(1, 2)); // Calculates result: 3
console.log(memorize(1, 2)); // Uses memoized result: 3
console.log(memorize(2, 3)); // Calculates result: 5
console.log(memorize(1, 2)); // Uses memoized result: 3


Q 3.  Her solution was also good but here I used reduce method 

const obj = [
    { key: 'Sample 1', data: 'Data1' },
    { key: 'Sample 1', data: 'Data1' },
    { key: 'Sample 2', data: 'Data2' },
    { key: 'Sample 1', data: 'Data1' },
    { key: 'Sample 3', data: 'Data1' },
    { key: 'Sample 4' }
];

function groupBy(arr) {
    return arr.reduce((value, item) => {
        const { key, data } = item;
        if (!value[key]) {
            value[key] = [];
        }
        value[key].push({ key, data });
        return value;
    }, {});
}

const output = groupBy(obj);
console.log(output);



Your explanation of questions are great Krupa. Will wait for next part of the series.

@manikantaprasadlopinti8375

18:35 we need to create a closure (that holds the cache  nothing but arguments and result) and return it ... then it will work independent of function that we are passing. until the functions are pure

@rizgamingvids

I am watching it. callback are not async, while they could be but is some cases.
Q1:
here, except setTimeout() each line of code is sync so it setTimeout will go to event loop, rest will executed immediately then in the last seTimeout will be printed regardless the time (ms) given or not.

@sameerfaridi2

This video has a lot of great information, but as a beginner, I didn't fully understand some parts like the callback in the initial question, memoization, and other detailed concepts. I plan to watch it again after more practice, and I'm sure I'll understand everything better then. Thank you, Chirag Goel Sir.

@chopper-kun-2025

Hello ! I just wanna say thank you very much for this mock interview. I really learned a lot here and this is helpful to anyone with varying levels of expertise. Hoping you have more content of this kind in the future!

@rahulkrishdev

I am very happy that for these problem statement I paused the video I tried by myself I can able to solve that and solved those without any hint. I am confident now that i will crack my interview in the future 😊

@nayansinghal23

CODE FOR QUESTION 3 :- 

const add = (a, b) => a + b

const memoizeOne = (add) => {
    const map = new Map();
    return (a, b) => {
        const obj = {
            args: [a, b]
        }
        if(map.has(JSON.stringify(obj))) {
            console.log('Add function is not executed: previous result is returned -> ', map.get(JSON.stringify(obj)));
        }
        else {
            const output = add(a, b);
            map.set(JSON.stringify(obj), output);
            console.log('Add function is called to get new value -> ', output);
        }
    }
}

const memoizeAdd = memoizeOne(add);

memoizeAdd(1, 2);
memoizeAdd(1, 2);
memoizeAdd(2, 3);
memoizeAdd(2, 3);

@venkiketavath4400

const result = numbers.flat(Infinity); // flatten all the nested array values into a single array of value

@nayansinghal23

CODE FOR QUESTION 2 :-

const obj = [
    { key: 'Sample 1', data: 'Data1' },
    { key: 'Sample 1', data: 'Data1' },
    { key: 'Sample 2', data: 'Data2' },
    { key: 'Sample 1', data: 'Data1' },
    { key: 'Sample 3', data: 'Data1' },
    { key: 'Sample 4', data: 'Data1' },
]

const normalize = (obj) => {
    let output = {}
    obj.forEach(({ key, data }) => {
        if(!output.hasOwnProperty(key)) {
            output[key] = [];
        }
        output[key] = [ ...output[key], { key, data } ]
    })
    return output;
}

console.log(normalize(obj))

@syncmaster320

Her explanation is great although the implementations are okay at best. First problem could be solved used Object.groupBy (I guess he was expecting that), the memorization problem took way too long and then the recursion problem doesn't need an array initialized out side the function. The overall interview also seemed easy for anyone with 2.5-3+ yoe.

@sanyamjain7058

I think this is fine from my side! Anyone can improve this?
function memoised(fn){
    const cache=new Map();
    return (...args)=>{
        const key=args.join('-');
        if(cache.has(key)) return cache.get(key);
        const ans=fn(...args);
        cache.set(key,ans);
        return ans;
    }
}

@UttamKumar-gi7mc

Thank you sir for this a great series. One of the most awaited series.

@vikasvarma9462

for the common question section flatten array i have a solution 

function flattenArray(arr){
    return arr.toString().split(",").map(Number)
}

@manjulamanjula3162

Thank You @Chirag, this is very helpful. Thank you

@mirage4731

function memoize(fn){
    const argsLength = fn.length
    const cache = new Map();
    return function(...args){
        const key = args.join("")
        if(cache.get(key)){
            return cache.get(key)
        }
        const result = fn(...args)  
        cache.set(key, result);
        return result;
    }
}


memorize function I made, I initially made it with object but then moved to map

@nayansinghal23

CODE FOR QUESTION 4 :-

const a = [1, 2, 3, [4, [5, 6]], 7, 8];

const func = (arr, output) => {
    arr.forEach((item) => {
        if(typeof(item) === 'number') {
            output.push(item);
        }
        else {
            func(item, output);
        }
    })
}

const flattenArray = (arr) => {
    const output = [];
    func(arr, output);
    return output;
}

console.log(flattenArray(a));