Count Ways to Assign Couriers to Orders Within Range
Last updated: November 29, 2026
Quick Overview
There are n couriers and n orders (n up to 12) and a matrix ok[i][j] saying whether courier i is close enough to take order j. Count the ways to assign every courier exactly one distinct order using only allowed pairs.
Uber
November 29, 20260
0
0 solved
There are n couriers and n orders (n up to 12) and a matrix ok[i][j] saying whether courier i is close enough to take order j. Count the ways to assign every courier exactly one distinct order using only allowed pairs.
An Uber onsite question that starts as plain backtracking and is upgraded with memoization over a bitmask, a common follow-up for dispatch-style matching problems. Interviewers check whether you spot repeated subproblems.
What the Interviewer Expects
- Backtracks courier by courier over unused allowed orders
- Represents used orders with a bitmask
- Notices that the state is only the mask, since the courier index is its popcount
- Memoizes to reach O(n * 2^n)
- Explains why plain backtracking can be O(n!)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Find the assignment that minimizes total pickup distance.
- What if there are more orders than couriers?
- Why does this become a bipartite matching problem when n is large and you need only one assignment?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
n x nboolean matrixok. - Output: number of perfect assignments.
- n is at most 12, so states are fine but may not be....
High-Level Design
Assign couriers in index order. Courier i chooses any order j with ok[i][j] that is not yet used, recorded as a bitmask. The number of set bits in the mask equals the index of the next courier, so t...