Collect Arguments Until the Function Is Full
Updated
Problem statement
Write collectArgs(fn). It takes a function fn and returns a new function that gathers arguments over one or more calls and runs fn once it has enough.
Let need be fn.length: the number of parameters fn declares before the first one that has a default value or a rest marker (...). Each call to a gathering function adds its arguments to the ones gathered before it:
- If there are now at least
needarguments, callfnwith all of them, in order, and return whatfnreturns. Extra arguments are passed on too. - Otherwise, return a new function that keeps gathering.
Rules for this exercise:
- A gathering function remembers only the arguments gathered up to its own call. Calling it again starts from the same place, and one function never changes what another one has gathered.
- A call with no arguments adds nothing, but it still counts as a call. It returns a gathering function, or runs
fnwhenneedis 0. undefinedandnullcount as arguments like any other value.- The call that completes the gathering supplies
thisforfn. Earlier calls do not. - If
fnthrows, the error goes to the caller. The gathering functions still work afterwards.
How the tests are written
Each test is a short script: a list of steps that run in order. {"let": "x", "value": V} makes a value called x. A value written as {"$js": "expression"} is the result of that JavaScript expression, and the expression may use names made earlier. {"call": "f", "args": [...]} calls f. The name solution is the function you write. A name like "obj.method" calls the method with obj as this. An optional "this" sets this for the call, an optional "new": true calls it with new, and an optional "let" saves the result under a name.
The expected output has one entry for each call, in order: {"returns": value} or {"throws": "ErrorName"}. A function is written {"$function": true} and undefined is written {"$undefined": true}.
Function
collectArgs(steps: JsSteps) → JsStepResultsExamples
Example 1
steps = [{"let":"sum3","value":{"$js":"(a, b, c) => a + b + c"}},{"call":"solution","args":[{"$js":"sum3"}],"let":"c"},{"call":"c","args":[1],"let":"c1"},{"call":"c1","args":[2],"let":"c12"},{"call":"c12","args":[3]},{"call":"c","args":[1,2,3]},{"call":"c","args":[1,2],"let":"c2"},{"call":"c2","args":[3]}]return = [{"returns":{"$function":true}},{"returns":{"$function":true}},{"returns":{"$function":true}},{"returns":6},{"returns":6},{"returns":{"$function":true}},{"returns":6}]- The gathering function for
sum3(which declares 3 parameters) is a function:{"returns": {"$function": true}}. c(1)has 1 of 3 arguments, so it returns a function.c1(2)has 2 of 3, so it returns a function again.c12(3)reaches 3 arguments and returns 1 + 2 + 3 = 6.c(1, 2, 3)also returns 6.c(1, 2)has 2 of 3 and returns a function, andc2(3)returns 6.
Example 2
steps = [{"let":"join3","value":{"$js":"(a, b, c) => [a, b, c].join('-')"}},{"call":"solution","args":[{"$js":"join3"}],"let":"c"},{"call":"c","args":["a"],"let":"ca"},{"call":"ca","args":["b","c"]},{"call":"ca","args":["x","y"]},{"call":"ca","args":["z"],"let":"caz"},{"call":"caz","args":["!"]}]return = [{"returns":{"$function":true}},{"returns":{"$function":true}},{"returns":"a-b-c"},{"returns":"a-x-y"},{"returns":{"$function":true}},{"returns":"a-z-!"}]c('a')returns a functionca.ca('b', 'c')reaches 3 arguments:"a-b-c".ca('x', 'y')starts again from'a', not from'b'and'c':"a-x-y".ca('z')has 2 of 3 and returns a functioncaz.caz('!')gives"a-z-!".
Example 3
steps = [{"let":"pack","value":{"$js":"function (a, b) { return [a, b, arguments.length]; }"}},{"call":"solution","args":[{"$js":"pack"}],"let":"c"},{"call":"c","args":[1,2,3,4]},{"call":"c","args":[1],"let":"c1"},{"call":"c1","args":[2,3]}]return = [{"returns":{"$function":true}},{"returns":[1,2,4]},{"returns":{"$function":true}},{"returns":[1,2,3]}]packdeclares 2 parameters and returns[a, b, number of arguments received].c(1, 2, 3, 4)has 4 arguments, which is at least 2, sopackgets all 4:[1, 2, 4].c(1)returns a function, andc1(2, 3)brings the total to 3:[1, 2, 3].
Constraints
- fn declares 0 to 6 parameters before any default value or rest marker
- Each test has 4 to 8 steps, and every call has 0 to 5 arguments
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Flatten a Multilevel Doubly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Recursive Task Worker with MemoizationCitadel · ONSITE INTERVIEWCitadel interview questions
- Implement a Self-Balancing BST (AVL Tree)Microsoft · ONSITE INTERVIEWMicrosoft interview questions
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Read Across Sound TracksAirbnb · PHONE SCREENAirbnb interview questions
- Streaming Median of Trade PricesXantium · PHONE SCREEN
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW