KatInterview

Collect All Valid Triples in a Four-Attribute Card Game

Arrays & HashingAuto-graded · 20 test cases
HardAuto-gradedArrays & Hashinghashingcombinatoricsmath

Last updated

Tests recognition of a combinatorial validity rule across four ternary attributes and the insight that any two cards uniquely determine the third, reducing O(n³) brute force to O(n²).

Problem

You are working with a deck where every card is described by exactly four properties: hue, figure, count, and fill. Each property takes one of three possible values, encoded as 0, 1, or 2. A card is therefore represented as a list of four integers.

Three cards form a matching triple when, for each of the four properties independently, the three cards' values are either all identical or all distinct. If even one property has two cards sharing a value while the third differs, the triple does not match.

Given a list of cards, return the indices of every matching triple.

Task

Implement the function:

def find_matching_triples(cards: list[list[int]]) -> list[list[int]]:

cards[i] is [hue, figure, count, fill] for card i.

Rules

  • Three indices i < j < k form a matching triple when, for every property index p in 0..3, the multiset {cards[i][p], cards[j][p], cards[k][p]} contains either one distinct value or three distinct values.
  • Return each matching triple as [i, j, k] with i < j < k.
  • The returned list must be sorted in ascending lexicographic order: first by i, then by j, then by k.
  • Return an empty list if no matching triple exists, including when fewer than three cards are provided.

Constraints

  • 1 <= len(cards) <= 81
  • len(cards[i]) == 4 for every i, and each element is 0, 1, or 2.
  • All cards in the input are distinct from one another.

Examples

Example 1

Input: find_matching_triples([[0, 1, 2, 0], [0, 2, 2, 1], [0, 0, 2, 2]])

Output: [[0, 1, 2]]

Example 2

Input: find_matching_triples([[0, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 2]])

Output: []

Example 3

Input: find_matching_triples([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [0, 1, 2, 0], [0, 2, 1, 0], [1, 0, 0, 0]])

Output: [[0, 1, 2], [0, 3, 4]]

Hints

Hint 1

For a single property, the only failing configuration is exactly two cards sharing a value while the third differs — equivalently, a property fails when the sum of its three values is not divisible by 3. Use this to check any triple cheaply.

Hint 2

Once you fix two cards, there is exactly one value for each property that could complete a matching triple: if the two cards agree on a property, the third must match them; if they differ, the third must be the one remaining value (since 0+1+2=3, that value is 3 minus the sum of the two you have). This means any two cards determine their unique potential partner.

Hint 3

Build a lookup table from card tuple to index before iterating over pairs. For each pair (i, j), compute the required partner card, check the table, and only record a triple when the partner's index k satisfies k > j. This last condition eliminates duplicates and keeps indices ordered without any extra bookkeeping.