Design poker-like hand comparison with custom ranking
Problem Description
Design an object-oriented solution for comparing two players' hands in a card game, similar to poker. Each hand consists of a fixed number of cards (assume 5 cards per hand unless specified otherwise) and is associated with a specific hand type (e.g., three-of-a-kind, pair, full house).
Requirements
- Hand Comparison: Implement a mechanism to compare two given hands and determine which one is stronger, or if they result in a tie.
- Type-Based Ranking: If two hands have different types, the winner is determined by the predefined ranking of their respective hand types (e.g., a Full House beats Three-of-a-Kind).
- Tie-Breaking within Same Type: If two hands share the same type, ties must be broken by comparing the relevant card ranks in order. For example, in a Three-of-a-Kind,
8-8-8-3-xshould beat7-7-7-3-xbecause the triplet of 8s is higher than the triplet of 7s. Further comparisons of "kicker" cards should be performed if necessary to resolve ties completely. - Extensibility:
- Provide an
add_type(type_id: string, matches_function: function)method to register new hand types and define the logic for detecting them within a hand. - The primary
evaluatefunction must accept anevaluation_orders: array<string>parameter. This array specifies the precedence of hand types, allowing for different game rulesets or custom rankings.
- Provide an
Deliverables
- Sketch the high-level class and module design, outlining key classes, interfaces, and their responsibilities.
- Explain the interaction between the
add_type(...)method and theevaluation_ordersparameter. - Detail the comparison logic, including how tie-break rules are applied and how a hand's "comparison key" might be represented for efficient evaluation.
Assumptions
- Card ranks are totally ordered (e.g., 2, 3, ..., King, Ace).
- Suits exist and can be used if relevant for specific hand types (e.g., Flush, Straight Flush), but the overall design should remain extensible.
AI interview prep
Core dilemma
Balancing a highly extensible, pluggable system for defining and ranking custom card hand types with efficient and robust comparison logic, especially for complex tie-breaking rules.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design poker-like hand comparison with custom ranking
Problem Description
Design an object-oriented solution for comparing two players' hands in a card game, similar to poker. Each hand consists of a fixed number of cards (assume 5 cards per hand unless specified otherwise) and is associated with a specific hand type (e.g., three-of-a-kind, pair, full house).
Requirements
- Hand Comparison: Implement a mechanism to compare two given hands and determine which one is stronger, or if they result in a tie.
- Type-Based Ranking: If two hands have different types, the winner is determined by the predefined ranking of their respective hand types (e.g., a Full House beats Three-of-a-Kind).
- Tie-Breaking within Same Type: If two hands share the same type, ties must be broken by comparing the relevant card ranks in order. For example, in a Three-of-a-Kind,
8-8-8-3-xshould beat7-7-7-3-xbecause the triplet of 8s is higher than the triplet of 7s. Further comparisons of "kicker" cards should be performed if necessary to resolve ties completely. - Extensibility:
- Provide an
add_type(type_id: string, matches_function: function)method to register new hand types and define the logic for detecting them within a hand. - The primary
evaluatefunction must accept anevaluation_orders: array<string>parameter. This array specifies the precedence of hand types, allowing for different game rulesets or custom rankings.
- Provide an
Deliverables
- Sketch the high-level class and module design, outlining key classes, interfaces, and their responsibilities.
- Explain the interaction between the
add_type(...)method and theevaluation_ordersparameter. - Detail the comparison logic, including how tie-break rules are applied and how a hand's "comparison key" might be represented for efficient evaluation.
Assumptions
- Card ranks are totally ordered (e.g., 2, 3, ..., King, Ace).
- Suits exist and can be used if relevant for specific hand types (e.g., Flush, Straight Flush), but the overall design should remain extensible.