Coding · stretch-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix

Hand of Straights

mediumgreedyhash-map

Problem

You hold a hand of cards, given as an integer array hand where each value is a card's rank. You want to rearrange the entire hand into groups of exactly groupSize cards each, where every group consists of consecutive ranks (for example 4, 5, 6). Every card must be used in exactly one group. Return True if such an arrangement exists and False otherwise.

  • The hand has between 1 and 50,000 cards.
  • Ranks are integers between 0 and 2,000,000,000, so they cannot be used as array indices.
  • groupSize is between 1 and the number of cards.

Examples

  • hand = [5, 7, 6, 9, 8, 6, 10, 7, 8], groupSize = 3 → True. Groups: [5, 6, 7], [6, 7, 8], [8, 9, 10].
  • hand = [2, 3, 4, 5, 6, 7, 8], groupSize = 4 → False. Seven cards cannot be split into groups of four.
  • hand = [4, 4, 5, 6], groupSize = 2 → False. The two 4s each need a 5, but there is only one 5.

Hints

  1. Consider the smallest card still in your hand. Is there any choice about which group it belongs to?
  2. Once the smallest card must start a group, which other cards are forced into that group?
  3. Count the cards, walk ranks in increasing order, and for each rank with a nonzero count, subtract that count from the next groupSize ranks; fail if any is short.

Last reviewed 8 September 2026