The interview started with a simple affordability check, then progressed to purchasing cards and updating player state. The final follow-up introduced a discount mechanism where previously purchased cards reduced the number of gems required for future purchases. I implemented the functionality in Java using classes, maps, and lists.
Interview Details
Coding: Card Purchasing and Discount System**
The interviewer presented a game involving players, collectible gems, and purchasable cards. Each card had a color and a cost expressed as a collection of colored gems. The game supported five gem colors:
enum Color {
BLUE,
WHITE,
GREEN,
RED,
YELLOW
}
enum Color {
BLUE,
WHITE,
GREEN,
RED,
YELLOW
}
A player maintained an inventory of gems and a collection of purchased cards. The task was to implement the operations needed to determine whether a player could afford a card, purchase it, and eventually apply discounts based on cards already owned.
Part 1 — Check Whether a Player Can Purchase a Card
The first task was to implement a function called can_purchase() that accepts a card and a player's gem collection. The function should return true if the player has enough gems of every required color to afford the card, and false otherwise.
Each card's cost was represented by a mapping from gem colors to required quantities. The player had a corresponding mapping representing the number of gems available for each color.
For example:
Player's Gems:
BLUE = 4
GREEN = 3
RED = 2
Card Cost:
BLUE = 2
GREEN = 2
RED = 1
Output:
true
Player's Gems:
BLUE = 4
GREEN = 3
RED = 2
Card Cost:
BLUE = 2
GREEN = 2
RED = 1
Output:
true
In this example, the player has enough gems of all three required colors. If the card instead required three red gems, the result would be false because the player only has two.
Part 2 — Purchase a Card and Update Player State
The next task was to implement purchase(), which performs the actual purchase if the player has enough gems. The function needed to check affordability first. If the player could afford the card, it should deduct the required
Unlock Full Details
Create a free account to unlock this interview experience report.