Simple solution for first problem const input = { A : (a,b,c) => a+b+c, B : (a,b,c) => a-b-c, C : (a,b,c) => a*b-c, D : {E : (a,b,c) => a*b*c }, } const compute =(input,a,b,c)=>{ const temp = {} for(let i in input){ const key = input[i] if(typeof key ==='object'){ temp[i] = compute(key,a,b,c) }else{ const val = key(a,b,c) temp[i] = val } } return temp } console.log(compute(input ,1,1,1))
@coderinprocess26784 ай бұрын
good use of recursion. try to use proper names of variable. i got confused here const key = input[i]. its not returning key, its returning value of input object. Also never iterate objects using `in` because for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array. You will get such warnings from ESLINT in prod level code.
@chandanjoshi57274 ай бұрын
@@coderinprocess2678 Thank you
@ur23524 ай бұрын
const compute = function (a, b, c) { const output = {}; for (const [key, value] of Object.entries(input)) { output[key] = typeof value === 'object' ? Object.values(value).at(0)(a, b, c) : value(a, b, c); } return output; }; this is how i solved the first problem
@Krishnasaini4 ай бұрын
i solved it using recursion. Time complexity should be O(n) as each and every property of object is visited once
@coderinprocess26784 ай бұрын
My feedback to the candidate who did good work on most part. - 1. since you are an experience folk, dont directly jump to code. ask clarifying questions. Breakdown the problem. 2. since you are an experience folk, take better examples , choose better naming conventions.
@mirijanyavo653220 күн бұрын
Kinda trivial question: const compute = (object, ...args) => Object.entries(object).reduce((r, [k, v]) => ((r[k] = typeof v === "function" ? v(...args) : compute(v, ...args)), r), {}); or: const compute = (object, ...args) => Object.fromEntries(Object.entries(object).map(([k, v]) => [k, typeof v === "function" ? v(...args) : compute(v, ...args)])); Array reduce similar to JS: const reduce = (...args) => arr => { const reducer = args[0]; if (typeof reducer !== "function") throw new TypeError(`${typeof reducer} is not a function`); if (!arr.length && args.length < 2) throw new TypeError("Reduce of empty array with no initial value"); let accumulated = args.length < 2 ? arr[0] : args[1]; const startIndex = args.length < 2 ? 1 : 0; for (let i = startIndex; i < arr.length; i++) { accumulated = reducer(accumulated, arr[i], i, arr); } return accumulated; }; const add = (a, b) => a + b; // multiple elements + no initial console.log([1, 2, 3, 4].reduce(add)); // 10 console.log(reduce(add)([1, 2, 3, 4])); // 10 // multiple elements + initial console.log([1, 2, 3, 4].reduce(add, 5)); // 15 console.log(reduce(add, 5)([1, 2, 3, 4])); // 15 // 1 element + initial console.log([1].reduce(add, 2)); // 3 console.log(reduce(add, 2)([1])); // 3 // 0 elements + initial console.log([].reduce(add, 0)); // 0 console.log(reduce(add, 0)([])); // 0 // 1 element + no initial console.log([1].reduce(add)); // 1 console.log(reduce(add)([1])); // 1 // no elements + `undefined` passed in as literal console.log([].reduce(add, undefined)); // undefined console.log(reduce(add, undefined)([])); // undefined // no elements + no initial // console.log([].reduce(add)); // TypeError "Reduce of empty array with no initial value" // console.log(reduce(add)([])); // TypeError // Reducer not function // console.log([].reduce({}, 5)); // TypeError "object is not a function" // console.log(reduce({}, 5)([])); // TypeError
@SonuKumar-lh3hg4 ай бұрын
Being a fresher/less exp. Sometime we feel happy when we were knowing basic core concept as compare to more exp. people.Very useful❤ bring such content regularly.
@4318A.M.vishal4 ай бұрын
This is my solution for the first problem, I've added an extra parameter to the compute function for using recursion based approach const input = { A: (a,b,c) => a+b+c, B: (a,b,c) => a-b-c, C: (a,b,c) => a+b*c, D: { E: (a,b,c) => a+b+c } } function compute(a, b, c, Input = null) { let obj = Input ? Input : input for(let key in obj) { if(typeof obj[key] === "function") { obj[key] = obj[key](a,b,c); }else { let temp = obj[key]; compute(a,b,c, temp); } } return obj; } console.log(compute(1,1,1));
@4318A.M.vishal4 ай бұрын
For the second question my answer is Array.prototype.myReduce = function (callback, initialVal) { const arr = this; for(let i=0;i item + acc, 0)); kindly tell me if there is any corrections to be made
@coderinprocess26784 ай бұрын
paused during Q1 and solved it using recursion. // clarifying questions to Chirag // 1. can you tell me more about types of the input object // 2. can input object takes more than 3 perameters for its function as value // 3. Should I validate the input, or can I assume it's always valid? Assumption - assuming the type of value of Input object is either function or object const outputConstructor = (input, ...args) => { if (typeof input !== "object" || Array.isArray(input)) { return input; } const output = {}; for (const [key, value] of Object.entries(input)) { output[key] = typeof value === "function" ? value(...args) : outputConstructor(value, ...args); } return output; }; const compute = (...args) => { return outputConstructor(input, ...args); }; compute(1, 1, 1);
@jaswanthponnusamy5970Ай бұрын
Since we are using the term dynamically the input can change and so args could also, function compute(...args) { function computeHandler(inputObj) { let output = {}; for(const key in inputObj) { let obj = inputObj[key]; if(typeof obj === "function") { output[key] = obj(...args); } else { output[key] = computeHandler(obj); } } return output; } return computeHandler(input); }
@ankitasrivastava69434 ай бұрын
My solution for first problem: :) const compute = (a,b,c) => { const input = { A: (a,b,c) => a + b + c, B: (a,b,c) => a + b - c, C: (a,b,c) => a + b * c, D: { E: (a,b,c) => a + b + c, } } const recCompute = (tempInput, a,b,c) => { let result = {}; for(let key of Object.keys(tempInput)) { if(tempInput[key] instanceof Function) { result = { ...result, [key]: tempInput[key](a,b,c) } } else { result = { ...result, [key]: recCompute(tempInput[key],a,b,c) } } } return result; } return recCompute(input,a,b,c) }
@gunjanvyas6954 ай бұрын
Thank you, for awesome content as always My Solution: //Question 1 : my recursive solution const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => (a / b) * c, D: { E: (a, b, c) => a + b + c, }, }; function getOutputExecuted(input, ...args) { let ans = {}; Object.keys(input).forEach((key, value) => { if (typeof input[key] === "function") { return (ans[key] = input[key](...args)); } else { ans[key] = getOutputExecuted(input[key], ...args); } }); return ans; } console.log("result -> ", getOutputExecuted(input, 1, 1, 1)); //Question2 Array.prototype.myReduce = myReduce; function myReduce(fn, initialValue) { let arr = this; let n = arr.length; let curr = initialValue; for (let i = 0; i < n; i++) { curr = fn(curr, arr[i]); } return curr; } let inbuiltSol = [1, 2, 3].reduce((acc, item) => { return (acc += item); }, 0); let myReducerSol = [1, 2, 3].myReduce((acc, item) => { return acc + item; }, 0); console.log(inbuiltSol); //6 console.log("myReduce polyfill Sol", myReducerSol); //6 //Reduce usage => 1) accumulation(same as above example) //Reduce usage => 2) transformation
@AtulKumar-gt7uq4 ай бұрын
[Input Not Modified Solution 1] | It was really a good problem. @chirag Please find my approach for the solution and do let me know If I missed anything here :) const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a * b * c, C: { D: (a, b, c) => a / b / c, E: { F: (a, b, c) => a - b - c } } } function compute(a, b, c) { if (typeof input !== 'object' || Array.isArray(input)) { throw new Error('Invalid input'); } function deepChecks(input) { let resObject = {} for (let key in input) { if (typeof input[key] === 'function') { resObject[key] = input[key](a, b, c); } else if (typeof input[key] === 'object') { resObject[key] = deepChecks(input[key]); } else { resObject[key] = input[key]; } } return resObject; } return deepChecks(input); } console.log(compute(1, 2, 3)); Thanks ❤
@shivareddy41693 ай бұрын
Thank you so much for the JavaScript content which you are providing. here the solution const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => a + b * c, D: { E: (a, b, c) => a + b + c } }; function compute(a, b, c) { const result = {}; for (const key in input) { if (typeof input[key] === 'function') { result[key] = input[key](a, b, c); } else if (typeof input[key] === 'object') { result[key] = {}; for (const innerKey in input[key]) { if (typeof input[key][innerKey] === 'function') { result[key][innerKey] = input[key][innerKey](a, b, c); } } } } return result; } console.log(compute(1, 1, 1));
@coderinprocess26784 ай бұрын
after quick reading of prototype and this, came up with this solution of polyfill of reduce Array.prototype.myReduce2 = function (cb, initialValue=0) { if (!Array.isArray(this) || typeof cb !== "function") { throw new Error("input is not an array"); } let acc = initialValue; // iterate through array for (let i = 0; i < this.length; i++) { acc = cb(acc, arr[i]); } return acc; }; console.log('polyfill2 via prototype', arr.myReduce2(sum, 1))
@mehulsatardekar89853 күн бұрын
my solution const obj = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => a * b * c, D: { E: (a, b, c) => a + b + c, }, }; const compute = (a, b, c) => { const process = (inputObj) => { const emptyObj = {}; for ([key, value] of Object.entries(inputObj)) { if (typeof value === "object") { // process() emptyObj[key] = process(value); //process(obj[key](a,b,c)) } else if (typeof value === "function") { emptyObj[key] = value(a, b, c); } } return emptyObj }; return process(obj); }; console.log(compute(1, 1, 1));
@nayansinghal234 ай бұрын
CODE - 1 :- function recursion(input, a, b, c) { const output = {}; for(const prop in input) { if(typeof(input[prop]) === 'object') { output[prop] = recursion(input[prop], a, b, c) } else { const func = input[prop]; const value = func(a, b, c); output[prop] = value; } } return output; } function compute(a, b, c) { const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => a + (b * c), D: { E: (a, b, c) => a + b + c, } } return recursion(input, a, b, c); } console.log(compute(1, 1, 1));
@nikhilkorane93644 ай бұрын
He has 7 years of experience and didn’t know how to get value from object 😮😅
@coderinprocess26784 ай бұрын
paused during polyfill of myReduce and here is my solution const myReduce = (arr, cb, initialValue = 0) => { if (!Array.isArray(arr || typeof cb !== "function")) { throw new Error("input is not an array"); } let result; let acc = initialValue; // iterate through array for (let i = 0; i < arr.length; i++) { acc = cb(acc, arr[i]); } return acc; };
@RounakSaga3 ай бұрын
function compute(a,b,c) { var output = {} function recur(input,output) { for(let k in input){ if(typeof(input[k]) == 'object') { output[k] ={} recur(input[k], output[k]) } else { output[k] = input[k](a,b,c) } } } recur(input,output) return output }
@AnkitTyagi-co8rs4 ай бұрын
const input = { A : (a,b,c) => a+b+c, B : (a,b,c) => a-b-c, C : (a,b,c) => a*b-c, D : {E : (a,b,c) => a*b*c }, } const iterator =(input,a,b,c)=>{ let output ={}; for(let i in input){ if(typeof input[i] ==='object'){ output[i] = iterator(input[i],a,b,c) } else{ output[i] = input[i](a,b,c) } } return output; } function compute(a,b,c){ let res = iterator(input,a,b,c) return res } const res=compute(1,1,1); console.log(res);
@DebayanMukherjee-wo2ul4 ай бұрын
I am a fresher.......I am able to done the second part of the object successfully.......
@suwalsabin92504 ай бұрын
my solution for qns 1 const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => a + b * c, D: { E: (a, b, c) => a + b + c, }, }; function compute(a, b, c) { let finaloutput = RecursiveFnCall(input); function RecursiveFnCall(value) { const output = {}; Object.keys(value).forEach((key) => { output[key] = callFN(value[key]); }); return output; } function callFN(propsFn) { if (typeof propsFn == 'function') { return propsFn(a, b, c); } else { return RecursiveFnCall(propsFn); } } return finaloutput; } console.log(compute(1, 1, 1));
@Krishnasaini4 ай бұрын
before jumping to code- clarifying questions to Interviewer- 1. can you tell me more about types of the input object 2. can input object takes more than 3 perameters for its function as value 3. Should I validate the input, or can I assume it's always valid?
@shubhanshusahuu4 ай бұрын
//My inplementation using recusrion for First problem const input = { A: (a,b,c)=> a+b+c, B: (a,b,c)=> a-b-c, C: (a,b,c)=> a+b*c, D: { E : (a,b,c) => a+b+c } } const getSolvedObj= (a,b,c,obj)=>{ let result ={} for(let key of Object.keys(obj)){ if(typeof obj[key] == 'object'){ result[key] = getSolvedObj(a,b,c,obj[key]) } else{ result[key] = obj[key](a,b,c) } } return result } console.log(getSolvedObj(1,2,3,input))
I hope this will work const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => a + b * c, D: { E: (a, b, c) => a * b * c, }, }; function compute(a, b, c) { const recurseCompute = (obj) => { const result = {}; for (const key in obj) { if (typeof obj[key] === 'function') { result[key] = obj[key](a, b, c); } else if (typeof obj[key] === 'object') { result[key] = recurseCompute(obj[key]); } } return result; }; return recurseCompute(input); } const output = compute(1, 1, 1); console.log(output);
what if we don't provide the initial value to the fun, i.e initial is undefined
@shubhanshusahuu2 ай бұрын
@@ur2352 we can use the default param for initial as null
@PrashantKumar-ui6rs4 ай бұрын
function compute(input, a, b, c){ const result = {}; for(let key in input){ if(typeof input[key] === 'function'){ result[key] = input[key](a,b,c) }else{ Object.assign(result,compute(input[key], a, b, c)) } } return result; }
@robinsaini84014 ай бұрын
First Problem solution by using graph dfs const calGraph = (input, ...a) => { if (typeof input === "function") return input(...a) let result = {}; for (const key in input) { result[key] = calGraph(input[key] , ...a); } return result; }; const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a * b * c, C: { E: (a, b, c) => a - b - c, D: { X: { Z: (a, b, c) => a + b - c, } } }, }; console.log(calGraph(input , 1,2,3));
@mrwhosaini4 ай бұрын
Solution to First Problem const myObj = { A : (a, b, c) => a + b + c , B : (a, b, c) => a - b - c, C : (a, b, c) => a - (b * c), D : { E: (a, b, c) => a + b + c, F: { G: (a, b, c) => Math.pow(a,b) + c } } } const result = new Object(); let compute = function(a, b, c, computed) { const props = Object.keys(this); props.forEach((prop) => { const propType = typeof this[prop]; if(propType === 'function') { //execute only if value is a function computed[prop] = this[prop](a, b, c); } else if(propType === 'object') { // do recursion if value is an object const subComputed = new Object(); computed[prop] = subComputed; compute.call(this[prop], a, b, c, subComputed); } }) } compute.call(myObj, 1, 1, 1, result);
@deepaksharma98744 ай бұрын
Could you take my interview sometime?It would mean a lot to me. Keep up the amazing work!
@anirudhhnalapur54264 ай бұрын
let input = { A: (a,b,c) => a+ b + c, B: (a,b,c) => a - b - c } function compute(a,b,c) { let keys = Object.keys(input); let outputObject = {} for(let i = 0; i
@ankushroy56064 ай бұрын
u make my saturday worthit
@engineerchirag4 ай бұрын
🙏❤️
@ajitchaubey32804 ай бұрын
Here is the solution for both the problem . Question 1- const input={ A:(a,b,c)=>a+b+c, B:(a,b,c)=>a-b-c, C:(a,b,c)=>a+b*c, D:{ E:(a,b,c)=>a+b+c } } function compute(...args){ function helperFunction(currObj){ return Object.keys(currObj).reduce((acc,curr)=>{ if(typeof currObj[curr]==="object"){ acc[curr]= helperFunction(currObj[curr]) }else{ acc[curr]=currObj[curr](...args) } return acc },{}) } return helperFunction(input) } console.log(compute(1,1,1)) Question 2 - //Custom my reduce function //1: callback function //2: initialValue Array.prototype.myReduce=function(cb,initialValue){ let acc; let startIndex=0; if(arguments.length
@omnikingomniking64124 ай бұрын
How can he have 6 years exp. He dont have his fundamentals clear. Clearly rejected
Set of good questions Chirag 👍. I feel Venkata has watched multiple tutorials but lacks hands on practice as he knows things but finds it difficult to implement. How do I get a chance to appear on an interview with you 😀.
@HelloIntizar4 ай бұрын
Thank you so much for the JavaScript content which you are providing. Here is the solution for the first problem const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => a + b - c, D: { E: (a, b, c) => a + b + c, }, }; function compupte(a, b, c) { const getObjectValue = (obj) => { if (typeof obj !== 'object') { return obj; } return Object.keys(obj).reduce((accumulator, key) => { const currentValue = typeof obj[key] === 'function' ? obj[key](a, b, c) : getObjectValue(obj[key]); return { ...accumulator, [key]: currentValue, }; }, {}); }; return getObjectValue(input); } console.log(compupte(1, 1, 1));
const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a-b-c, C: (a, b, c) => a + b*c, D: { F: (a, b, c) => a+ b+c } } function CB (a,b,c, payload) { let result = {} for (let data in payload) { if (typeof payload[data] == 'function') { result[data] = payload[data](a,b,c) } else if (typeof payload[data] == 'object') { result[data] = CB(a,b,c,payload[data]) } } return result; } function compute(a, b, c) { let result = {}; return CB(a,b,c, input) } console.log(compute(1,1,1));
@PavanKumar-os9xj4 ай бұрын
Good question lot of leaning in the implementation
@engineerchirag4 ай бұрын
Keep learning, keep sharing 🚀
@rishabhmehta62044 ай бұрын
const input = { A : (a,b,c) => a+b+c, B : (a,b,c) => a-b-c, C : (a,b,c) => a*b-c, D : {E : (a,b,c) => a*b*c }, } compute (1,1,1) function compute(input,...args){ const ans ={}; for (let key in input){ if(typeof input[key] ==="object"){ ans[key] = compute(input[key],...args) } else if(typeof input[key]=="function"){ ans[key] = input[key](...args) } else{ ans[key] = input[key] } } return ans; } console.log(compute(input,1,1,1)) my solution
@arkaprabhadas85902 ай бұрын
bhaiya please look into the audio quality, its a little hard to follow
@HunkIsHere4 ай бұрын
Solution of Problem 1: const input = { A: (a, b, c) => a + b + c, B: (a, b, c) => a - b - c, C: (a, b, c) => a * b * c, D: { E: (a, b, c) => a + b * c, }, }; function compute(...args) { const input = this; const result = {}; Object.keys(input).map((key) => { if (typeof input[key] === 'function') { result[key] = input[key](...args); } else if (typeof input[key] === 'object') { result[key] = compute.call(input[key], ...args); } else { result[key] = input[key]; } }); return result; } const result = compute.call(input, 1, 1, 1); console.log(result);
@AtulKumar-gt7uq4 ай бұрын
Is this valid?? as We were not suppose to modify the input or usage of the compute function. !!!
@lalipathak-g3c4 ай бұрын
second problem Array.prototype.myReduce = function(cb, init) { let acc = init for(let i = 0; i < this.length; i++){ acc = acc ? cb(acc, this[i], i, this) : this[i]; } return acc; }
@ketanthorat43374 ай бұрын
@Chirag Goel can you please suggest KZbin channels in English as well as Hindi to learn react j's
@வைரமுத்து.மா4 ай бұрын
First Question My Solution function compute(a,b,c, obj = input) { for(let va in obj) { if(typeof(obj[va]) === 'function') { obj[va] = obj[va](a,b,c); } else if(typeof(obj[va]) === 'object' && !Array.isArray(obj[va])) { obj[va] = compute(a,b,c, obj[va]); } } return obj; } const op = compute(1,1,1); Second Question my Solution Array.prototype.myReduce = function(fn, initVal) { for(let i =0; i < this.length; i++) { initVal = fn.apply(null, [initVal, this[i]]); } return initVal; }
@sumitmobiotics31614 ай бұрын
Its awsome.
@engineerchirag4 ай бұрын
❤️
@MacheteBoy4 ай бұрын
My opinion would be Reject
@Krishnasaini4 ай бұрын
which platform is this where they are solving questions?
@inspirelogic74 ай бұрын
Codility
@Fam-m4i4 ай бұрын
Bhai accumulated value return karde😅
@ryandsouza29624 ай бұрын
How can I apply for these?
@engineerchirag4 ай бұрын
Apply here: forms.gle/8VyGEUuRwEUrj4vT6
@rizwansaifi35904 ай бұрын
how can i give these mocks interviews ?
@engineerchirag4 ай бұрын
Apply here: forms.gle/8VyGEUuRwEUrj4vT6
@rizwansaifi35904 ай бұрын
@@engineerchirag done
@CodeNinja184 ай бұрын
Bring react instead of javascript it will be more helpful 😅
@engineerchirag4 ай бұрын
Sure, noted 👍
@trialaccount22444 ай бұрын
@@engineerchiragI think if candidate not able to give soln for Vanilla js then it will make no sense to elevate it into react soln 😉
@AnkitTyagi-co8rs4 ай бұрын
let arr =[1,2,3,4,5]; let sum = arr.reduce((acc,curr)=>{ acc +=curr; return acc },0) console.log(sum) Array.prototype.myReduce = function(cb,initialVal){ let arr= this; let acc=initialVal; for(let i=0; i{ acc+=curr; return acc; },0) console.log(output)