KatInterview

Cluster Exposures: Net Change per Asset Group

Trees & GraphsModel answer
MediumModel answerTrees & Graphsgraph traversalconnected componentshash map

Last updated

Find connected components in an asset correlation graph and return the summed position delta for each component as a labeled group.

Cluster Exposures

You are building a risk aggregation tool. The tool receives two inputs:

  1. positions — a list of (ticker, delta) pairs, where ticker is a string asset identifier and delta is a signed integer representing the net change in that asset's holdings. The same ticker may appear more than once; treat repeated entries as additive.

  2. links — a list of (ticker_a, ticker_b) pairs indicating that the two assets belong to the same hedging cluster. The relationship is symmetric: if A is linked to B, then B is linked to A. Linkage is also transitive: if A–B and B–C, all three belong to the same cluster.

A ticker that appears in links but has no entry in positions is still a valid cluster member with a delta of zero. A ticker with no links at all forms a singleton cluster.

Your task: partition all tickers into their clusters and return a dictionary mapping a cluster label to the cluster's total delta.

The label for a cluster is the alphabetically sorted ticker names joined by _. For a singleton, the label is just the ticker name.

Function Signature

def cluster_deltas(
    positions: List[Tuple[str, int]],
    links: List[Tuple[str, str]],
) -> Dict[str, int]:

Constraints

  • Number of distinct tickers: 1 to 10^5
  • Number of links: 0 to 10^5
  • delta: −10^9 to 10^9

Examples

Example 1

positions  = [('AAPL', 10), ('MSFT', -5), ('GOOG', 20)]
links      = [('AAPL', 'MSFT')]

Output: {'AAPL_MSFT': 5, 'GOOG': 20}

AAPL and MSFT are in the same cluster: 10 + (−5) = 5. GOOG is unlinked, so it stands alone with delta 20.

Example 2

positions  = [('A', 1), ('B', 2), ('C', 3)]
links      = [('A', 'B'), ('B', 'C')]

Output: {'A_B_C': 6}

A–B and B–C make all three one cluster: 1 + 2 + 3 = 6.

What is expected

Return the dictionary described above. Order of keys does not matter. Explain your approach, its time and space complexity, and how you handle the edge cases listed below:

  • A ticker that appears in links but not in positions.
  • Duplicate or reversed link entries.
  • Very large chains of tickers (up to 10^5 nodes).
  • Extremely large total deltas.

Hints

Hint 1

Model each ticker as a graph node and each link as an undirected edge. The clusters you need are exactly the connected components of that graph.

Hint 2

Before traversing, aggregate deltas per ticker into a hash map — a ticker can appear multiple times in `positions`. Also register any ticker that appears only in `links` with a default delta of zero, or you will miss nodes that bridge two groups.

Hint 3

Use iterative DFS (an explicit stack) rather than recursive DFS. A chain of 10^5 nodes will blow Python's default recursion limit. After collecting all members of a component, sort their names and join with `_` to get a deterministic key regardless of traversal order.