InterviewDB Experience

A Hand of Three Cards: Evaluate Poker-Like Hand Rankings for Three-Card Combinations

Interview Experience

Problem

You are given a hand of exactly 3 cards, each as (rank, suit) where rank is 2-14 (14=Ace) and suit is one of H/D/C/S. Evaluate the hand and return its category. Categories in descending order:

  1. STRAIGHT_FLUSH — three consecutive ranks, same suit
  2. THREE_OF_A_KIND — all same rank
  3. STRAIGHT — three consecutive ranks, mixed suits
  4. FLUSH — same suit, not consecutive
  5. PAIR — exactly two same rank
  6. HIGH_CARD — none of the above
python
from typing import NamedTuple

class Card(NamedTuple):
    rank: int   # 2-14
    suit: str   # H/D/C/S

def evaluate_hand(cards: list[Card]) -> str:
    ...

**Input**:  [(10,'H'),(11,'H'),(12,'H')]  -> "STRAIGHT_FLUSH"

**Input**:  [(7,'H'),(7,'D'),(7,'C')]    -> "THREE_OF_A_KIND"

**Input**:  [(2,'H'),(5,'D'),(3,'C')]    -> "STRAIGHT"

**Input**:  [(9,'H'),(3,'H'),(6,'H')]    -> "FLUSH"

**Input**:  [(9,'H'),(9,'D'),(2,'C')]    -> "PAIR"

**Input**:  [(2,'H'),(7,'D'),(K,'C')]    -> "HIGH_CARD"

Follow-ups

  1. Ace can be low (A-2-3) or high (Q-K-A). How do you handle both cases in your straight detection?
  2. Given two hands of the same category, how do you compare them to determine a winner (tiebreaker logic)?
  3. Extend to 5-card poker hands. How does your architecture change?
  4. How would you generate all possible 3-card hands from a standard 52-card deck and count hands per category?

Full Details

Problem

You are given a hand of exactly 3 cards, each as (rank, suit) where rank is 2-14 (14=Ace) and suit is one of H/D/C/S. Evaluate the hand and return its category. Categories in descending order:

  1. STRAIGHT_FLUSH — three consecutive ranks, same suit
  2. THREE_OF_A_KIND — all same rank
  3. STRAIGHT — three consecutive ranks, mixed suits
  4. FLUSH — same suit, not consecutive
  5. PAIR — exactly two same rank
  6. HIGH_CARD — none of the above
python
from typing import NamedTuple

class Card(NamedTuple):
    rank: int   # 2-14
    suit: str   # H/D/C/S

def evaluate_hand(cards: list[Card]) -> str:
    ...

**Input**:  [(10,'H'),(11,'H'),(12,'H')]  -> "STRAIGHT_FLUSH"

**Input**:  [(7,'H'),(7,'D'),(7,'C')]    -> "THREE_OF_A_KIND"

**Input**:  [(2,'H'),(5,'D'),(3,'C')]    -> "STRAIGHT"

**Input**:  [(9,'H'),(3,'H'),(6,'H')]    -> "FLUSH"

**Input**:  [(9,'H'),(9,'D'),(2,'C')]    -> "PAIR"

**Input**:  [(2,'H'),(7,'D'),(K,'C')]    -> "HIGH_CARD"

Follow-ups

  1. Ace can be low (A-2-3) or high (Q-K-A). How do you handle both cases in your straight detection?
  2. Given two hands of the same category, how do you compare them to determine a winner (tiebreaker logic)?
  3. Extend to 5-card poker hands. How does your architecture change?
  4. How would you generate all possible 3-card hands from a standard 52-card deck and count hands per category?

About This Question

This is a candidate experience report from a instacart interview during the phone round.

It covers the following topics: Coding, Onsite, Phone .