Implement discounted card purchases
Company: Brex
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates implementation-level skills in resource accounting and state management, specifically handling gem-cost mappings, per-color discounts, bounded arithmetic, and transactional updates to player state.
Constraints
- Gem colors are exactly: BLUE, WHITE, GREEN, RED, YELLOW.
- All gem quantities and costs are non-negative integers.
- A discounted cost for any color is floored at 0 (never negative).
- The discount applied to a gem color comes from the count of owned cards of that same color, not the card being purchased.
- A failed purchase leaves the player's state completely unchanged.
- Operations are replayed in order against one shared, evolving player.
Examples
Input: ({'RED': 5}, [['purchase', 'RED', {'RED': 2}], ['purchase', 'RED', {'RED': 2}], ['purchase', 'RED', {'RED': 2}], ['purchase', 'RED', {'RED': 2}], ['purchase', 'RED', {'RED': 2}]])
Expected Output: [True, True, True, True, True]
Explanation: Buying RED cards (cost 2 RED) accrues RED discounts. Purchase 1: cost 2, gems 5->3, discount[RED]=1. Purchase 2: cost max(2-1,0)=1, gems 3->2, discount=2. Purchase 3: cost max(2-2,0)=0, gems unchanged, discount=3. Purchases 4 and 5 also cost 0. All five succeed.
Input: ({'WHITE': 2, 'RED': 1}, [['purchase', 'WHITE', {'WHITE': 1}], ['purchase', 'WHITE', {'WHITE': 1}], ['canPurchase', 'RED', {'WHITE': 4, 'RED': 1}]])
Expected Output: [True, True, False]
Explanation: Buy two WHITE cards (each cost 1 WHITE): after them gems WHITE=1, discount[WHITE]=2. Now check a RED card costing 4 WHITE + 1 RED: discounted WHITE = max(4-2,0)=2, RED = max(1-0,0)=1. Player has only 1 WHITE gem (< 2), so it is not affordable -> False.
Hints
- Maintain three running structures across operations: the player's gem counts, a per-color discount counter, and (optionally) a hand size.
- Discount for gem color c is discount[c] (cards of color c already owned), independent of the card you are buying. Apply max(cost - discount, 0) per color.
- canPurchase succeeds only if, for every color, owned gems >= the discounted cost.
- On a successful purchase, deduct the discounted cost, then increment discount[card.color] by 1 so future cards of related colors get cheaper.