sum(1)(2)(3)(4)..( n)() | Amazon UI/Frontend Javascript Interview Question

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so suppose we have a function and we can pass in arguments like this and it goes on it can be any number and so what this should do is it should give me a sum of all these numbers so it should probably give me the output as 10. so whenever you see any such type of question in interview first try to think what this function is trying to do and how it is trying to execute things so people who are not from javascript background may find it little weird like how can we call this uh like this but let me give you a brief example like so in javascript functions are first class objects so what do i mean by or you can say call it as first class functions so function behave just like any other variable or an object so what i mean by that is suppose if we have a function so let's say so this is just a merely variable and we can assign a function to it so this this is just a brief info for all those who are not from javascript background so i can assign a variable with a function so that means i can even pass a function inside another function as an argument and i can even return a function from a function so that means functions are nothing but simple variables in javascript so that's the beauty of first class functions and so and i can simply return like this as well so functions are allowed to do this so i can return a function uh it is just behaving just like another variable that's it so let's come on to the question again so this was just a little background about how functions are in javascript and how they behave under the hood so we it's a valid syntax so whenever you face such type of problem and interview so just try to break your problem into smaller parts and think about what this function is trying to do so what i mean by that is let's let's just solve it for a smaller problem so let's just first think how this much of the code will work so suppose it was something like this one and two just two arguments and this should return me a function which can be called and the output should be three so how can we approach this problem is so just try to think it very simple so let's take it so sum is a function which takes an argument a so this a would here be one and b okay let's call it as b and this function sum will be a function which will again return me a function right so this part this part will return me a function which can again be executed by passing a variable or a passing argument inside it let's call it as b and what and that and this function so this function this return function when executed should give me sum of a and b so that means this function what it will do is it will give me it will return me the sum of a and b simple so it's so first try to understand this smaller problem so if you if you get it right so uh so sum is a function which takes a and and this returns me this function this function takes b and returns me the sum of a and b when executed so that's how it is so it will return as 3 so this is a smaller problem but what if we had more arguments over here so suppose if we had 3 then this should again return me a function which should take c and then return me a plus b plus c so we can see we can see a clear pattern in this so what we will try to now do is we will try to somehow solve it recursively because see you can you can see that this is a pattern so if there was d then i would have written again another function with an argument d so this is a clear pattern so we will try to somehow optimize our solution and call the sum function again recursively so that we can try to reduce our problem sub problems so if i had to solve it for all these things so what let's try to write some code so sum will again be a function which takes an argument a in this case a will be 1 and it should return me a function again which takes the argument b so now we can clearly see that this function should also return me a function which can take an argument and which can take an argument and again return me a function and again return me a function until we'll re until we reach to a point where there is no argument left so it is it is like calling the sum function recursively so it should return me it should return me some this function but with the arguments a plus b so why did we do this so we did this because we wanted this function to again return a function that means we are not doing anything we are just going down the hierarchy and we are just recursively calling this function so if we see the purpose of this function sum what it what it does is it takes a argument and returns me a function which can again take a argument so that's it and then we have to continue this cycle till we don't find an argument in this case so what this will do is let's try to dry run it so we called sum of 1 so sum of 1 will take 1 over here and return me a function which can take another argument so it took b and then what it will do is so after this thing after this thing we are calling we are returning sum of a plus b that means this sum of a and b will again return me a function which takes which takes an argument b and returns me again sum of a and b so in that case after this thing when this sum a and b or 1 plus 2 will return me a function which is again this function but this function will now be but this function here b will now refer to c 3 and then again return me a sum of a plus b so that means we are returning a function which takes an argument and returns me sum of a plus b so this cycle will go on go and go on until we have to get to some base point of this recursion so the base point of this recursion would be an empty argument so that means if we had b if we don't had b or if we had b as undefined then we should somehow stop so let's write the base case for a recursion so we'll let me first write it so if b exists that means if b exists then only then only return me sum of a plus b if b is not present then it what should it return it should directly return me a so what will this a be it will keep on adding this 1 plus 2 plus 3 plus 4 as in when we are doing a plus b and calling the sum recursively so what will happen is when we do a sum of 1 this will return me a function which can take now 2 and again return me a function which will now take 3 as b and again return me a function which can take 4 and it will go on so this is what a very simple code of this approach is if you just try to run this sum like this and try to console log it if you go and type all this code and try to console log sum of one two three four so it will so it will return us 10 for sure so that is how we do it and it looked very tricky but it is actually very less line of code and it is easily solved like this and there is no uh rocket science in this question so that was all it nothing except this so so this was the solution so if we try to use the es6 syntax we can we can write something like this so people in my videos do comment that i should tell them to write in es6 format so so you can somehow write like this so instead of functions you can just directly write like this over here we can use a ternary operator instead of this if an if else case so something like this we can directly write return of return of if b exists then we can return sum of a plus b else we can directly return a so this is how it looks in es6 syntax we can modify it more let me let me just shorten it down for you more so so it could it could just fit in one line as well so sum is equals to it takes an argument a and then returns me a function b which again returns me if b exists it returns me some a plus b else b sorry a so you can implement this whole thing by just simple single line of statement and this will result to 10 over here you can pass in as many more arguments to it but it will return us so this was all the code for this whole complex looking problem so it is it is very easy to implement it is just that you should know a little bit of recursion that's it so just try to so whenever you get such type of problem just try to infer what this question is trying to tell you and what it is expecting this function was just expecting it to return a function again and keep on returning a function until i get to some conclusion which can be an empty uh empty braces that means we are not passing anything into b that's it so let's write the single line of code in the browser and also check how it looks uh and does it give us the same output or not so to implement that what we tried to do on the board was so a function a and then so this returns me a function again which takes b and then if b exists then return me sum of a plus b else just return me a so that's all it was and it was pretty easy and then let's try to console log and check it how it looks like how the output looks like so if i put a console log and try to run on the browser see it gives us 10 so if we to e card numbers let's say i have the first argument as 10 then it gives me 19 which is 10 plus 2 plus 3 plus 4 so so if i try to remove this fl statement over there and try to write ternary operator so i can do something like return if b exists then return me sum of a plus b else just return me a so that's how that's how we can do and if we try to use the es6 syntax of arrow functions we can write something like this so and if there is one argument single argument then we need not write these braces as well so it looks something like this so we can still reduce this by removing these return statements so if there is just single line then i can directly uh remove this return statement and write something like this so and we can remove this return statement as well so so now our code looks something like this so this is just a single line of code now it's running on the browser perfectly and let's tweak some other parameters and check it so it gives us 37. so this single line of code is able to achieve the solution which we are trying to do over here this problem just looks complex but it is actually very simple to execute it's just like you need to understand how things are working and how we can call this sum function recursively and try to also check the base case so that's all for this demo so thank you so that was all about this javascript interview question so if you have liked the video then do give it a thumbs up and say subscribe to my channel because i am coming up with lot more new javascript interview questions and i am trying to help you by solving all these on whiteboard so that you can directly uh solve just like i am solving in the interviews so its quite helpful and if you have any suggestions in mind or if you want me to cover any specific interview question then do comment down in this video i do check the comments and i will if that question sounds good then i would definitely come up with a solution just like this and thanks to abhinav who suggested this question to solve and it will really help everyone out there so stay subscribed to my channel and press the bell icon so that you get all the notifications of my videos and you don't miss any any of the javascript interview question these questions are really very important so that's all in this video thanks for watching
Info
Channel: Akshay Saini
Views: 69,161
Rating: 4.9163651 out of 5
Keywords: akshaysaini.in, akshay saini javascript, akshay saini web engineer, akshay saini uber, akshay saini frontend, javascript tutorials, javascript interview questions, frontend interview questions, frontend tutorials, javascript fundamentals, akshay saini js, amazon frontend interview question, amazon web engineer interview question, sum(1)(2)(3), sum(a)(b)(c), multiply(1)(2)(3), fn(1)(2)(3), fn()()()()() javascript, first class functions javascript
Id: D5ENjfSkHY4
Channel Id: undefined
Length: 16min 46sec (1006 seconds)
Published: Sat Feb 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.