Katkat

Find linked merchants by shared fields

StripeSoftware Engineer · Technical Screen

Last updated

This question tests the ability to model entity relationships using a graph representation and implement a traversal algorithm to identify connected components based on shared identity fields.

Problem Statement

You are given a list of Merchant objects. Two merchants are considered 'linked' if they share at least one non-null value in the same field (e.g., they share the same tax_id or email). A linkage is transitive: if Merchant A is linked to Merchant B, and B is linked to C, then A, B, and C are all part of the same entity group.

Your task is to write a function that takes a list of merchants and returns a list of sets, where each set contains the ids of all merchants belonging to the same entity group.

Class Definition

class Merchant:
    def __init__(self, id, tax_id, email, phone):
        self.id = id
        self.tax_id = tax_id # Can be None
        self.email = email   # Can be None
        self.phone = phone   # Can be None

Example

Input:

  • M1: {id: 1, tax_id: 'T1', email: '[email protected]', phone: None}
  • M2: {id: 2, tax_id: 'T1', email: None, phone: '123'}
  • M3: {id: 3, tax_id: None, email: None, phone: '456'}

Output: [{1, 2}, {3}] Explanation: M1 and M2 share tax_id 'T1', linking them. M3 shares no fields with anyone else.

Constraints

  • n (number of merchants) <= 10,000.
  • Fields may be None. Ignore None values when determining links.
  • An empty list returns an empty list.

Edge Cases

  • All fields are None for a merchant (should appear as a singleton group).
  • Chains of links (A-B, B-C, C-D).
  • Multiple merchants sharing the same email but different tax_ids.

Hints

Hint 1

Treat each merchant as a node in a graph and create an edge between any two merchants that share a non-null attribute.

Hint 2

Instead of creating edges between every pair, use a hash map to group merchants by each shared attribute (e.g., map tax_id 'T1' -> [1, 2]).

Hint 3

Once you have the adjacency structure or the attribute-to-merchant map, use Breadth-First Search (BFS) or Disjoint Set Union (DSU) to find the connected components.

Follow-up questions

  • How would your approach change if you had millions of merchants instead of thousands?
  • If a shared 'email' was considered a weak link (not enough to merge on its own) while a shared 'tax_id' was a strong link, how would you modify the grouping logic?
  • How would you handle the case where a single merchant has multiple emails in a list attribute?