KatInterview

Residue Distribution of Dice Sums and 2×2 Stochastic Determinants

Math & Bit ManipulationAuto-graded · 19 test cases
MediumAuto-gradedMath & Bit Manipulationprobabilitylinear-algebramodular-arithmetic

Last updated

Tests whether a candidate can compute modular probability distributions over two-dice outcomes and derive the determinant formula for a constrained matrix class.

Part 1: Two Dice, Sum Modulo 3

You roll two independent fair six-sided dice. Each face shows an integer from 1 to 6 with equal probability. Let T be the total of the two face values.

Compute the full probability distribution of T mod 3 — that is, find P(T mod 3 = k) for each k in {0, 1, 2}.

Return the three probabilities as a list indexed by residue: index 0 holds P(T mod 3 = 0), index 1 holds P(T mod 3 = 1), and index 2 holds P(T mod 3 = 2).


Part 2: Eigenvalue and Determinant of a Balanced Square Matrix

A square matrix is called balanced if every entry is non-negative and every row and every column sums to exactly 1.

Part 2a — Fixed eigenvalue

Show that 1 is always an eigenvalue of any balanced matrix. Your function should return True for the key "eigenvalue_one" to indicate this fact holds unconditionally.

Part 2b — Determinant of a 2×2 balanced matrix

Every 2×2 balanced matrix is completely determined by its top-left entry a, where 0 ≤ a ≤ 1. The matrix must take the form:

M = [[a,   1-a],
     [1-a,   a]]

Given a, compute the determinant of M and return it under the key "determinant".


What to return

Return a dictionary with exactly these three keys:

KeyTypeMeaning
"dice_mod_3"list[float][P(T mod 3=0), P(T mod 3=1), P(T mod 3=2)]
"eigenvalue_one"boolAlways True
"determinant"floatdet(M) for the given a

Constraints

  • The dice outcomes are always a standard pair of fair six-sided dice (faces 1–6); no input controls this.
  • a is a float. The function must handle any real value of a; validation of a ∈ [0, 1] is not required.
  • Each entry in "dice_mod_3" must equal exactly 1/3 (as a Python float).
  • "eigenvalue_one" must always be True.
  • "determinant" must equal 2*a - 1 to floating-point precision.

Examples

Example 1

Input: dice_and_balanced_matrix(0.5)

Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': 0}

Example 2

Input: dice_and_balanced_matrix(0)

Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': -1}

Example 3

Input: dice_and_balanced_matrix(1)

Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': 1}

Example 4

Input: dice_and_balanced_matrix(0.25)

Output: {'dice_mod_3': [0.3333333333333333, 0.3333333333333333, 0.3333333333333333], 'eigenvalue_one': True, 'determinant': -0.5}

Hints

Hint 1

For Part 1, think about what values a single fair die can take modulo 3. Each residue class {0, 1, 2} is hit by exactly two faces. What does that say about the distribution of one die mod 3, and therefore about the sum of two independent dice mod 3?

Hint 2

For Part 2a, try multiplying any balanced matrix by the all-ones column vector. What does each row sum tell you about the result?

Hint 3

For Part 2b, write out all four constraints (two row sums, two column sums) in terms of a, b, c, d. You will find that b, c, and d are each fully determined by a alone, which gives you a one-parameter family. Then compute ad - bc.