The Hand of Straights interview question asks you to determine if a given array of integers (hand of cards) can be rearranged into groups of a specific size groupSize, where each group consists of consecutive integers. For example, if groupSize is 3, a valid group could be [1, 2, 3]. Every card in the hand must be part of exactly one group.
This problem is popular at companies like Apple and Amazon because it tests a candidate's ability to apply a greedy interview pattern to a collection of data. It evaluation whether you can efficiently manage counts of elements and identify the "bottleneck" (the smallest available card). It requires a combination of Hash Table for frequency counting and Sorting or an Ordered Set to process elements in the correct order.
The problem is solved using a Greedy approach with Frequency Counting.
groupSize. If not, return false.Hand: [1, 2, 3, 6, 2, 3, 4, 7, 8], groupSize = 3.
{1:1, 2:2, 3:2, 4:1, 6:1, 7:1, 8:1}.[1, 2, 3].
{2:1, 3:1, 4:1, 6:1, 7:1, 8:1}.[2, 3, 4].
{6:1, 7:1, 8:1}.[6, 7, 8].
[1, 2, 3] groups).Whenever you have a problem involving "consecutive groups," always process from the minimum value. This ensures that the smallest element must be the start of a group, which simplifies the decision logic and avoids the need for backtracking.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find Original Array From Doubled Array | Medium | Solve | |
| Divide Array in Sets of K Consecutive Numbers | Medium | Solve | |
| Array of Doubled Pairs | Medium | Solve | |
| Largest Values From Labels | Medium | Solve | |
| Least Number of Unique Integers after K Removals | Medium | Solve |