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.