Check whether a player has enough gems of every color to buy a card

Quick Overview

Write a function that decides whether a player in a gem-based card game owns enough gems of every color to buy a card. It tests element-wise comparison of two parallel arrays, early exit, and why comparing total gem counts gives the wrong answer.

Check whether a player has enough gems of every color to buy a card

Company: Brex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A card game uses several gem colors. You are given two integer arrays of the same length, where index `i` refers to the same gem color in both: - `player_gems[i]` is the number of gems of color `i` the player owns. - `card_cost[i]` is the number of gems of color `i` the card costs. Implement `can_purchase(player_gems, card_cost)`, which returns `True` if the player can afford the card, meaning that for every color the player owns at least as many gems as the card costs. Otherwise it returns `False`. ### Function Signature ```python def can_purchase(player_gems: list[int], card_cost: list[int]) -> bool: ``` ### Rules - Both arrays have the same length, and index `i` is the same color in both arrays. - Gems of one color cannot pay for another color. - A cost of 0 for a color needs no gems of that color. - The function only answers whether the purchase is possible. It does not change either array. ### Constraints - `1 <= len(player_gems) == len(card_cost) <= 5` - `0 <= player_gems[i] <= 10` - `0 <= card_cost[i] <= 10` ### Examples ```text Input: player_gems = [2, 3, 1, 0, 4], card_cost = [1, 2, 1, 0, 3] Output: True ``` Every color is covered: 2 >= 1, 3 >= 2, 1 >= 1, 0 >= 0 and 4 >= 3, so the player can buy the card. ```text Input: player_gems = [1, 0, 2], card_cost = [2, 0, 1] Output: False ``` The card costs 2 gems of the first color and the player owns only 1. The spare gem in the third color does not help, because it is a different color. ```text Input: player_gems = [0, 0, 0], card_cost = [0, 0, 0] Output: True ``` The card costs nothing in every color, so a player with no gems can still afford it.

Overview: Write a function that decides whether a player in a gem-based card game owns enough gems of every color to buy a card. It tests element-wise comparison of two parallel arrays, early exit, and why comparing total gem counts gives the wrong answer.

Brex logo
Brex
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A card game uses several gem colors. You are given two integer arrays of the same length, where index i refers to the same gem color in both:

  • player_gems[i] is the number of gems of color i the player owns.
  • card_cost[i] is the number of gems of color i the card costs.

Implement can_purchase(player_gems, card_cost), which returns True if the player can afford the card, meaning that for every color the player owns at least as many gems as the card costs. Otherwise it returns False.

Function Signature

def can_purchase(player_gems: list[int], card_cost: list[int]) -> bool:

Rules

  • Both arrays have the same length, and index i is the same color in both arrays.
  • Gems of one color cannot pay for another color.
  • A cost of 0 for a color needs no gems of that color.
  • The function only answers whether the purchase is possible. It does not change either array.

Constraints

  • 1 <= len(player_gems) == len(card_cost) <= 5
  • 0 <= player_gems[i] <= 10
  • 0 <= card_cost[i] <= 10

Examples

Input: player_gems = [2, 3, 1, 0, 4], card_cost = [1, 2, 1, 0, 3]
Output: True

Every color is covered: 2 >= 1, 3 >= 2, 1 >= 1, 0 >= 0 and 4 >= 3, so the player can buy the card.

Input: player_gems = [1, 0, 2], card_cost = [2, 0, 1]
Output: False

The card costs 2 gems of the first color and the player owns only 1. The spare gem in the third color does not help, because it is a different color.

Input: player_gems = [0, 0, 0], card_cost = [0, 0, 0]
Output: True

The card costs nothing in every color, so a player with no gems can still afford it.