Count Distinct Preferred-Hat Assignments

Quick Overview

Count Distinct Preferred-Hat Assignments. There are n people and hat labels1..40. Person i must receive one label in hats[i], and no label may be assigned to two people.

Count Distinct Preferred-Hat Assignments

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem There are n people and hat labels 1..40. Person i must receive one label in hats[i], and no label may be assigned to two people. Count the assignments to the labeled people, modulo 1000000007. ```cpp class Solution { public: int numberWays(vector<vector<int>>& hats); }; ``` ### Examples **Example 1:** ```text Input: hats = [[1,2],[2,3]] Output: 3 Explanation: The ordered assignments are(1,2),(1,3),and(2,3). ``` **Example 2:** ```text Input: hats = [[7],[7]] Output: 0 Explanation: Both people require the same single hat, which cannot be shared. ``` ### Constraints - n equals hats.length, with `1 <= n <= 10`. - Each person lists 1 to 40 unique labels, all in `[1,40]`. ### Contract considerations Only the count modulo the stated modulus is returned, not a selected assignment. ```hint Read the boundary condition Respect each person's preferences and prohibit sharing a hat label. Distinct people remain distinct when their preference lists agree. ``` ### Boundary reminders Only the count modulo the stated modulus is returned, not a selected assignment.

Overview: Count Distinct Preferred-Hat Assignments. There are n people and hat labels1..40. Person i must receive one label in hats[i], and no label may be assigned to two people.

Roblox logo
Roblox
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

There are n people and hat labels 1..40. Person i must receive one label in hats[i], and no label may be assigned to two people. Count the assignments to the labeled people, modulo 1000000007.

class Solution {
public:
    int numberWays(vector<vector<int>>& hats);
};

Examples

Example 1:

Input: hats = [[1,2],[2,3]]
Output: 3
Explanation: The ordered assignments are(1,2),(1,3),and(2,3).

Example 2:

Input: hats = [[7],[7]]
Output: 0
Explanation: Both people require the same single hat, which cannot be shared.

Constraints

  • n equals hats.length, with 1 <= n <= 10 .
  • Each person lists 1 to 40 unique labels, all in [1,40] .

Contract considerations

Only the count modulo the stated modulus is returned, not a selected assignment.

Boundary reminders

Only the count modulo the stated modulus is returned, not a selected assignment.