DSA interview question
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]).
bool isNStraightHand(const vector<int>& hand, int groupSize);
Input (hand, W) | Output | Explanation |
|---|---|---|
([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) | false | 5 cards can't form groups of 4 |
([1, 1, 2, 2, 3, 3], 3) | true | Two runs of 1-2-3 |
([1, 2, 4], 3) | false | The 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.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in