PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • easy
  • Brex
  • Coding & Algorithms
  • Software Engineer

Implement discounted card purchases

Company: Brex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Implement purchase logic for a gem-based card game. There are five gem colors: `BLUE`, `WHITE`, `GREEN`, `RED`, and `YELLOW`. Each `Card` has: - a `color` representing the permanent discount it grants after purchase - a `cost` map from gem color to required quantity Each `Player` has: - a `gems` map from gem color to current quantity owned - a `hand` containing purchased cards - a `discountMap` where `discountMap[c]` equals the number of previously purchased cards of color `c` Rules: 1. When buying a card, the player gets a discount on each gem color equal to the number of owned cards of that same color. - Example: if the player owns 2 white cards and 1 red card, then a card costing 4 white and 1 red has discounted cost 2 white and 0 red. 2. A discounted cost for any color cannot go below 0. 3. `canPurchase(card)` should return `true` if the player can afford the card after applying discounts, otherwise `false`. 4. `purchase(card)` should: - return `false` and change nothing if the card is not affordable - otherwise deduct the discounted gem cost from the player's gems - add the card to the player's hand - increase the discount count for the purchased card's `color` by 1 - return `true` Write the logic for `canPurchase` and `purchase` correctly.

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.

Implement the purchase logic for a gem-based card game (think Splendor). There are five gem colors: `BLUE`, `WHITE`, `GREEN`, `RED`, and `YELLOW`. A **Card** has a `color` (the permanent discount it grants once purchased) and a `cost` (a map from gem color to the quantity required). A **Player** starts with a map of gems they own, an empty hand, and a discount counter per color (`discount[c]` = number of cards of color `c` already purchased). Rules: 1. When buying a card, the player gets a discount on **each** gem color equal to the number of owned cards of that **same** color. Example: if the player owns 2 white cards and 1 red card, a card costing 4 white and 1 red has a discounted cost of 2 white and 0 red. 2. A discounted cost for any color can never go below 0. 3. `canPurchase` returns `true` if the player can afford the card after applying discounts, else `false`. 4. `purchase`: if the card is not affordable, change nothing and return `false`; otherwise deduct the discounted gem cost, add the card to the hand, increment the discount count for the card's own `color` by 1, and return `true`. You are given the player's `initial_gems` and a list of `operations`. Each operation is `[action, card_color, cost_map]` where `action` is `"canPurchase"` or `"purchase"`. Replay every operation in order against a single evolving player and return the list of boolean results (one per operation).

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.

Input: ({'BLUE': 1, 'GREEN': 2, 'RED': 2}, [['canPurchase', 'BLUE', {'BLUE': 1, 'GREEN': 2, 'RED': 3}], ['purchase', 'BLUE', {'BLUE': 1, 'GREEN': 2, 'RED': 3}]])

Expected Output: [False, False]

Explanation: No discounts yet. The card needs 3 RED but the player has only 2, so canPurchase is False, and purchase returns False without changing any state.

Input: ({'BLUE': 3, 'GREEN': 4, 'RED': 4}, [['canPurchase', 'BLUE', {'BLUE': 2, 'GREEN': 2, 'RED': 2}], ['purchase', 'BLUE', {'BLUE': 2, 'GREEN': 2, 'RED': 2}]])

Expected Output: [True, True]

Explanation: Player has 3 BLUE, 4 GREEN, 4 RED; card costs 2/2/2 with no discounts, so it is affordable. The purchase succeeds and deducts the cost.

Input: ({}, [['canPurchase', 'BLUE', {}], ['purchase', 'BLUE', {}], ['canPurchase', 'RED', {'RED': 1}]])

Expected Output: [True, True, False]

Explanation: Edge case: a zero-cost BLUE card is always affordable even with no gems, so canPurchase and purchase both succeed (granting a BLUE discount). A RED card costing 1 RED is then unaffordable since the player owns no gems.

Input: ({'WHITE': 5}, [['purchase', 'WHITE', {'WHITE': 3}], ['canPurchase', 'WHITE', {'WHITE': 1}], ['purchase', 'WHITE', {'WHITE': 1}]])

Expected Output: [True, True, True]

Explanation: Buy a WHITE card costing 3 WHITE: gems 5->2, discount[WHITE]=1. A WHITE card costing 1 WHITE now has discounted cost max(1-1,0)=0, so it is affordable (canPurchase True) and the purchase succeeds for free.

Hints

  1. Maintain three running structures across operations: the player's gem counts, a per-color discount counter, and (optionally) a hand size.
  2. 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.
  3. canPurchase succeeds only if, for every color, owned gems >= the discounted cost.
  4. On a successful purchase, deduct the discounted cost, then increment discount[card.color] by 1 so future cards of related colors get cheaper.
Last updated: Jun 26, 2026

Related Coding Questions

  • Debug and Explain Fixes - Brex (Medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.