KatInterview

Shortest Supplier Chain Between Two Vendors

Trees & GraphsAuto-graded · 21 test cases
HardAuto-gradedTrees & GraphsgraphsBFSshortest-path

Last updated

Tests BFS-based shortest-path traversal on an undirected graph, including path reconstruction and degree-of-separation counting with correct edge-case handling.

Shortest Supplier Chain Between Two Vendors

You are building a network analysis tool for a vendor directory. The directory tracks supply relationships — when one vendor regularly sources goods or services from another. Each such relationship is undirected: if vendor X supplies vendor Y, then Y also supplies X.

Given a list of supply relationships and two vendor identifiers, find the shortest chain connecting them through the supply network.

Definitions

  • A chain is a sequence of vendors [v0, v1, v2, ..., vk] where every consecutive pair (vi, vi+1) is a recorded relationship.
  • The link distance between two vendors is the number of edges on the shortest chain (i.e., len(path) - 1).
  • The hop count (degree) is the number of intermediate vendors on the shortest chain: len(path) - 2. Two directly connected vendors therefore have hop count 0, and a chain of three vendors has hop count 1.

Task

Implement a function that accepts:

  1. relationships — a list of pairs [a, b], each representing an undirected supply link between vendor a and vendor b.
  2. source — the starting vendor.
  3. target — the destination vendor.

Return a dictionary with two keys:

  • "path" — a list of vendor names representing the shortest chain from source to target, inclusive. If multiple shortest chains exist, return the lexicographically smallest one (i.e., at each step along the chain, choose the neighbor that comes first alphabetically).
  • "degree" — the hop count as defined above.

Special cases

  • If source and target are the same vendor, return {"path": [source], "degree": 0}.
  • If no chain exists (the two vendors are in disconnected parts of the network, or either vendor does not appear in any relationship), return {"path": [], "degree": -1}.
  • A direct connection source -> target has hop count 0, not 1.

Constraints

  • 0 <= len(relationships) <= 10^4
  • Each relationship is a list of exactly two strings: [a, b] where a != b.
  • Vendor names are non-empty strings containing only alphanumeric characters.
  • source and target are non-empty strings.
  • There may be duplicate relationships in the input; they are treated as a single link.
  • Self-loops do not appear in the input.

Examples

Example 1

Input: find_vendor_supply_chain([['A', 'B'], ['B', 'C']], 'A', 'C')

Output: {'path': ['A', 'B', 'C'], 'degree': 1}

Example 2

Input: find_vendor_supply_chain([['Acme', 'Beta'], ['Beta', 'Cedar'], ['Acme', 'Delta'], ['Delta', 'Cedar']], 'Acme', 'Cedar')

Output: {'path': ['Acme', 'Beta', 'Cedar'], 'degree': 1}

Example 3

Input: find_vendor_supply_chain([['A', 'B'], ['C', 'D']], 'A', 'D')

Output: {'path': [], 'degree': -1}

Hints

Hint 1

Model the relationships as an undirected adjacency structure. Which traversal strategy is guaranteed to find the shortest path in an unweighted graph?

Hint 2

Rather than storing the full path in each queue entry, keep a parent map that records how each vendor was first reached. Once the target is found, follow parent pointers back to reconstruct the path.

Hint 3

To guarantee the lexicographically smallest path among all shortest paths, process each vendor's neighbors in sorted order before adding them to the queue.