Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an integer array ranks and a character array suits. You have 5 cards where the ith card has a rank of ranks[i] and a suit of suits[i].
The following are the types of poker hands you can make from best to worst:
"Flush": Five cards of the same suit."Three of a Kind": Three cards of the same rank."Pair": Two cards of the same rank."High Card": Any single card.Return a string representing the best type of poker hand you can make with the given cards.
Note that the return values are case-sensitive.
Example 1:
Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"] Output: "Flush" Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
Example 2:
Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"] Output: "Three of a Kind" Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind". Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand. Also note that other cards could be used to make the "Three of a Kind" hand.
Example 3:
Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"] Output: "Pair" Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair". Note that we cannot make a "Flush" or a "Three of a Kind".
Constraints:
ranks.length == suits.length == 51 <= ranks[i] <= 13'a' <= suits[i] <= 'd'Problem summary: You are given an integer array ranks and a character array suits. You have 5 cards where the ith card has a rank of ranks[i] and a suit of suits[i]. The following are the types of poker hands you can make from best to worst: "Flush": Five cards of the same suit. "Three of a Kind": Three cards of the same rank. "Pair": Two cards of the same rank. "High Card": Any single card. Return a string representing the best type of poker hand you can make with the given cards. Note that the return values are case-sensitive.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[13,2,3,1,9] ["a","a","a","a","a"]
[4,4,2,4,4] ["d","a","a","b","c"]
[10,10,2,12,9] ["a","b","c","a","d"]
categorize-box-according-to-criteria)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2347: Best Poker Hand
class Solution {
public String bestHand(int[] ranks, char[] suits) {
boolean flush = true;
for (int i = 1; i < 5 && flush; ++i) {
flush = suits[i] == suits[i - 1];
}
if (flush) {
return "Flush";
}
int[] cnt = new int[14];
boolean pair = false;
for (int x : ranks) {
if (++cnt[x] == 3) {
return "Three of a Kind";
}
pair = pair || cnt[x] == 2;
}
return pair ? "Pair" : "High Card";
}
}
// Accepted solution for LeetCode #2347: Best Poker Hand
func bestHand(ranks []int, suits []byte) string {
flush := true
for i := 1; i < 5 && flush; i++ {
flush = suits[i] == suits[i-1]
}
if flush {
return "Flush"
}
cnt := [14]int{}
pair := false
for _, x := range ranks {
cnt[x]++
if cnt[x] == 3 {
return "Three of a Kind"
}
pair = pair || cnt[x] == 2
}
if pair {
return "Pair"
}
return "High Card"
}
# Accepted solution for LeetCode #2347: Best Poker Hand
class Solution:
def bestHand(self, ranks: List[int], suits: List[str]) -> str:
# if len(set(suits)) == 1:
if all(a == b for a, b in pairwise(suits)):
return 'Flush'
cnt = Counter(ranks)
if any(v >= 3 for v in cnt.values()):
return 'Three of a Kind'
if any(v == 2 for v in cnt.values()):
return 'Pair'
return 'High Card'
// Accepted solution for LeetCode #2347: Best Poker Hand
impl Solution {
pub fn best_hand(ranks: Vec<i32>, suits: Vec<char>) -> String {
if suits.iter().all(|v| *v == suits[0]) {
return "Flush".to_string();
}
let mut count = [0; 14];
let mut is_pair = false;
for &v in ranks.iter() {
let i = v as usize;
count[i] += 1;
if count[i] == 3 {
return "Three of a Kind".to_string();
}
is_pair = is_pair || count[i] == 2;
}
(if is_pair { "Pair" } else { "High Card" }).to_string()
}
}
// Accepted solution for LeetCode #2347: Best Poker Hand
function bestHand(ranks: number[], suits: string[]): string {
if (suits.every(v => v === suits[0])) {
return 'Flush';
}
const count = new Array(14).fill(0);
let isPair = false;
for (const v of ranks) {
if (++count[v] === 3) {
return 'Three of a Kind';
}
isPair = isPair || count[v] === 2;
}
if (isPair) {
return 'Pair';
}
return 'High Card';
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.