Implement discounted card purchases
Problem Description
Implement the core purchase logic for a gem-based card game. Players collect gems to buy cards, and these cards provide permanent discounts on future purchases of specific gem colors.
Entities
- Gem Colors: There are five distinct gem colors:
BLUE,WHITE,GREEN,RED, andYELLOW. - Card:
color: Represents the gem color for which this card provides a permanent discount once purchased.cost: A map where keys are gem colors and values are the quantities of those gems required to purchase the card.
- Player:
gems: A map representing the player's current inventory of gems, mapping gem colors to their respective quantities.hand: A collection (e.g., list or set) ofCardobjects the player has successfully purchased.discountMap: A map tracking the total discounts a player receives for each gem color.discountMap[c]indicates the number of previously purchased cards of colorc, which directly translates to the discount applied for that gem color.
Rules for Purchasing
- Discount Application: When a player attempts to buy a card, a discount is applied to each gem color required for the card's cost. The discount for a specific gem color is equal to the number of cards of that same color the player already owns (as tracked in
discountMap).- Example: If a player owns 2 white cards and 1 red card, and attempts to buy a card costing 4 white gems and 1 red gem, the discounted cost would be 2 white gems (4 - 2) and 0 red gems (1 - 1).
- Minimum Discounted Cost: The discounted cost for any gem color cannot fall below zero. If the discount exceeds the required quantity for a gem, the cost for that gem becomes 0.
Required Functions
You must implement the following two functions:
canPurchase(card):- This function should return
trueif the player can afford the givencardafter applying all relevant discounts, andfalseotherwise. It should not modify the player's state.
- This function should return
purchase(card):- If the
cardis not affordable (as determined bycanPurchase), this function should returnfalseand ensure no changes are made to the player's state. - If the
cardis affordable, this function must:- Deduct the calculated discounted gem cost from the player's
gemsinventory. - Add the
cardto the player'shand. - Increment the discount count for the purchased card's
colorin the player'sdiscountMapby 1. - Return
true.
- Deduct the calculated discounted gem cost from the player's
- If the
AI interview prep
Core dilemma
Maintaining consistent and atomic state changes within a single player's game state while applying complex, state-dependent calculations (discounts).
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement discounted card purchases
Problem Description
Implement the core purchase logic for a gem-based card game. Players collect gems to buy cards, and these cards provide permanent discounts on future purchases of specific gem colors.
Entities
- Gem Colors: There are five distinct gem colors:
BLUE,WHITE,GREEN,RED, andYELLOW. - Card:
color: Represents the gem color for which this card provides a permanent discount once purchased.cost: A map where keys are gem colors and values are the quantities of those gems required to purchase the card.
- Player:
gems: A map representing the player's current inventory of gems, mapping gem colors to their respective quantities.hand: A collection (e.g., list or set) ofCardobjects the player has successfully purchased.discountMap: A map tracking the total discounts a player receives for each gem color.discountMap[c]indicates the number of previously purchased cards of colorc, which directly translates to the discount applied for that gem color.
Rules for Purchasing
- Discount Application: When a player attempts to buy a card, a discount is applied to each gem color required for the card's cost. The discount for a specific gem color is equal to the number of cards of that same color the player already owns (as tracked in
discountMap).- Example: If a player owns 2 white cards and 1 red card, and attempts to buy a card costing 4 white gems and 1 red gem, the discounted cost would be 2 white gems (4 - 2) and 0 red gems (1 - 1).
- Minimum Discounted Cost: The discounted cost for any gem color cannot fall below zero. If the discount exceeds the required quantity for a gem, the cost for that gem becomes 0.
Required Functions
You must implement the following two functions:
canPurchase(card):- This function should return
trueif the player can afford the givencardafter applying all relevant discounts, andfalseotherwise. It should not modify the player's state.
- This function should return
purchase(card):- If the
cardis not affordable (as determined bycanPurchase), this function should returnfalseand ensure no changes are made to the player's state. - If the
cardis affordable, this function must:- Deduct the calculated discounted gem cost from the player's
gemsinventory. - Add the
cardto the player'shand. - Increment the discount count for the purchased card's
colorin the player'sdiscountMapby 1. - Return
true.
- Deduct the calculated discounted gem cost from the player's
- If the