PPractick

DSA interview question

Hand of Straights

Medium · Data structures · Topics: heap, greedy, hash map

The problem

A card game deals a hand of numbered cards, and the player wants to split the whole hand into groups of exactly W cards, where each group is a run of consecutive numbers, like 3-4-5. Is it possible? Greedy works because the choice is forced: the smallest card left can't be in the middle or at the end of any run (nothing smaller remains), so it must start a run. Take it and the next W − 1 consecutive values, fail if any is missing, and repeat. An ordered map of counts makes "smallest card left" and "is x + 1 available?" cheap.

Example: [1, 2, 3, 6, 2, 3, 4, 7, 8], W = 3 → true ([1,2,3] [2,3,4] [6,7,8]).

Function signature

bool isNStraightHand(const vector<int>& hand, int groupSize);

Test cases

Input (hand, W)OutputExplanation
([1, 2, 3, 6, 2, 3, 4, 7, 8], 3)true[1,2,3] [2,3,4] [6,7,8]
([1, 2, 3, 4, 5], 4)false5 cards can't form groups of 4
([1, 1, 2, 2, 3, 3], 3)trueTwo runs of 1-2-3
([1, 2, 4], 3)falseThe size fits, but 3 is missing
([1], 1)true

(Confirm: every card must be used; groups are runs of consecutive values of length exactly W.)

How interviewers phrase it

Clarifying questions to ask

  1. Must every card be used? (Yes.)
  2. Can cards repeat? (Yes.)
  3. How large are the values? (Up to 10⁹, so use a map, not an array.)

Follow-ups to expect

  1. "Why must the smallest card start a group?"
  2. "What's the quick rejection?"
  3. "How does a min-heap version work?"
  4. "Can it be O(n)-ish after sorting?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in