Exactly K Preferred Role Assignments
Updated
Problem statement
There are n distinct players and n distinct roles. Every player must receive exactly one role, and each role must be assigned to exactly one player.
For this exercise, assume each player has a different preferred role, so the preferences form a one-to-one mapping. Relabel players and roles so player i prefers role i.
Return the number of assignments in which exactly k players receive their preferred role. Count assignments, rather than their probability.
For this exercise, assume n and k generalize the five-player, two-match case in the first example. When n = 0 and k = 0, the empty assignment counts as one.
Complete countPreferredAssignments(n, k) and return the exact count as a signed 64-bit integer.
Function
countPreferredAssignments(n: int, k: int) → longExamples
Example 1
n = 5k = 2return = 20Choose the two matching players in C(5,2) = 10 ways. The other three players must all miss their preferred roles; their roles can be assigned in two cyclic arrangements. The total is 10 * 2 = 20.
Example 2
n = 3k = 1return = 3Choose one of the three players to receive their preferred role. The two remaining players must swap roles, giving exactly three assignments.
Example 3
n = 4k = 3return = 0If three players receive their preferred roles, only the fourth player and their own role remain. That player also matches, so exactly three matches is impossible.
Example 4
n = 0k = 0return = 1There is one empty assignment, and it has zero matches.
Constraints
- For this exercise, assume
0 <= n <= 20and0 <= k <= n. - All players and roles are distinct; every preferred role belongs to exactly one player.
- The exact count and intermediate values in the presented solutions fit in a signed 64-bit integer:
20! = 2432902008176640000.
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Chain of CommandOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
Similar questions at other companies
- Count Distinct Arrays After Decrement OperationsBloomberg LP · OABloomberg LP interview questions
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Get Triplet CountIBM · OAIBM interview questions
- Count Fancy NumbersHudson River Trading · OAHudson River Trading interview questions
- Count Numbers with the Same Set-Bit CountMicrosoft · OAMicrosoft interview questions
- Count Reverse-Digit PairsVisa · OAVisa interview questions
- Get Triplet CountPoint72 · OAPoint72 interview questions
- Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
- Color a Grid with No Monochromatic Row or ColumnIntuit · OAIntuit interview questions
- Product of Subset MaximaArcesium · OAArcesium interview questions