Lead Assignment Rules
MediumBinary Search · SalesForce
A CRM routes new leads to sales owners using an ordered list of assignment rules.
- Each lead is
[lead_id, region, company_size]. - Each rule is
[rule_region, min_size, owner].rulesis in priority order:rules[0]has the highest priority.
A rule matches a lead when both hold:
rule_regionequals the lead'sregionexactly (case-sensitive), orrule_regionis"*", which matches every region.- The lead's
company_size >= min_size.
Each lead is assigned to the owner of the first (highest-priority) matching rule, or to "UNASSIGNED" if no rule matches. Return a list of [lead_id, owner] pairs in the same order as leads.
Both lists can hold up to 10^5 entries, so checking every rule for every lead is too slow in the worst case.
Example 1
- Input:
- leads = [["L1", "US", 250], ["L2", "EU", 40], ["L3", "US", 30], ["L4", "APAC", 5]], rules = [["US", 500, "enterprise"], ["*", 200, "midmarket"], ["US", 0, "us_smb"], ["EU", 50, "eu_team"]]
- Output:
- [["L1", "midmarket"], ["L2", "UNASSIGNED"], ["L3", "us_smb"], ["L4", "UNASSIGNED"]]
- Explanation
- L1 (US, 250) fails rule 0 (needs 500) and matches rule 1, the catch-all for size 200+. L2 (EU, 40) is below both 200 and 50. L3 (US, 30) first matches rule 2. L4 (APAC, 5) is too small for the catch-all and no APAC rule exists.
Example 2
- Input:
- leads = [["A", "EU", 10]], rules = [["EU", 20, "x"], ["EU", 5, "y"], ["*", 0, "z"]]
- Output:
- [["A", "y"]]
- Explanation
- Rule 0 needs size 20. Rule 1 matches, and because it comes before the catch-all rule 2,
ywins.
Example 3
- Input:
- leads = [["B", "LATAM", 0]], rules = []
- Output:
- [["B", "UNASSIGNED"]]
- Explanation
- With no rules at all, every lead is unassigned.
Constraints
0 <= leads.length, rules.length <= 10^50 <= company_size, min_size <= 10^9- Lead regions are non-empty strings other than
"*" - Lead ids are unique