PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Brex

Implement discounted card purchases

Last updated: May 11, 2026

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.

Related Interview Questions

  • Debug and Explain Fixes - Brex (Medium)
Brex logo
Brex
Jan 5, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
24
0

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.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Brex•More Software Engineer•Brex Software Engineer•Brex Coding & Algorithms•Software Engineer Coding & Algorithms
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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.