Brex Phone Interview

mail@pastecode.io avatar
unknown
java
2 years ago
4.7 kB
4
Indexable
Never
/*
Q1: 
We want to write a function can_purchase() such that, given a particular card and collection of gems for a player, we return true if the player can afford the card, and false if they cannot.\

Q2:
We want to write a function purchase() such that, given a particular card and collection of gems for a player, we add the card to the player's hand and subtract the cost from the player's gems, if they are able to afford the card. The function should return true if the player can afford the card, and false if they cannot.

Q3:
We want to introduce a new concept: for each card in a player’s hand of a given color, we want to reduce the cost of any new purchase by 1 gem for that held card’s color. For example, if the player holds 2 (G)reen cards and 1 (R)ed, and we are considering a card that lists its cost as 3 (G)reen, 2 (R)ed, and 1 (B)lue, then the player should be able to purchase it for 1 G, 1 R, and 1 B.

Note: the discount should never lower the gem cost below zero.

You should ensure that both purchase() and can_purchase() can handle this.

Card
Name - String

Gem
Color - String


Boolean CanPurchase(Card card, Map<Gem, Integer> gems);


*/

import java.io.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.*;

// Main class should be named 'Solution'
class Solution {
    enum COLOR { 
        WHITE, GREEN, RED, YELLOW, BLUE 
    };
    static class Card {
        Gem gem;
        Map<Gem, Integer> cost; 
        public Card(Gem gem, Map<Gem, Integer> cost) {
            this.gem = gem;
            this.cost = cost;
        }
    }
    static class Gem {
        COLOR color;
        public Gem(COLOR color) {
            this.color = color;
        }
    }
    static class Player {
        Map<Gem, Integer> gems;
        List<Card> hand;
        public Player(Map<Gem, Integer> gems) {
            this.gems = gems;
            this.hand = new ArrayList<>();
        }
        
        public void addCard(Card card) {
            this.hand.add(card);
        }
    }
    
    public static void main(String[] args) {
        Gem gem1 = new Gem(COLOR.WHITE);
        Gem gem2 = new Gem(COLOR.GREEN);
        Gem gem3 = new Gem(COLOR.RED);
        Map<Gem, Integer> cardCost = new HashMap<>();
        cardCost.put(gem1, 3);
        cardCost.put(gem2, 2);
        cardCost.put(gem3, 1);
        Card card1 = new Card(gem2, cardCost);
        
        Map<Gem, Integer> playerWallet = new HashMap<>();
        playerWallet.put(gem1, 6);
        playerWallet.put(gem2, 3); 
        playerWallet.put(gem3, 2); 
        
        Player player = new Player(playerWallet);
        purchase(card1, player);
        purchase(card1, player);
        for (Gem key: player.gems.keySet()){  
			System.out.println(key.color.toString()+ " = " + player.gems.get(key));
		} 
        for (Card card: player.hand) {
            System.out.println("Player's hand card: " + card.gem.color);
        }
    }
    
    public static boolean canPurchase(Map<Gem, Integer> currentCost, Player player) {
        
        Map<Gem, Integer> playerGems = player.gems;
        for (Gem gem: currentCost.keySet()){
            int playerGemCount = playerGems.getOrDefault(gem, 0);
            int cardGemCost = currentCost.get(gem);
            if (playerGemCount < cardGemCost) {
                return false;
            }
        }
        
        return true;
    }
    
    public static boolean purchase(Card card, Player player) {
        Map<Gem, Integer> currentCost = getCurrentCost(card, player);
        
        if (!canPurchase(currentCost, player)) {
            return false;
        }
        
        Map<Gem, Integer> costMap = currentCost;
        Map<Gem, Integer> playerGems = player.gems;
        for (Gem gem: costMap.keySet()){
            int playerGemCount = playerGems.getOrDefault(gem, 0);
            int cardGemCost = costMap.get(gem);
            
            playerGems.put(gem, playerGemCount - cardGemCost);
        }
        
        player.addCard(card);
        return true;
    }
    
    public static Map<Gem, Integer> getCurrentCost(Card card, Player player) {
        Map<Gem, Integer> currentCost = new HashMap<>();
        for (Gem key: card.cost.keySet()) {
            currentCost.put(key, card.cost.get(key));
        }
        for (Card c: player.hand) {
            currentCost.put(c.gem, currentCost.getOrDefault(c.gem, 0) - 1);
        }
        
        for (Gem gem: currentCost.keySet()) {
            System.out.println("Wallet: " + gem.color + ", " + currentCost.get(gem));
        }
        return currentCost;
    }
}