Find Merchants Linked to a Source by Weighted Field Matches Within Two Hops
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
A payments platform wants to find merchant accounts that are likely run by the same business as a given source merchant. Each merchant record has an id, a company name, an email address and a phone number.
The interview built the problem up in three steps:
1. Two merchants match when at least one of the three fields is equal.
2. Each field carries a score, for example 20 for the company name, 30 for the email and 50 for the phone. Two merchants match only when the total score of their equal fields exceeds a threshold.
3. Links extend to two hops: a merchant that matches any direct match of the source is linked too.
Implement one function that covers all three steps. Given the merchant records, the id of the source merchant, the three field scores, a threshold and a hop limit of 1 or 2, return the ids of every merchant linked to the source.
### Function Signature
```python
def linked_merchants(merchants: list[list[str]], source_id: str, scores: list[int], threshold: int, max_hops: int) -> list[str]:
```
### Rules
- Each record is `[merchant_id, company_name, email, phone]`, and `scores` is `[name_score, email_score, phone_score]`.
- For two different merchants, the match score is the sum of the scores of the fields whose values are exactly equal: case-sensitive, with no trimming or other normalization. The two merchants match when the match score is strictly greater than `threshold`.
- With every score positive and `threshold = 0`, a single equal field is enough for a match, which is step 1.
- With `max_hops = 1`, the result is every merchant that matches the source.
- With `max_hops = 2`, the result also contains every merchant that matches at least one merchant from the `max_hops = 1` result.
- The source merchant is never part of the result, even when another linked merchant matches it.
- Each id appears at most once. Return the ids sorted in ascending lexicographic order, or an empty list when nothing is linked.
### Constraints
- `1 <= len(merchants) <= 2000`
- Merchant ids are unique nonempty strings of at most 20 characters, and `source_id` is one of them.
- Every company name, email and phone is a nonempty string of at most 50 characters.
- `len(scores) == 3` and `0 <= scores[i] <= 1000`
- `0 <= threshold <= 3000`
- `max_hops` is `1` or `2`
### Examples
All three examples use these merchants:
```text
merchants = [
["m1", "Acme", "a@acme.com", "555-0100"],
["m2", "Acme", "billing@acme.com", "555-0199"],
["m3", "Beta Co", "a@acme.com", "555-0123"],
["m4", "Gamma", "g@gamma.com", "555-0199"],
["m5", "Delta", "d@delta.com", "555-0500"]
]
```
#### Example 1
```text
Input: merchants (above), source_id = "m1", scores = [20, 30, 50], threshold = 0, max_hops = 1
Output: ["m2", "m3"]
```
`m2` shares the company name with `m1` (score 20), and `m3` shares the email (score 30). `m4` and `m5` share no field with `m1`.
#### Example 2
```text
Input: merchants (above), source_id = "m1", scores = [20, 30, 50], threshold = 20, max_hops = 1
Output: ["m3"]
```
`m2` scores exactly 20 against `m1`, which does not exceed the threshold.
#### Example 3
```text
Input: merchants (above), source_id = "m1", scores = [20, 30, 50], threshold = 0, max_hops = 2
Output: ["m2", "m3", "m4"]
```
`m4` shares the phone `555-0199` with `m2` (score 50), so it is linked through `m2`. `m3` matches only `m1`, and `m5` matches no one.
Overview: Given merchant records with a company name, email and phone, find every merchant linked to a source merchant, where equal fields add weighted scores that must exceed a threshold and links can extend to two hops. It tests precise matching rules, deduplication and deterministic sorted output.
Read the full Stripe Software Engineer interview experience this question came from
A payments platform wants to find merchant accounts that are likely run by the same business as a given source merchant. Each merchant record has an id, a company name, an email address and a phone number.
Given the merchant records, the id of the source merchant, the three field scores, a threshold and a hop limit of 1 or 2, return the ids of every merchant linked to the source.
### Rules
- Each record is `[merchant_id, company_name, email, phone]`, and `scores` is `[name_score, email_score, phone_score]`.
- For two different merchants, the match score is the sum of the scores of the fields whose values are exactly equal: case-sensitive, with no trimming or other normalization. The two merchants match when the match score is strictly greater than `threshold`.
- With every score positive and `threshold = 0`, a single equal field is enough for a match.
- With `max_hops = 1`, the result is every merchant that matches the source.
- With `max_hops = 2`, the result also contains every merchant that matches at least one merchant from the `max_hops = 1` result.
- The source merchant is never part of the result, even when another linked merchant matches it.
- Each id appears at most once. Return the ids sorted in ascending lexicographic order, comparing character codes from left to right (so `"m10"` comes before `"m2"`), or an empty list when nothing is linked.
### Types
- Python and JavaScript: `merchants` is a list of four-string lists, `scores` is a list of three integers, and the result is a list of strings.
- Java: `String[][] merchants` and `int[] scores`, returning `java.util.List<String>`.
- C++: each record arrives as a `std::vector<std::string>` of four strings inside `const std::vector<std::vector<std::string>>& merchants`, `scores` is `const std::vector<int>&`, and the result is `std::vector<std::string>`.
- Every match score is at most 3000, so no value exceeds 2^31-1 and 32-bit integers are enough in every language.
### Example 1
```text
merchants = [
["m1", "Acme", "a@acme.com", "555-0100"],
["m2", "Acme", "billing@acme.com", "555-0199"],
["m3", "Beta Co", "a@acme.com", "555-0123"],
["m4", "Gamma", "g@gamma.com", "555-0199"],
["m5", "Delta", "d@delta.com", "555-0500"]
]
source_id = "m1", scores = [20, 30, 50], threshold = 0, max_hops = 1
Output: ["m2", "m3"]
```
`m2` shares the company name with `m1` (score 20), and `m3` shares the email (score 30). `m4` and `m5` share no field with `m1`.
### Example 2
```text
Same merchants, source_id = "m1", scores = [20, 30, 50], threshold = 0, max_hops = 2
Output: ["m2", "m3", "m4"]
```
`m4` shares the phone `555-0199` with `m2` (score 50), so it is linked through `m2`. `m3` matches only `m1`, and `m5` matches no one.
### Constraints
- `1 <= len(merchants) <= 2000`
- Each record holds exactly four strings.
- Merchant ids are unique nonempty strings of at most 20 characters, and `source_id` is one of them.
- Every company name, email and phone is a nonempty string of at most 50 characters.
- `len(scores) == 3` and `0 <= scores[i] <= 1000`
- `0 <= threshold <= 3000`
- `max_hops` is `1` or `2`
Constraints
- 1 <= len(merchants) <= 2000
- Each record holds exactly four strings: [merchant_id, company_name, email, phone]
- Merchant ids are unique nonempty strings of at most 20 characters, and source_id is one of them
- Every company name, email and phone is a nonempty string of at most 50 characters
- len(scores) == 3 and 0 <= scores[i] <= 1000
- 0 <= threshold <= 3000
- max_hops is 1 or 2
Examples
Input: ([['m1', 'Acme', 'a@acme.com', '555-0100'], ['m2', 'Acme', 'billing@acme.com', '555-0199'], ['m3', 'Beta Co', 'a@acme.com', '555-0123'], ['m4', 'Gamma', 'g@gamma.com', '555-0199'], ['m5', 'Delta', 'd@delta.com', '555-0500']], 'm1', [20, 30, 50], 0, 1)
Expected Output: ['m2', 'm3']
Explanation: Source example 1: m2 shares the name (20) and m3 shares the email (30), while m4 and m5 share nothing with m1.
Input: ([['m1', 'Acme', 'a@acme.com', '555-0100'], ['m2', 'Acme', 'billing@acme.com', '555-0199'], ['m3', 'Beta Co', 'a@acme.com', '555-0123'], ['m4', 'Gamma', 'g@gamma.com', '555-0199'], ['m5', 'Delta', 'd@delta.com', '555-0500']], 'm1', [20, 30, 50], 20, 1)
Expected Output: ['m3']
Explanation: Source example 2: m2 scores exactly 20, which does not exceed threshold 20, and m3 scores 30.
Hints
- Find the merchants that match the source first; with max_hops = 2, the next step looks only for merchants that match one of those.
- Compare field values exactly as stored: "Acme", "acme" and "Acme " are three different company names.
- The source can match one of its own direct matches. Leave it out, and report each id once.