Magicsheet logo

Best Poker Hand

Easy
37.5%
Updated 8/1/2025

Asked by 2 Companies

Best Poker Hand

What is this problem about?

The "Best Poker Hand interview question" is a classification problem based on card game rules. You are given two arrays: ranks (the numerical values of five cards) and suits (the suits of those same five cards). Your task is to identify the best poker hand you can form according to a simplified set of rules: "Flush" (all five cards have the same suit), "Three of a Kind" (three or more cards have the same rank), "Pair" (two cards have the same rank), or "High Card" (none of the above).

Why is this asked in interviews?

Companies like Apple and Amazon use the "Best Poker Hand coding problem" as a warm-up exercise. It tests a candidate's ability to use "Hash Table interview pattern" logic and "Counting" techniques. It’s a straightforward test of clean coding, conditional logic, and the ability to process multiple data points into a single categorical result.

Algorithmic pattern used

This problem follows the Frequency Counting and Categorization pattern.

  1. Flush Check: First, check if all values in the suits array are identical. This is the highest-ranking hand in this simplified version.
  2. Rank Counting: Use a hash map or a frequency array to count how many times each rank appears in the ranks array.
  3. Hierarchy Logic:
    • If any rank appears 3\geq 3 times, it's "Three of a Kind".
    • Else if any rank appears 2\geq 2 times, it's "Pair".
    • Otherwise, it's "High Card". The order of checking is important to ensure the "best" hand is returned.

Example explanation

Ranks: [13, 2, 3, 1, 9], Suits: ['a', 'a', 'a', 'a', 'a']

  • Result: "Flush" (All suits are 'a'). Ranks: [4, 4, 2, 4, 4], Suits: ['a', 'b', 'c', 'd', 'e']
  • Max rank count is 4 (for rank 4). Since 4 is 3\geq 3, result is "Three of a Kind". Ranks: [10, 10, 2, 12, 9], Suits: ['a', 'b', 'c', 'a', 'd']
  • Max rank count is 2 (for rank 10). Result is "Pair".

Common mistakes candidates make

  • Wrong Order: Checking for a "Pair" before checking for "Three of a Kind". You must check from the highest-value hand to the lowest.
  • Suit Counting: Trying to count the frequency of ranks before checking for a Flush. Since Flush is independent of rank, it should be the first check.
  • Redundant Logic: Using complex sorting when simple frequency counting is faster and more direct.

Interview preparation tip

Get comfortable with hash maps (dictionaries in Python, Maps in JS/Java) for counting frequencies. This is a recurring "Array interview pattern" that helps you avoid nested loops and keeps your solution in O(N)O(N) time.

Similar Questions