Amazon.com

www.amazon.com
Employer Engaged

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

3 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
3

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

Add Answers or Comments

To comment on this, Sign In or Sign Up.