ORIGINAL CODING QUESTION
Skills shared by two sorted lists
A candidate skill list and a job requirement list are both given as skill codes in nondecreasing order, and codes may repeat. Match codes one-to-one: each occurrence can be matched with at most one equal occurrence in the other list. Return the largest number of matches.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n nondecreasing codes, then m, then m nondecreasing codes. Print the match count.
Constraints
1 ≤ n, m ≤ 200; -1000 ≤ code ≤ 1000; both lists are sorted in nondecreasing order.
Example 1
Input
5 1 2 2 4 7 4 2 2 3 7
Output
3
Both copies of 2 and the 7 can be matched.
Example 2
Input
2 1 3 2 2 4
Output
0
The lists share no code.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Because both lists are sorted, you never need to look backwards.
Hint 2
Keep one index in each list and compare the two current codes.
Hint 3
If the codes are equal count a match and advance both; otherwise advance only the index pointing at the smaller code.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →