Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Memoized Squares

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 144: Closures

Implement `memoSquares(nums)` that squares each number, caching results in a closure, and returns the array of squares.

Sample tests

Input: memoSquares([2,3,2])
Output: [4,9,4]
Input: memoSquares([])
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Putting the cache inside the per-call function defeats memoization.

Learning resources

  • MDN: Closures
Approach & explanation (try first)

The cache lives in the closure, so repeated inputs reuse stored squares across calls.

Loading...
⌘/Ctrl + Enter

Run your code to see results.