InterviewDB Experience

Card Set Detection: Determine if a Hand of Cards Forms Valid Sets Under Custom Rules

Interview Experience

Problem

You are given a hand of cards. Each card has three attributes: color (red/green/blue), shape (oval/diamond/squiggle), and count (1/2/3). A valid "set" is any group of 3 cards where, for each attribute, the values across the 3 cards are either all the same or all different.

Given a list of cards, find all valid sets in the hand.

python
from dataclasses import dataclass

@dataclass
class Card:
    color: str
    shape: str
    count: int

def find_sets(hand: list[Card]) -> list[tuple[Card, Card, Card]]:
    ...
Cards: 
  A=(red, oval, 1)
  B=(green, oval, 2)
  C=(blue, oval, 3)
  D=(red, diamond, 2)

(A,B,C): color=all diff, shape=all same, count=all diff -> VALID SET
(A,B,D): color=all diff, shape=2 same 1 diff -> INVALID

**Output**: [(A,B,C)]

Follow-ups

  1. Your brute-force is O(n^3). For a 12-card layout, how many triples are there? Is O(n^3) acceptable in practice?
  2. How would you detect if a hand has NO valid set (used in the game to trigger a redeal)?
  3. Add a 4th attribute fill (solid/striped/empty). How does this change your validity check?
  4. Given that each attribute has 3 values and there are 4 attributes, what is the maximum deck size, and what is the expected number of sets in a random 12-card deal?

Full Details

Problem

You are given a hand of cards. Each card has three attributes: color (red/green/blue), shape (oval/diamond/squiggle), and count (1/2/3). A valid "set" is any group of 3 cards where, for each attribute, the values across the 3 cards are either all the same or all different.

Given a list of cards, find all valid sets in the hand.

python
from dataclasses import dataclass

@dataclass
class Card:
    color: str
    shape: str
    count: int

def find_sets(hand: list[Card]) -> list[tuple[Card, Card, Card]]:
    ...
Cards: 
  A=(red, oval, 1)
  B=(green, oval, 2)
  C=(blue, oval, 3)
  D=(red, diamond, 2)

(A,B,C): color=all diff, shape=all same, count=all diff -> VALID SET
(A,B,D): color=all diff, shape=2 same 1 diff -> INVALID

**Output**: [(A,B,C)]

Follow-ups

  1. Your brute-force is O(n^3). For a 12-card layout, how many triples are there? Is O(n^3) acceptable in practice?
  2. How would you detect if a hand has NO valid set (used in the game to trigger a redeal)?
  3. Add a 4th attribute fill (solid/striped/empty). How does this change your validity check?
  4. Given that each attribute has 3 values and there are 4 attributes, what is the maximum deck size, and what is the expected number of sets in a random 12-card deal?

About This Question

This is a candidate experience report from a jump trading interview during the oa round.

It covers the following topics: Coding, Oop, Oa .