This is an interesting programming challenge that reddit user
i7_leaf claims to have received as an interview question.
There is one key difference between the original question and what I will cover in this article. The interview question asked the candidate to write a function that executes as follows:
It is worth noting that this curried function does not end in any sort of delimiter, e.g. a terminating method
.execute() or empty parameter
(). What makes this challenge both difficult and interesting is the lack of signal that “this is the last digit in the sequence.”
I agree with the majority of comments in the discussion thread that the interviewer did not mean to ask this question per se. As postulated, this function cannot exist. It is impossible for
addSubtract(1)(2)(3) to both be a primitive (the number
0 in the first example) and a function (that accepts 4 as a parameter in the second example).
That said, this is conceptually possible with a very slight tweak. While the following two statements cannot both be true, the third statement can.
The use of
+ in the third example attempts to type cast both sides of the argument to a
Number type. If there is no way to type cast to a
Number type, it will attempt to typecast to a
String type (the same way
'Hello ' + 123 === 'Hello 123'). It is because of our ability to explicitly define how to type cast an object that we are able to solve the problem of
addSubtract(1)(2)(3) is the primitive
0. It is, in fact, not the primitive number
0. It is a function, which is why we can both treat it as an object able to be type cast and call it as a function:
When treating an object (or function) as a number, the
valueOf method of that object will be called, the return value of which is what is used for the numerical operation. When treating an object as a string, the
toString method of that object will be called.
That’s really all the introduction you need to solve this problem, so I’ll provide the solution.
I defined the reducer function outside of the main function for readability. You may prefer including it in the function for better encapsulation.
The reducer merely alternates addition and subtraction. Given a running total and a new number, if it’s an even index, add; if it’s an odd index, subtract.
The recursive function
f is used to curry parameters. Every function call to
f just returns
f, allowing you to call it ad nauseum, each time adding the new parameter to the array of numbers that we will add/subtract.
f has a
valueOf property. When we type cast
f to a number, this property will get called. Starting with the first provided number (
valueOf property reducers the remaining numbers using the aforementioned alternating operations.
The first call to
addSubtract then returns the recursive function
f after having created it.
In typical interview fashion, this solution is not perfect. If an interviewer were to grill you about limitations, this has weird behaviors when caching the return value of any of the function calls. Each function call beyond the first call to
addSubtract will be using the same array of
nums. This may cause unintended behavior.
nums array is stored inside
addTo1. The first call adds 2 to the value. The second call subtracts 2 from the value. This can be resolved by returning a new instance of an array for each function call. The original interview question did not stipulate this as a requirement, so I opted to provide the solution with less code complexity.
If you have any questions or relevant insight, please leave a comment. It’s quick, it’s easy, and it’s free!
March 20, 2019