Amazon Interview Question: An optimal algorithm to check... | Glassdoor

Interview Question

Software Development Engineer Intern Interview

An optimal algorithm to check whether a hand of cards was a

  full house (in Poker) or not.
Tags:
algorithm
Answer

Interview Answer

8 Answers

0

5 card draw?

Take the set of the cards
    if the carnality of the set of the cards is not 2:
        it is not a full house.
    If it is:
        Take the first element of the set of cards
        count the number of occurrences of this card in the original list
            if there are 2 or 3 occurrences it is a full house
            otherwise it is not (it must be 4 of a kind)

Nick Topper on Nov 15, 2014
5

Check first card, store number of card, set first counter to one
Check number of next cards
 - If different card, store number of card, set second counter to one, break
 - If same card, increment first counter, check if greater than three:
  - If less or equal to three, continue
  - If greater than three, return false
Continue checking cards
 - If matching either stored card, increment respective counter, check if >3
  - If less or equal to three, continue
  - If greater than three, return false
 - If not matching either card, return false
Return true

Anonymous on Jan 8, 2015
0

1. Draw first card. card1 = firstCard and count1 = 1
2. Draw next card x and repeat upto step 12 for all cards
3. If (card2 != null && (x!=card1 && x != card2)
4. return false
5. if(x == card1)
7. count1++;
8. if(card2 == x)
9. count2++;
10. if (card2 == null)
11. card2 = x and count2 = 1
12. go to step 2
13. if(abs(count1-count2) == 1)
14. return true
15. else return false

public static boolean isFullHouse(String[] s){
        int count1 = 1, count2 = 0;
        String card1 = s[0], card2 = null, nextCard;
        for(int i = 1; i < 5; i++){
             nextCard = s[i];
             if(card2 != null && (!nextCard.equals(card1) && !nextCard.equals(card2)))
                 return false;
             if(nextCard.equals(card1))
                 count1++;
             else if(nextCard.equals(card2))
                 count2++;
             else if(card2 == null){
                 card2 = nextCard;
                 count2 = 1;
             }
        }

        if(Math.abs(count2-count1) == 1)
            return true;
        else
            return false;
    }

chepa on Feb 18, 2015
0

Most optimal and easy in my opinion:
1. Cards are represented as int : 2, 3, .., 10, 11='J', 12='Q',13= 'K',14= 'A'
2. Hand is build of n cards vector hand

int bits;
for(int i=1; i<5; i++){
    if(bits&(1<<hand[i])){

}

Anonymous on Jun 27, 2016
0

(SOLVE PROBLEM OF FOUR OF KIND (but can be easly modified) )

Most optimal in my opinion:
1. Cards are represented as int : 2, 3, .., 10, 11='J', 12='Q',13= 'K',14= 'A'
2. Hand is build of 5 cards vector hand

int bits;

for(int i=0; i<5; i++){
    if(bits&(1<<hand[i])){ //check wether first bit coresponding to given card is set
        if(bits&(1<<hand[i]+15)){ //check wether second bit coresponding to given card is set
            bits=bits&~(1<<hand[i]) // if it is unset first bit coresponding to given card
        }
        else{
            bits=bits|(1<<hand[i]+15) // if it is not set second bit coresponding to given card
        }
    }
    else{
        if(bits&(1<<hand[i]+15)){ //check wether second bit coresponding to given card is set
            bits=bits&~(1<<hand[i]+15) // if it is unset second bit coresponding to given card
        }
        else{
            bits=bits|(1<<hand[i]) // if it is not set first bit coresponding to given card
        }
    }
}

if( !(bits & (bits-1))){ // check if only one card differ to others
    cout<<"Four of kind in hand!"<<endl;
}
else{
    cout<<"Not this time!"<<endl;
}

Anonymous on Jun 27, 2016
0

We can use set in c++ ,
if hand.size() == 5;
sets(hand) ;
if(s.size() == 2)

Anonymous on Sep 19, 2016
0

We can use set in c++ ,
if hand.size() == 5;
sets(hand) ;
if(s.size() == 2)
return true;
return false;

Anonymous on Sep 19, 2016
0

We can use set in c++ ,
if hand.size() == 5;
sets(hand) ;
if(s.size() == 2)
return true;
return false;

Anonymous on Sep 19, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.