Amazon | SDE Interview Questions
Anonymous User
1906

Sharing a question asked in the first tech round interview with Amazon for SDE role, interviewer only gave me 20 mins to finish this, somehow makes me think he want to fail me so bad lol.

Question:

//We are doing a game to find Super3 in a game of cards.
//You have 30 cards. [0-9] and [RGB]
//Each player gets 3 cards. 
//
//Find the winner based on following criteria.
//1. All same number
//2. All same color
//3. 2 same number and 1 other number
//....

My roughly solution, please feel free to help me find bugs and share your thought on improve this code, much appreciated !

class Card{
    int number;
    String color;
    
    public Card(int number, String color){
        this.number = number;
        this.color = color;
    }
}


class Player{
	
	// number map and freq
    HashMap<Integer, Integer> countNum = new HashMap<>();
    HashMap<String, Integer> countColor = new HashMap<>();
    
    Card[] cards;
    Character name;
    public Player(char name, Card[] cards) {
    	this.cards=cards;
    	this.name=name;
    }
    
    public void updateCounts() {
    	for(Card card: cards) {
    		if(!countNum.containsKey(card.number)) {
    			countNum.put(card.number, 1);
    		} else {
    			countNum.put(card.number, countNum.get(card.number)+1);
    		}
    		
    		
    		if(!countColor.containsKey(card.color)) {
    			countColor.put(card.color, 1);
    		} else {
    			countColor.put(card.color, countColor.get(card.color)+1);
    		}
    	}
    }
}


public class BoardGame {
	   
    // 1 winner / 1+ winners ?
    List<Character> winners=new ArrayList<>();
    int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    String[] colors = { "R", "G", "B"};
    Random rand = new Random();
    Player[] players;
    
    
    public BoardGame(int num_player){
        players = new Player[num_player];
        int i=0;
        // initiate player and hands
        while(i<num_player){
            char playerName= (char)i;
            Card[] hands = generateCards();
            
            players[i] = new Player(playerName, hands);
            players[i].updateCounts();
            
            i++;
        }
    }
    
    public List<Character> StartGame(){
        while(winners.size()==0){
            // play the game by distribute card and check winner
        	play();
        	winners = CheckWinners();
        }
        
        return winners;
    }
    
    // generate hand of three cards
    public Card[] generateCards(){
        Card[] hand = new Card[3];
        int i=0;
        while(i<3){
            Card newCard = new Card(nums[rand.nextInt(nums.length)], colors[rand.nextInt(colors.length)]);
            hand[i]=newCard;
            i++;
        }
        
        return hand;
    }
    
    
    public void play() {
    	// generate cards for each player and check win
    	for(Player p: players) {
    		p.cards = generateCards();
    		p.updateCounts();
    	}
    	
    }
    
    public List<Character> CheckWinners() {
    	// more than one winner or one winner ?
    	List<Character> winners = new ArrayList<>();
    	for(Player p: players) {
    		
    		for(Integer number: p.countNum.keySet()) {
    			if(p.countNum.size()==2 || p.countNum.get(number)==p.cards.length) {
    				winners.add(p.name);
    			}
    			
    		}
    		
    		for(String color: p.countColor.keySet()) {
    			if(p.countColor.get(color)==p.cards.length) {
    				winners.add(p.name);
    			}
    		}
    	}
    	return winners;
    }
Comments (5)