InterviewDB Question

Group Cards by Consecutive Runs of K

Question Details

Problem

You are given a hand of cards represented as an integer array. Rearrange the cards into groups of exactly k consecutive cards (e.g., [1,2,3], [2,3,4]).

Return True if this is possible, False otherwise.

python
def can_group_cards(hand: list[int], k: int) -> bool:
    pass

**Input**:  hand = [1, 2, 3, 6, 2, 3, 4, 7, 8], k = 3
Output: True
# Groups: [1,2,3], [2,3,4], [6,7,8]

**Input**:  hand = [1, 2, 3, 4, 5], k = 4
Output: False
# 5 cards not divisible into groups of 4

**Input**:  hand = [1, 1, 2, 2, 3, 3], k = 3
Output: True
# Groups: [1,2,3], [1,2,3]

Follow-ups

  1. What is the time complexity of your approach using a sorted map?
  2. If k is not fixed — find the minimum k >= 2 for which grouping is possible — how do you solve it?
  3. How does this problem relate to LeetCode 846 and 1296? What differs?
  4. What if cards can be repeated in the same group?

Full Details

Problem

You are given a hand of cards represented as an integer array. Rearrange the cards into groups of exactly k consecutive cards (e.g., [1,2,3], [2,3,4]).

Return True if this is possible, False otherwise.

python
def can_group_cards(hand: list[int], k: int) -> bool:
    pass

**Input**:  hand = [1, 2, 3, 6, 2, 3, 4, 7, 8], k = 3
Output: True
# Groups: [1,2,3], [2,3,4], [6,7,8]

**Input**:  hand = [1, 2, 3, 4, 5], k = 4
Output: False
# 5 cards not divisible into groups of 4

**Input**:  hand = [1, 1, 2, 2, 3, 3], k = 3
Output: True
# Groups: [1,2,3], [1,2,3]

Follow-ups

  1. What is the time complexity of your approach using a sorted map?
  2. If k is not fixed — find the minimum k >= 2 for which grouping is possible — how do you solve it?
  3. How does this problem relate to LeetCode 846 and 1296? What differs?
  4. What if cards can be repeated in the same group?

About This Question

This is a reported interview question from a ziphq interview during the phone round.

It covers the following topics: Arrays, Coding, Phone, Onsite .