← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Uber SWE interview with a bitmask DP problem on hat assignments. Pretty niche problem type and I hadn't seen it before, so it was a rough ride.

Questions Asked (1)

Q1

Given n people (up to 10) and 40 hat types, where each person specifies which hats they're willing to wear, count the number of ways to assign exactly one hat per person such that no two people share a hat. Return the result modulo 10^9 + 7.

Algorithms & Data Structures
Author's notes

I knew it was some kind of DP but spent way too long thinking about it person-by-person before realizing you should iterate over hats instead, since there are only 40 hats but up to 10 people.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as counting perfect matchings in a bipartite graph where people are on one side and hats on the other, with edges representing allowed assignments. Use DP over subsets of people (since n ≤ 10) to count assignments, iterating over hats and updating the set of assigned people. Apply modulo 10^9+7 to handle large numbers.

Pro tip: Mention that the small n (≤10) makes bitmask DP feasible, and that the 40 hat types are handled by iterating over hats and updating the DP state; this avoids exponential blowup in hats. Also, clarify that each hat can be used at most once, so the DP state must track which people have been assigned.

1. Understand the problem and constraints

Restate the problem: assign exactly one hat to each person from their allowed set, with no two people sharing a hat. Note n ≤ 10 and 40 hat types, so a bitmask DP over people is efficient.

2. Define DP state and transition

Let dp[mask] be the number of ways to assign hats to the subset of people represented by mask. Initialize dp[0] = 1. For each hat, update dp by considering assigning it to any unassigned person who allows it, adding dp[mask] to dp[mask | (1<<i)].

3. Iterate over hats and update DP

Process hats one by one. For each hat, iterate over all masks from (1<<n)-1 down to 0 to avoid reusing the same hat in one iteration. For each mask, if person i is not in mask and allows the hat, add dp[mask] to dp[mask | (1<<i)] modulo 10^9+7.

4. Return the result

After processing all hats, dp[(1<<n)-1] gives the number of valid assignments. Return it modulo 10^9+7.

Key Points to Mention

  • Bitmask DP over subsets of people (n ≤ 10) to track assigned individuals.
  • Iterate over hats to ensure each hat is used at most once.
  • Modulo 10^9+7 to prevent integer overflow.
  • Time complexity O(40 * 2^n * n) and space O(2^n).
  • Alternative: inclusion-exclusion or backtracking with pruning, but DP is more efficient.
  • Clarify that each person must get exactly one hat and hats cannot be reused.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.