Linked Merchants Within Two Hops
Learn this problemProblem 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.
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, andmerchantNameare case-sensitive printable ASCII strings of length at most100and contain no pipe.targetMerchantIdidentifies exactly one input record.