FastPrepLinked Merchants Within Two Hops

Linked Merchants Within Two Hops

Stripe logoStripe● MediumFULLTIMENEW GRADINTERNPHONE SCREEN

Updated

Problem statement

Each merchant record is encoded as merchantId|email|merchantName. Merchant IDs are unique. Two distinct merchants have a direct link when their email values match exactly or their merchant-name values match exactly.

A direct edge has confidence 100 when both fields match and 50 when exactly one field matches. Starting from targetMerchantId, include every other merchant reachable by a path of one or two direct edges. The confidence of a path is the minimum confidence of its edges.

For each reachable merchant, keep the path with the highest confidence. When several paths have that confidence, keep the one with fewer hops. Emit the merchant only once as merchantId|hops|confidence. Sort the final strings by merchant ID in ascending lexicographic order.

Three-stage variant: weighted merchant attributes

A campus version uses nine merchants with unique IDs and three attributes: companyName, email, and phone. First, list merchants sharing at least one attribute with a merchant of interest. Next, use field confidence weights 50, 30, and 25 respectively and a cutoff, with 55 given as an example. Finally, include merchants linked directly or through one intermediate merchant. Stop at two edges; do not traverse the entire connected component.

Additional runnable rules

The main function remains linkedMerchantsWithinTwoHops with its two-field contract above. For the three-stage variant, implement the additional function relatedMerchantIds(String[][] merchants, String targetMerchantId, int threshold, int part). Its examples below describe a separate call.

For this exercise, assume each row is [id, companyName, email, phone]. Attribute comparisons are exact and case-sensitive. Interpret a pair's confidence as the sum of the listed weights for matching attributes.

  1. part = 1: return direct matches sharing any of the three attributes; ignore the threshold.
  2. part = 2: return direct matches whose additive confidence is greater than or equal to threshold.
  3. part = 3: form the same threshold-qualified direct edges, then return merchants at distance one or two from the target. Both edges of a two-edge path must qualify independently.

For this exercise, assume valid, nonempty printable-ASCII values, unique IDs, a target present in the input, 1 <= merchants.length <= 2000, field lengths at most 100, 0 <= threshold <= 106, and part in {1, 2, 3}. Exclude the target, remove duplicate results, and sort IDs in ascending lexicographic order.

Nine-record example

merchants = [
  ["A", "C0", "E0", "P0"],
  ["B", "C0", "E0", "P1"],
  ["C", "C0", "E1", "P1"],
  ["D", "C1", "E1", "P1"],
  ["E", "C0", "E0", "P0"],
  ["F", "CF", "EF", "PF"],
  ["G", "C0", "EG", "P0"],
  ["H", "CH", "E0", "P0"],
  ["I", "C0", "E1", "P9"]
]
targetMerchantId = "A"
threshold = 55
part 1: ["B", "C", "E", "G", "H", "I"]
part 2: ["B", "E", "G", "H"]
part 3: ["B", "C", "E", "G", "H"]

A-B scores 80, B-C scores 75, and C-D scores 55. So C qualifies at two edges, while D and I require three edges and are excluded from part 3. Direct one-field company matches score only 50 and do not meet the example cutoff. With threshold 0, even pairs sharing no attribute qualify; with threshold 106, no pair qualifies because the largest additive score is 105.

Implementation focus

Keep record lookup, pair scoring, and the two-step expansion in separate helpers. A map of merchant records supports the first two stages; for the third, expand only the target's direct neighbors, then stop. Modular code helps here.

Function

linkedMerchantsWithinTwoHops(merchants: String[], targetMerchantId: String) → String[]

Examples

Example 1

merchants = ["a|x@shop.com|North","b|x@shop.com|East","c|y@shop.com|East","d|x@shop.com|North","e|z@shop.com|West"]targetMerchantId = "a"return = ["b|1|50","c|2|50","d|1|100"]

b shares the email with a. c is two hops away through b. d shares both identity fields with a and therefore has confidence 100.

Example 2

merchants = ["a|root@x.com|Root","b|root@x.com|Blue","c|c@x.com|Root","d|root@x.com|Root","e|c@x.com|Blue"]targetMerchantId = "a"return = ["b|1|50","c|1|50","d|1|100","e|2|50"]

e is reachable through both b and c, but it appears once. The direct merchants also remain unique even when additional two-hop paths reach them.

Constraints

  • 1 <= merchants.length <= 2000.
  • Every record has exactly three nonempty pipe-separated fields.
  • merchantId, email, and merchantName are case-sensitive printable ASCII strings of length at most 100 and contain no pipe.
  • targetMerchantId identifies exactly one input record.

More Stripe Phone Screen questions

Similar questions at other companies

Interview reports that mention this question

Stripe interview experiences

All Stripe interview questionsSee Stripe hiring insights
Saved
class Solution {
    public String[] linkedMerchantsWithinTwoHops(String[] merchants, String targetMerchantId) {
        // write your code here
    }
}
Checking account…