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
- Initial Purchase: $15 gives you 15 chocolates. (Wrappers = 15).
- First Exchange: Exchange 15 wrappers for 5 new chocolates. (Wrappers = 5).
- Second Exchange: Exchange 3 out of your 5 wrappers for 1 new chocolate.
- (Wrappers remaining: 2 from before + 1 brand new = 3 wrappers).
- Third Exchange: Exchange those 3 wrappers for 1 final chocolate. (Wrappers = 1).
2. The Total
Summing them up: 15+5+1+1={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.
Premium Content
Unlock Maximum Chocolate and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans