Katkat

Compute Graph Spread and Portfolio Trades

Akuna CapitalData Engineer · Online Assessment

Last updated

This problem tests graph traversal (connected components) and hash-map manipulation to calculate net delta changes in portfolio assets within identified groups.

Problem Statement

You are given a list of trades representing rebalancing actions and a list of correlations representing assets that must be balanced together.

Each trade is a tuple (asset_id, amount), where amount is a signed integer representing the net change in that asset's position. The correlations list contains pairs (asset_a, asset_b), indicating that these two assets belong to the same 'risk group'. Any change in one asset within a risk group must be offset by the aggregate changes of other assets in that same group.

Your task is to identify all connected risk groups and return the net delta for each group. The net delta of a group is the sum of the amount values of all assets within that group. If an asset has no correlations, it forms a group of size one.

Function Signature

def compute_group_deltas(trades: List[Tuple[str, int]], correlations: List[Tuple[str, str]]) -> Dict[str, int]

Constraints

  • Number of assets: 1 to 10^5
  • Number of correlations: 0 to 10^5
  • amount: -10^9 to 10^9

Examples

Example 1:

  • trades = [('AAPL', 10), ('MSFT', -5), ('GOOG', 20)]
  • correlations = [('AAPL', 'MSFT')]
  • Output: {'AAPL_MSFT': 5, 'GOOG': 20}
  • Explanation: AAPL and MSFT are linked (10 + -5 = 5). GOOG is independent (20).

Example 2:

  • trades = [('A', 1), ('B', 2), ('C', 3)]
  • correlations = [('A', 'B'), ('B', 'C')]
  • Output: {'A_B_C': 6}
  • Explanation: All three are in one component (1 + 2 + 3 = 6).

Hints

Hint 1

Model the assets as nodes in a graph and the correlations as undirected edges between them.

Hint 2

Use Depth First Search (DFS) or Breadth First Search (BFS) to identify all connected components in the graph.

Hint 3

For each connected component, aggregate the trade amounts of all nodes belonging to that component and store the result using a unique key derived from the assets in that group.

Follow-up questions

  • How would your implementation change if the correlations were directed, implying that only one-way hedging is required?
  • What is the time complexity of your solution in terms of assets (V) and correlations (E)?
  • How would you handle the case where an asset appears in the correlations list but is missing from the trades list (assume 0 amount)?