Find flush and straight groups in cards
Problem Description
You are given an array of strings, cards, where each string represents a distinct playing card. Each card string follows the format <rank><suit>:
rankis an integer from 1 to 13 (e.g.,1,10,13). Ranks are guaranteed to be written without leading zeros.suitis a single uppercase letter from the set{C, D, H, S}(Clubs, Diamonds, Hearts, Spades).
Valid Card Groups
A valid group of cards is defined as any set of cards that satisfies either of the following conditions:
-
Same-suit Consecutive Run (Straight Flush-style):
- All cards in the group must share the same suit.
- Their ranks, when sorted, must form a sequence of consecutive integers with a step of 1.
- The group size must be at least 3.
- Important Note: For each maximal same-suit consecutive run of length
L ≥ 3(meaning it cannot be extended on either side within that suit), you must output every contiguous sub-run whose length is between 3 andL, inclusive.- Example: If for suit
Cyou have ranks[1, 2, 3, 4, 5, 6], you must output all of these runs (represented by ranks only for brevity):- 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]
- Length 3:
- Example: If for suit
-
Same-rank Set (Three-of-a-kind-style):
- All cards in the group must share the same rank (suits can be different).
- The group size must be at least 3.
- Important Note: For each rank that appears
k ≥ 3times (with any suits), you should output one group containing all cards of that rank.
Overlapping Groups
If a card group satisfies both properties (e.g., three cards of the same suit and in consecutive ranks), it should still be output only once as a single group.
Output Format
The result should be a collection of all such valid groups, where each group is represented as a list of card strings. The order of groups in the overall output and the order of cards within each individual 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"]
One possible output:
[["1C", "2C", "3C"], ["1C", "1H", "1D"]]
Task
Implement a function findValidGroups(cards) that takes the cards array as input and returns all valid groups as described. You should provide the algorithm and code to compute the output.
AI interview prep
Core dilemma
The central engineering conflict is efficiently generating all required card groups, including numerous sub-runs, and then robustly deduplicating them to ensure each unique valid group is output exactly once, despite potential overlaps in card content and group type.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find flush and straight groups in cards
Problem Description
You are given an array of strings, cards, where each string represents a distinct playing card. Each card string follows the format <rank><suit>:
rankis an integer from 1 to 13 (e.g.,1,10,13). Ranks are guaranteed to be written without leading zeros.suitis a single uppercase letter from the set{C, D, H, S}(Clubs, Diamonds, Hearts, Spades).
Valid Card Groups
A valid group of cards is defined as any set of cards that satisfies either of the following conditions:
-
Same-suit Consecutive Run (Straight Flush-style):
- All cards in the group must share the same suit.
- Their ranks, when sorted, must form a sequence of consecutive integers with a step of 1.
- The group size must be at least 3.
- Important Note: For each maximal same-suit consecutive run of length
L ≥ 3(meaning it cannot be extended on either side within that suit), you must output every contiguous sub-run whose length is between 3 andL, inclusive.- Example: If for suit
Cyou have ranks[1, 2, 3, 4, 5, 6], you must output all of these runs (represented by ranks only for brevity):- 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]
- Length 3:
- Example: If for suit
-
Same-rank Set (Three-of-a-kind-style):
- All cards in the group must share the same rank (suits can be different).
- The group size must be at least 3.
- Important Note: For each rank that appears
k ≥ 3times (with any suits), you should output one group containing all cards of that rank.
Overlapping Groups
If a card group satisfies both properties (e.g., three cards of the same suit and in consecutive ranks), it should still be output only once as a single group.
Output Format
The result should be a collection of all such valid groups, where each group is represented as a list of card strings. The order of groups in the overall output and the order of cards within each individual 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"]
One possible output:
[["1C", "2C", "3C"], ["1C", "1H", "1D"]]
Task
Implement a function findValidGroups(cards) that takes the cards array as input and returns all valid groups as described. You should provide the algorithm and code to compute the output.