KatInterview

Check Whether Two Custom Dice Give a Flat Sum Distribution

Arrays & HashingAuto-graded · 24 test cases
MediumAuto-gradedArrays & Hashingcountinghash-mapenumeration

Last updated

Decide whether two lists of die-face labels make every total from 1 to 12 appear exactly three times across all 36 face pairings, using pair counting.

Two six-sided dice are rolled together. Each die is described by a list holding the integer written on each of its faces. Labels may repeat, and they may be zero or negative.

Every face of each die is equally likely, so there are 36 equally likely (face of die X, face of die Y) pairings. Each pairing produces a total: the label on X plus the label on Y.

The pair of dice is flat if every total from 1 through 12 is produced by exactly 3 of the 36 pairings. That makes each of those totals equally likely, with probability 1/12, and no other total can occur.

Write a function that takes the two label lists and returns True if the pair is flat, and False otherwise.

Details

  • Count pairings, not distinct label values. Two faces with the same label are separate faces and count separately.
  • A die must have exactly six faces. If either list does not have length 6, return False.
  • The order of the faces within a list does not matter.
  • Return a Python bool.

Constraints

  • Each list holds integers. The labels can be zero, negative, or repeated.
  • Lists may have any length, including empty, but only length exactly 6 can give True.
  • Label magnitudes are small enough that sums fit in ordinary integers.
  • You only need to check the given pair. You do not need to find other label sets.

Examples

Example 1

Input: is_flat_sum_pair([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])

Output: False

Example 2

Input: is_flat_sum_pair([0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6])

Output: False

Example 3

Input: is_flat_sum_pair([1, 1, 1, 4, 5, 6], [1, 2, 3, 4, 5, 6])

Output: False

Example 4

Input: is_flat_sum_pair([1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6])

Output: False

Hints

Hint 1

Six faces times six faces gives 36 pairings. If 12 totals share them equally, how many pairings must produce each total?

Hint 2

Build a frequency table of all 36 sums, using two nested loops over the faces and a counter keyed by the sum.

Hint 3

Check each total from 1 to 12 for a count of exactly 3. Once those 12 counts are 3 each, they use up all 36 pairings, so no other total can appear. Remember to reject lists whose length is not 6.