# Why does this memoized recursive coin-change function return stale results across calls?

**URL:** <https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996>\
**Category:** web dev\
**Created:** [April 4, 2026, 4:00pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996 "2026-04-04T16:00:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 4, 2026, 4:00pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996/1 "2026-04-04T16:00:10Z")

</div>

I wrote a recursive coin-change helper with memoization. It works on the first call, but later calls with different coin sets sometimes reuse old results. I expected each top-level call to start fresh. What exactly is causing the stale cache here, and what’s the cleanest fix without removing memoization?

```js
function minCoins(amount, coins, memo = {}) {
  if (amount === 0) return 0;
  if (amount < 0) return Infinity;
  if (memo[amount] !== undefined) return memo[amount];
  let best = Infinity;
  for (const c of coins) best = Math.min(best, 1 + minCoins(amount - c, coins, memo));
  return memo[amount] = best;
}
console.log(minCoins(6, [1,3,4]));
console.log(minCoins(6, [4,5]));

```

WaffleFries

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 4, 2026, 4:14pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996/2 "2026-04-04T16:14:07Z")

</div>

Your cache key is only `amount`, so a value computed for `6` with `[1,3,4]` is indistinguishable from `6` with `[4,5]`.

MechaPrime 😄

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 4, 2026, 8:50pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996/3 "2026-04-04T20:50:17Z")

</div>

@MechaPrime is right about `amount

```js
being too small as the key, and one caveat is this bug only happens if the same

```

memo`object survives across top-level calls; make a new memo per call or key by both`amount` and the coin set.

BobaMilk

---

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [April 4, 2026, 10:35pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996/4 "2026-04-04T22:35:10Z")

</div>

@BobaMilk’s bit about the same memo surviving across top-level calls is the practical gotcha, because `memo[6] = 2` from `[1,3,4]

```js
gets happily reused for

```

[4,5]`like the function has the memory of a goldfish with a grudge. Clean fix is to keep the memo private to one public call, or if you truly need a shared cache, key it with both`amount`and a stable coin signature like`6|4,5`.

Arthur
