DSA Tracker

Problem

Medium

Hand of Straights

A medium Greedy interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Greedy
Sheets
1
Core for
3 roles

The problem

You have an integer array hand where hand[i] is the value of the ith card, and an integer groupSize. Check if you can rearrange the cards into groups of groupSize consecutive cards. Return true if possible.

Example 1

Input
hand = [4,5,6,2,3,4,5,6,7], groupSize = 3
Output
true
Why
The cards split into [2,3,4], [4,5,6] and [5,6,7].

Example 2

Input
hand = [3,4,5,6], groupSize = 3
Output
false
Why
Four cards cannot split into groups of three.

Constraints

  • 1 <= hand.length <= 10^5
  • 1 <= hand[i] <= 10^9
  • 1 <= groupSize <= hand.length

How to think about it

Updated 2026-09-29

The smallest available card cannot be part of any group other than one where it serves as the opening value. Once you lock in the minimum card, its succeeding group members are completely predetermined, leaving no choices to deliberate.

Approaches, worst first

  1. Sort and frequency map

    time O(n log n) · space O(n)

    Count frequencies of all cards and sort unique values. Repeatedly take the smallest remaining card and decrement counts for the next groupSize consecutive values. Fails immediately if any expected consecutive card is missing.

  2. Queue-based greedy matchingWrite this one

    time O(n log n) · space O(n)

    Sort unique keys, maintain a queue of active group starts with counts, and verify consecutive transitions in a sliding window manner. Avoids nested lookups for each individual card in a group.

Where people lose marks · 3
  • Failing to check `hand.length % groupSize === 0` at the very beginning wastes execution time on arrays that cannot partition evenly.
  • Card values can reach 10^9, so an array-based frequency counter will exhaust memory; use a hash table or tree map.
  • Re-checking depleted card values: skipping counts that have already hit zero is essential for maintaining efficiency.

The theory behind it

Greedy — the ground this problem stands on. All Greedy problems

What Greedy is

A greedy algorithm makes the best-looking choice available right now, at every step, without ever looking back or second-guessing its decision. Think of a cashier making change by handing over the largest possible coin first, repeatedly, until the total is reached. Unlike dynamic programming, which saves and compares answers to multiple overlapping paths, a greedy strategy commits to one immediate option and keeps moving forward.

When to reach for it

Reach for greedy when problems ask for minimum jumps, interval scheduling, assigning resources to maximize satisfaction, or finding fractional values. Key signals include sorted orders where greedily taking the next item never hurts future options, or gas station round trips where running balances prove reachability. If you can prove that taking the immediate best choice never leaves you worse off than any alternative, greedy gives the fastest answer.

How the pattern works

Start by sorting the input to bring the most promising candidates to the front. At each position, evaluate your local rule, take the best available piece, and update your running state. The crucial mental step is proving the greedy choice property: demonstrate that picking this immediate winner cannot block a better global solution down the road. If choosing an item now forces you to reconsider past decisions when conditions change later, greedy fails and you must switch to dynamic programming instead.

More Greedy problems

Target Roles

Core requirement for 3 roles:

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .