Back to Practice Dashboard
Top 150 InterviewEasy

Hand of Straights

Learn how to solve the 'Hand of Straights' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards. Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return True if she can rearrange the cards, or False otherwise.

Write a function isNStraightHand(hand: List[int], groupSize: int) -> bool.

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

Examples

Example 1
Input
hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
Output
True
Explanation

[1,2,3], [2,3,4], [6,7,8] are consecutive groups of 3.

Example 2
Input
hand = [1,2,3,4,5], groupSize = 4
Output
False
Explanation

Cannot rearrange cards into consecutive groups of 4.

Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor