Quick 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.

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

  1. Find the merchants that match the source first; with max_hops = 2, the next step looks only for merchants that match one of those.
  2. Compare field values exactly as stored: "Acme", "acme" and "Acme " are three different company names.
  3. The source can match one of its own direct matches. Leave it out, and report each id once.

Merchants Linked to a Source Within Two Hops

mediumStripeSoftware Engineer

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

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

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

Examples

Example 1
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)
Output
['m2', 'm3']
Notes
Source example 1: m2 shares the name (20) and m3 shares the email (30), while m4 and m5 share nothing with m1.
Example 2
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)
Output
['m3']
Notes
Source example 2: m2 scores exactly 20, which does not exceed threshold 20, and m3 scores 30.

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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def linked_merchants(merchants: list[list[str]], source_id: str, scores: list[int], threshold: int, max_hops: int) -> list[str]:
Sample call
linked_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']], 'm1', [20, 30, 50], 0, 1)
Language
Case 1
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
['m2', 'm3']
Case 2
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
['m3']

Browse More Questions

Show the approach

Approach

Find the source record. Compare it with every other merchant, add the scores of the fields whose strings are exactly equal, and keep the merchant when that sum is strictly greater than threshold. These direct matches are the max_hops = 1 result.

For max_hops = 2, whether two merchants match depends only on which fields they share. Of the seven nonempty subsets of {name, email, phone}, keep those whose score total exceeds threshold. Two merchants match exactly when they agree on every field of at least one kept subset. If they match, the subset of all their equal fields is kept. If they agree on a kept subset S, their equal fields include S, and since no score is negative the match score is at least the total of S. So for each kept subset, a hash set stores the values of every direct match on that subset (JavaScript, Java and C++ join the strings with length prefixes, so two different value tuples never produce the same string). Every merchant that is neither the source nor a direct match is linked when its values on some kept subset are in that subset's set. Such a merchant always differs from the direct match it agrees with, so no merchant is matched with itself.

Ids are collected in a set, so a merchant reached through several direct matches appears once, and the source is skipped in both passes. The ids are sorted by character code, which reproduces Python string order in all four languages.

Edge cases: a single merchant or an empty direct-match list gives an empty result; a field with score 0 adds nothing, so with threshold 0 an equal zero-score field alone never matches; with threshold 3000 no pair matches because the largest possible match score is 3000. Merchants three hops out are never added because the second pass compares only against direct matches.

Time complexity:
O(n log n)
Space complexity:
O(n)