PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/ZipHQ

Find flush and straight groups in cards

Last updated: Mar 29, 2026

Quick Overview

This question evaluates array and string manipulation, grouping and sequence-detection competencies, specifically handling same-rank aggregation and same-suit consecutive runs with overlap and maximality considerations.

  • medium
  • ZipHQ
  • Coding & Algorithms
  • Software Engineer

Find flush and straight groups in cards

Company: ZipHQ

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given an array of strings `cards`, where each string represents a distinct playing card. Each card string has the format `<rank><suit>`: - `rank` is an integer from 1 to 13 (you can assume it is written without leading zeros, e.g., `1`, `10`, `13`). - `suit` is a single uppercase letter from the set `{C, D, H, S}` (Clubs, Diamonds, Hearts, Spades). Example of input: ```text ["1C", "1H", "1D", "2C", "3C"] ``` We define a **valid group of cards** as any set of cards that satisfies **either** of the following conditions: 1. **Same-suit consecutive run (a straight flush–style run):** - All cards in the group have the **same suit**. - Their ranks form a sequence of **consecutive integers** with step 1 when sorted by rank. - The group size is at least 3. 2. **Same-rank set (three-of-a-kind–style set):** - All cards in the group have the **same rank** (suits can be different). - The group size is at least 3. Additional requirement for same-suit consecutive runs: - For each **maximal** same-suit consecutive run of length `L ≥ 3` (i.e., you cannot extend it on either side within that suit), you must output **every contiguous sub-run** whose length is between 3 and `L`, inclusive. - Example: if for suit `C` you have ranks `[1,2,3,4,5,6]`, then you must output all of these runs (written here as ranks only): - length 3: `[1,2,3]`, `[2,3,4]`, `[3,4,5]`, `[4,5,6]` - length 4: `[1,2,3,4]`, `[2,3,4,5]`, `[3,4,5,6]` - length 5: `[1,2,3,4,5]`, `[2,3,4,5,6]` - length 6: `[1,2,3,4,5,6]` For same-rank sets: - For each rank that appears `k ≥ 3` times (with any suits), you should output **one group** containing **all** cards of that rank. A card group that satisfies both properties (for example, three cards of the same suit and in consecutive ranks) should still be output **only once** as a single group. The result should be all such valid groups, represented as lists of card strings. The order of groups in the output and the order of cards within each group do **not** matter. **Example** Input: ```text ["1C", "1H", "1D", "2C", "3C"] ``` Valid groups are: - Same-suit consecutive run (suit `C`, ranks 1,2,3): - `["1C", "2C", "3C"]` - Same-rank set (rank 1, suits `C`, `H`, `D`): - `["1C", "1H", "1D"]` So one possible output is: ```text [["1C", "2C", "3C"], ["1C", "1H", "1D"]] ``` **Task**: Implement a function that, given `cards`, returns all valid groups as described above. Provide the algorithm and code to compute the output.

Quick Answer: This question evaluates array and string manipulation, grouping and sequence-detection competencies, specifically handling same-rank aggregation and same-suit consecutive runs with overlap and maximality considerations.

Related Interview Questions

  • Find shortest path on infinite grid - ZipHQ (hard)
  • Maximize non-adjacent sum on an N-ary tree - ZipHQ (medium)
  • Compute maximum sum in a tree without adjacency - ZipHQ (Medium)
  • Find root IDs and paths - ZipHQ (Medium)
ZipHQ logo
ZipHQ
Nov 28, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
34
0

You are given an array of strings cards, where each string represents a distinct playing card. Each card string has the format <rank><suit>:

  • rank is an integer from 1 to 13 (you can assume it is written without leading zeros, e.g., 1 , 10 , 13 ).
  • suit is a single uppercase letter from the set {C, D, H, S} (Clubs, Diamonds, Hearts, Spades).

Example of input:

["1C", "1H", "1D", "2C", "3C"]

We define a valid group of cards as any set of cards that satisfies either of the following conditions:

  1. Same-suit consecutive run (a straight flush–style run):
    • All cards in the group have the same suit .
    • Their ranks form a sequence of consecutive integers with step 1 when sorted by rank.
    • The group size is at least 3.
  2. Same-rank set (three-of-a-kind–style set):
    • All cards in the group have the same rank (suits can be different).
    • The group size is at least 3.

Additional requirement for same-suit consecutive runs:

  • For each maximal same-suit consecutive run of length L ≥ 3 (i.e., you cannot extend it on either side within that suit), you must output every contiguous sub-run whose length is between 3 and L , inclusive.
    • Example: if for suit C you have ranks [1,2,3,4,5,6] , then you must output all of these runs (written here as ranks only):
      • length 3: [1,2,3] , [2,3,4] , [3,4,5] , [4,5,6]
      • length 4: [1,2,3,4] , [2,3,4,5] , [3,4,5,6]
      • length 5: [1,2,3,4,5] , [2,3,4,5,6]
      • length 6: [1,2,3,4,5,6]

For same-rank sets:

  • For each rank that appears k ≥ 3 times (with any suits), you should output one group containing all cards of that rank.

A card group that satisfies both properties (for example, three cards of the same suit and in consecutive ranks) should still be output only once as a single group.

The result should be all such valid groups, represented as lists of card strings. The order of groups in the output and the order of cards within each group do not matter.

Example

Input:

["1C", "1H", "1D", "2C", "3C"]

Valid groups are:

  • Same-suit consecutive run (suit C , ranks 1,2,3):
    • ["1C", "2C", "3C"]
  • Same-rank set (rank 1, suits C , H , D ):
    • ["1C", "1H", "1D"]

So one possible output is:

[["1C", "2C", "3C"], ["1C", "1H", "1D"]]

Task: Implement a function that, given cards, returns all valid groups as described above. Provide the algorithm and code to compute the output.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More ZipHQ•More Software Engineer•ZipHQ Software Engineer•ZipHQ Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.