FastPrepExactly K Preferred Role Assignments
Problems/Exactly K Preferred Role Assignments

Exactly K Preferred Role Assignments

IMC Trading logoIMC Trading● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an IMC Trading new grad online assessment. The reported question counts assignments with exactly two preferred-role matches among five players. Our counting task is about 95% the same. This version works for any counts, and states a one-to-one preference mapping and what to return for an empty assignment.

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) → long

Examples

Example 1

n = 5k = 2return = 20

Choose 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 = 3

Choose 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 = 0

If 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 = 1

There is one empty assignment, and it has zero matches.

Constraints

  • For this exercise, assume 0 <= n <= 20 and 0 <= 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

Interview reports that mention this question

  • IMC Trading OA: What It Tests (2026)

IMC Trading interview experiences

  • IMC Trading SWE OA and Video: 18 Experiences (Part 3 of 3)
  • IMC Trading SWE OA Questions: 22 Experiences (Part 2 of 3)
  • IMC SWE New Grad OA and Interviews: 58 Experiences (Part 1 of 3)
All IMC Trading interview questionsSee IMC Trading hiring insights
Saved
class Solution {
    public long countPreferredAssignments(int n, int k) {
        // Write your solution here.
    }
}
Checking account…