Check whether a hand of cards splits into groups of consecutive values

Quick Overview

Decide whether a hand of integer cards can be split into groups of a fixed size, where each group holds consecutive values and every card is used exactly once. Tests greedy reasoning, frequency counting with duplicate values, and an efficient check for hands of up to 10,000 cards.

Check whether a hand of cards splits into groups of consecutive values

Company: eBay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Alice has some number of cards, and she wants to rearrange them into groups so that each group has `group_size` cards and consists of `group_size` consecutive values. Given an integer array `hand`, where `hand[i]` is the value written on the i-th card, and an integer `group_size`, return `True` if she can rearrange the cards this way, or `False` otherwise. ### Function Signature ```python def is_n_straight_hand(hand: List[int], group_size: int) -> bool: ``` ### Rules - Every card goes into exactly one group, and every group has exactly `group_size` cards. - A group is consecutive when its values, sorted, are `x, x + 1, ..., x + group_size - 1` for some `x`. - Cards with the same value are separate cards. ### Constraints - `1 <= len(hand) <= 10^4` - `0 <= hand[i] <= 10^9` - `1 <= group_size <= len(hand)` ### Examples ```text Input: hand = [1,2,3,6,2,3,4,7,8], group_size = 3 Output: True ``` Sorted, the cards are 1, 2, 2, 3, 3, 4, 6, 7, 8. They split into [1,2,3], [2,3,4] and [6,7,8], which uses all nine cards. ```text Input: hand = [1,2,3,4,5], group_size = 4 Output: False ``` Five cards cannot form groups of 4, because 5 is not a multiple of 4. ```text Input: hand = [1,1,2,3], group_size = 2 Output: False ``` There is no card 0, so each card 1 can only go into the group [1,2]. Two such groups need two cards of value 2, and the hand has one.

Overview: Decide whether a hand of integer cards can be split into groups of a fixed size, where each group holds consecutive values and every card is used exactly once. Tests greedy reasoning, frequency counting with duplicate values, and an efficient check for hands of up to 10,000 cards.

eBay logo
eBay
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Alice has some number of cards, and she wants to rearrange them into groups so that each group has group_size cards and consists of group_size consecutive values.

Given an integer array hand, where hand[i] is the value written on the i-th card, and an integer group_size, return True if she can rearrange the cards this way, or False otherwise.

Function Signature

def is_n_straight_hand(hand: List[int], group_size: int) -> bool:

Rules

  • Every card goes into exactly one group, and every group has exactly group_size cards.
  • A group is consecutive when its values, sorted, are x, x + 1, ..., x + group_size - 1 for some x .
  • Cards with the same value are separate cards.

Constraints

  • 1 <= len(hand) <= 10^4
  • 0 <= hand[i] <= 10^9
  • 1 <= group_size <= len(hand)

Examples

Input: hand = [1,2,3,6,2,3,4,7,8], group_size = 3
Output: True

Sorted, the cards are 1, 2, 2, 3, 3, 4, 6, 7, 8. They split into [1,2,3], [2,3,4] and [6,7,8], which uses all nine cards.

Input: hand = [1,2,3,4,5], group_size = 4
Output: False

Five cards cannot form groups of 4, because 5 is not a multiple of 4.

Input: hand = [1,1,2,3], group_size = 2
Output: False

There is no card 0, so each card 1 can only go into the group [1,2]. Two such groups need two cards of value 2, and the hand has one.