Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Graphs

Place as many applicants as possible

Medium · Target 25 minutes · Graphs

There are N applicants and M jobs. matrix[i][j] is 1 if applicant i is qualified for job j and 0 otherwise. Each job can be given to at most one applicant and each applicant can take at most one job. Return the largest number of applicants who can be given a job they are qualified for.

Examples

InputOutputWhy
matrix = [[1,1,0,0],[1,0,1,0],[0,1,1,0],[0,0,1,1]]4For example 0→job 1, 1→job 0, 2→job 2, 3→job 3.
matrix = [[1,1,1],[1,0,0],[1,0,0]]2Applicants 1 and 2 both want only job 0; one of them must miss out.
matrix = [[1],[1],[1]]1One job.

Constraints

  • 1 ≤ N, M ≤ 200
  • matrix[i][j] is 0 or 1

Hints

Hint 1

Give each applicant a job in turn. If a job they want is taken, ask whether its holder could move to another job they qualify for.

Hint 2

That question is recursive: the holder may need to displace someone else. Track which jobs have been tried in this round so the search ends.

Hint 3

Each applicant who finds such a chain adds one to the matching. The chain is an augmenting path.

Where people lose marks

  • Greedily giving each applicant the first free job and never reassigning: example 1 then places only three.
  • Sharing one tried set across all applicants instead of resetting it for each.
  • Assuming N = M: the matrix may be rectangular.
Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.