Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Maximum Chocolate
INTERVIEWPUZZLE

Maximum Chocolate

Learn how to maximize the number of chocolates obtained by reasoning about exchanges, wrappers, and available resources.

The Puzzle: You have $15.

  • Each chocolate costs $1.
  • You can exchange 3 wrappers for 1 new chocolate. What is the maximum number of chocolates you can eat?

1. The Breakdown

  1. Initial Purchase: $15 gives you 15 chocolates. (Wrappers = 15).
  2. First Exchange: Exchange 15 wrappers for 5 new chocolates. (Wrappers = 5).
  3. Second Exchange: Exchange 3 out of your 5 wrappers for 1 new chocolate.
    • (Wrappers remaining: 2 from before + 1 brand new = 3 wrappers).
  4. Third Exchange: Exchange those 3 wrappers for 1 final chocolate. (Wrappers = 1).

2. The Total

Summing them up: 15+5+1+1={22}15 + 5 + 1 + 1 = \mathbf\{22\} chocolates.


Interview-Focused Questions

Q: Can you write a recursive function for this?

A: Yes.

function countMax(money, price, wrap) {
  let count = Math.floor(money / price);
  return count + countMore(count, wrap);
}
function countMore(wrappers, wrap) {
  if (wrappers < wrap) return 0;
  let newChoc = Math.floor(wrappers / wrap);
  let extra = wrappers % wrap;
  return newChoc + countMore(newChoc + extra, wrap);
}

Q: What if you had 2 wrappers and needed 3?

A: This is a cheeky variation! In some versions, you can “borrow” a chocolate or wrapper from the shopkeeper, eat it, and then pay them back with the resulting wrapper. If that’s allowed, you can sometimes squeeze out one more.

Key Takeaway

This is a test of Iterative Thinking. Most people miss the fact that the “new” chocolates also come with wrappers that can be reused.

My Private Notes

Notes are auto-saved locally to this device.