MediumPro challengePythonJavaScriptTypeScript
Memoize
FunctionsPatternsObjects
Implement memoize(fn) returning a wrapper that caches the result of fn
per argument vector. Subsequent calls with the same arguments must skip
the underlying fn and serve the cached result instead.
Requirements
- Support arbitrary arity (zero or more arguments).
- Argument equality must be structural (
[1,2]and[1,2]hit the same
cache entry; [1,2] and [1,3] don't).
- Don't recompute on cache hit — the test suite verifies the underlying
function is invoked at most once per unique argument vector.
The provided solve(callArgs) drives your implementation through a sequence
of calls and reports how many times the underlying expensive function ran.
Sample tests
Test #1Same single argument repeated → 1 underlying call
Input: [[[1],[1],[1]]]
Output: {"calls":1,"results":[1,1,1]}
Test #2Distinct arguments → as many underlying calls
Input: [[[1],[2],[3]]]
Output: {"calls":3,"results":[1,2,3]}
Test #3Alternating repeats stay cached
Input: [[[5],[10],[5],[10],[5],[10]]]
Output: {"calls":2,"results":[5,10,5,10,5,10]}
Test #4Multi-arg — vector equality, not first-arg only
Input: [[[1,2],[1,2],[3,4]]]
Output: {"calls":2,"results":[3,3,7]}
Test #5No calls → no underlying invocation
Input: [[]]
Output: {"calls":0,"results":[]}