Rank Linked Merchants by Weighted Shared Attributes
Problem statement
You are given merchant records, attribute weights, and a target merchant ID.
Each row in merchants starts with a unique merchant ID and then contains alternating attribute names and values: [merchantId, name1, value1, name2, value2, ...]. Attribute names are unique within one row.
Each entry in attributeWeights has the form attributeName|weight. An attribute contributes its weight to another merchant's link score when that merchant and the target have exactly the same attribute name and value. Attributes without a supplied weight contribute zero.
Exclude the target. A different merchant is linked when its score is positive. Return every linked merchant as merchantId|score, ordered by descending score and then by ascending merchant ID.
When every supplied weight is 1, the result contains exactly the merchants that share at least one weighted attribute with the target.
Reported two-part dictionary variation
This optional interview variation is separate from the ranked-output practice contract above. Merchant records were provided as native dictionaries in a list, with fields such as merchantID, companyName, email, and phone. A target merchant ID identifies the merchant to compare against the other records.
[{"merchantID": "m1", "companyName": "name", "email": "email", "phone": "pn1"}]- In Part 1, a merchant is directly connected to the target when at least one corresponding attribute matches exactly. Matching is case-sensitive.
- In Part 2, the caller also supplies a dictionary mapping
companyName,email, andphoneto integer weights, together with a score threshold. Exact matches of the corresponding fields determine the weighted shared-attribute score, which is checked against that threshold.
The reported result is a list of matching IDs, such as ["m2", "m6", "m8"], rather than scored strings. The recollection does not establish whether equality at the threshold qualifies, the required output order, or missing-field behavior; clarify those details before implementing that variation. It does not add a two-hop requirement or a third part. The judged submission for this page still uses rankLinkedMerchants and the ranked merchantId|score results defined above.
Function
rankLinkedMerchants(merchants: String[][], attributeWeights: String[], targetMerchantId: String) → String[]Examples
Example 1
merchants = [["m1","email","a@x","country","US","phone","111"],["m2","email","a@x","country","US","phone","999"],["m3","email","z@x","country","US","phone","111"],["m4","email","a@x","country","CA","phone","111"],["m5","email","z@x","country","CA"]]attributeWeights = ["email|5","phone|3","country|2"]targetMerchantId = "m1"return = ["m4|8","m2|7","m3|5"]m4 matches email and phone for score 8; m2 matches email and country for 7; m3 matches phone and country for 5. m5 scores zero.
Example 2
merchants = [["target","email","root@x","note","same"],["b","email","root@x"],["a","email","root@x"],["c","note","same"]]attributeWeights = ["email|4","note|0"]targetMerchantId = "target"return = ["a|4","b|4"]a and b tie and are ordered by ID. Although c matches note, that attribute has weight zero, so c is not linked.
Example 3
merchants = [["solo","region","west"]]attributeWeights = ["region|10"]targetMerchantId = "solo"return = []The target is excluded and there are no other merchants.
Constraints
1 <= merchants.length <= 100000.- Every merchant row contains a unique nonempty ID followed by at most
20attribute-name/value pairs. - Merchant IDs, attribute names, and attribute values are printable ASCII strings of length at most
100and contain no pipe character. 1 <= attributeWeights.length <= 20, and every weighted attribute name appears at most once.- Every weight is an integer from
0through1000000. targetMerchantIdidentifies exactly one merchant.