Count the chocolates you can eat when empty wrappers buy free ones

Quick Overview

Given an amount of money, the price of one chocolate and the number of empty wrappers that buy a free chocolate, count how many chocolates you can eat in total. It tests careful simulation of a repeated exchange, including the wrappers that free chocolates add back, and reasoning about the count well enough to reach a constant-time answer.

Count the chocolates you can eat when empty wrappers buy free ones

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You have `money` units of money, one chocolate costs `price`, and the shop gives you one free chocolate for every `wrappers` empty wrappers you hand in. Every chocolate you eat, bought or free, leaves you one wrapper, and you may keep exchanging as long as you hold at least `wrappers` of them. Return the total number of chocolates you can eat. ### Function Signature ```python def total_chocolates(money: int, price: int, wrappers: int) -> int: ``` ### Rules - You buy with money only, one whole chocolate at a time. Money left over after buying `money // price` chocolates goes unused. - Each exchange hands in exactly `wrappers` wrappers for one chocolate, and the wrapper of that chocolate can be used in later exchanges. - You cannot borrow chocolates or wrappers. - If `money < price`, you buy nothing and the answer is 0. ### Constraints - `1 <= money <= 10^6` - `1 <= price <= 10^6` - `2 <= wrappers <= 10^6` ### Examples ```text Input: money = 10, price = 1, wrappers = 2 Output: 19 ``` You buy 10 chocolates and hold 10 wrappers. The exchanges go 10 wrappers for 5 chocolates (5 held), 4 for 2 (3 held), 2 for 1 (2 held) and 2 for 1 (1 held), so the total is 10 + 5 + 2 + 1 + 1 = 19. ```text Input: money = 15, price = 1, wrappers = 3 Output: 22 ``` You buy 15 chocolates. The exchanges go 15 wrappers for 5 (5 held), 3 for 1 (3 held) and 3 for 1 (1 held), so the total is 15 + 5 + 1 + 1 = 22. ```text Input: money = 20, price = 3, wrappers = 5 Output: 7 ``` You buy 6 chocolates and 2 units of money go unused. Five of the 6 wrappers buy one more chocolate, which leaves 2 wrappers, too few for another exchange.

Overview: Given an amount of money, the price of one chocolate and the number of empty wrappers that buy a free chocolate, count how many chocolates you can eat in total. It tests careful simulation of a repeated exchange, including the wrappers that free chocolates add back, and reasoning about the count well enough to reach a constant-time answer.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You have money units of money, one chocolate costs price, and the shop gives you one free chocolate for every wrappers empty wrappers you hand in. Every chocolate you eat, bought or free, leaves you one wrapper, and you may keep exchanging as long as you hold at least wrappers of them.

Return the total number of chocolates you can eat.

Function Signature

def total_chocolates(money: int, price: int, wrappers: int) -> int:

Rules

  • You buy with money only, one whole chocolate at a time. Money left over after buying money // price chocolates goes unused.
  • Each exchange hands in exactly wrappers wrappers for one chocolate, and the wrapper of that chocolate can be used in later exchanges.
  • You cannot borrow chocolates or wrappers.
  • If money < price , you buy nothing and the answer is 0.

Constraints

  • 1 <= money <= 10^6
  • 1 <= price <= 10^6
  • 2 <= wrappers <= 10^6

Examples

Input: money = 10, price = 1, wrappers = 2
Output: 19

You buy 10 chocolates and hold 10 wrappers. The exchanges go 10 wrappers for 5 chocolates (5 held), 4 for 2 (3 held), 2 for 1 (2 held) and 2 for 1 (1 held), so the total is 10 + 5 + 2 + 1 + 1 = 19.

Input: money = 15, price = 1, wrappers = 3
Output: 22

You buy 15 chocolates. The exchanges go 15 wrappers for 5 (5 held), 3 for 1 (3 held) and 3 for 1 (1 held), so the total is 15 + 5 + 1 + 1 = 22.

Input: money = 20, price = 3, wrappers = 5
Output: 7

You buy 6 chocolates and 2 units of money go unused. Five of the 6 wrappers buy one more chocolate, which leaves 2 wrappers, too few for another exchange.