Katkat

Implement discounted card purchases

BrexSoftware Engineer · Technical Screen

Last updated

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 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.
  1. A discounted cost for any color cannot go below 0.
  2. canPurchase(card) should return true if the player can afford the card after applying discounts, otherwise false.
  3. 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.