KatInterview

Gem Purchase Simulator with Color-Based Discounts

Data Structure DesignAuto-graded · 18 test cases
EasyAuto-gradedData Structure Designsimulationstate-managementhash-map

Last updated

Tests stateful simulation with resource tracking, discount accumulation, and transactional updates.

You are building a gem-trading game simulator where players buy cards using colored gems.

Five gem colors exist: BLUE, WHITE, GREEN, RED, and YELLOW.

Each card is represented as a dictionary with:

  • "color": a string indicating which color discount this card provides once purchased
  • "cost": a dictionary mapping gem colors to the quantity required to buy this card

You must simulate a sequence of operations on a player's state. The player begins with:

  • A starting inventory of gems (given as a dictionary mapping colors to quantities)
  • No purchased cards
  • Zero discount in every color

Discount Mechanic:

When purchasing a card, the player receives a discount equal to the number of cards they already own in each gem color. Specifically, if the player owns d cards of a particular color, they pay d fewer gems of that color. The effective cost for any color cannot drop below zero.

Operations:

You will process two types of operations:

  1. "canPurchase": Check whether the player can currently afford the specified card (after applying discounts). Return true or false. This operation does not modify any state.

  2. "purchase": Attempt to buy the specified card. If the player cannot afford it after discounts, return false and make no changes to state. If affordable:

    • Deduct the discounted gem cost from the player's inventory
    • Increment the discount counter for the purchased card's color by 1
    • Return true

Function Signature:

Implement process_gem_operations(starting_gems, card_list, operation_types, operation_args):

  • starting_gems: Dictionary mapping color names to initial gem quantities. Colors not present have quantity 0.
  • card_list: List of card dictionaries (each with "color" and "cost" keys). Missing cost entries mean 0.
  • operation_types: List of operation names ("canPurchase" or "purchase").
  • operation_args: List of argument lists. Each inner list contains one integer: the index into card_list.

Return a list of booleans, one result per operation in order.

Constraints:

  • 1 ≤ number of operations ≤ 100
  • 0 ≤ number of cards ≤ 50
  • 0 ≤ gem quantities ≤ 100
  • 0 ≤ cost values ≤ 100
  • All color names are from the five valid colors
  • Card indices in operations are valid
  • All operations are either "canPurchase" or "purchase"

Examples

Example 1

Input: process_gem_operations({'WHITE': 3, 'RED': 1}, [{'color': 'BLUE', 'cost': {'WHITE': 4, 'RED': 1}}], ['canPurchase', 'purchase', 'canPurchase'], [[0], [0], [0]])

Output: [False, False, False]

Example 2

Input: process_gem_operations({'WHITE': 5, 'RED': 1}, [{'color': 'WHITE', 'cost': {'WHITE': 4}}, {'color': 'RED', 'cost': {'WHITE': 2, 'RED': 1}}], ['purchase', 'canPurchase', 'purchase', 'canPurchase'], [[0], [1], [1], [1]])

Output: [True, True, True, False]

Example 3

Input: process_gem_operations({'BLUE': 0, 'WHITE': 0, 'GREEN': 0, 'RED': 0, 'YELLOW': 0}, [{'color': 'GREEN', 'cost': {}}], ['canPurchase', 'purchase', 'canPurchase'], [[0], [0], [0]])

Output: [True, True, True]

Hints

Hint 1

Calculate the effective cost for each color as max(0, base_cost - owned_cards_of_that_color) to ensure costs never go negative.

Hint 2

Validate affordability across all colors before modifying any state in a purchase operation to maintain atomicity.

Hint 3

The purchased card only grants its discount to future purchases, not to itself.