Compute Graph Spread and Portfolio Trades
Company: Akuna Capital
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates graph traversal and data-manipulation competencies, combining connected-component analysis on undirected graphs with map-based computation of allocation deltas for portfolio rebalancing in the Coding & Algorithms domain for a Data Engineer role.
Part 1: Maximum Connected-Component Label Difference
Constraints
- 0 <= g_nodes <= 200000
- 0 <= len(g_from) == len(g_to) <= 200000
- 1 <= g_from[i], g_to[i] <= g_nodes when g_nodes > 0
- The graph is undirected and may contain isolated nodes
Examples
Input: (5, [1, 2, 3], [2, 5, 4])
Expected Output: 4
Explanation: The components are {1, 2, 5} with difference 5 - 1 = 4, and {3, 4} with difference 4 - 3 = 1. The maximum is 4.
Input: (4, [], [])
Expected Output: 0
Explanation: All nodes are isolated, so every component has difference 0.
Input: (6, [1, 2, 4], [2, 3, 5])
Expected Output: 2
Explanation: The components are {1, 2, 3} with difference 2, {4, 5} with difference 1, and {6} with difference 0.
Input: (0, [], [])
Expected Output: 0
Explanation: An empty graph has no components, so the result is 0.
Hints
- Traverse the graph one connected component at a time using BFS, DFS, or Union-Find.
- While exploring a component, track only its minimum and maximum node labels instead of storing every node.
Part 2: Rebalance a Portfolio
Constraints
- 0 <= number of assets <= 100000
- Both dictionaries contain exactly the same asset symbols
- Each allocation amount is an integer in the range 0 to 10^9
- The output must preserve the iteration order of current_allocations
Examples
Input: ({'AAPL': 50, 'GOOG': 30}, {'AAPL': 40, 'GOOG': 45})
Expected Output: {'AAPL': -10, 'GOOG': 15}
Explanation: Sell 10 AAPL and buy 15 GOOG.
Input: ({'BOND': 100, 'STOCK': 200, 'CASH': 50}, {'BOND': 100, 'STOCK': 200, 'CASH': 50})
Expected Output: {'BOND': 0, 'STOCK': 0, 'CASH': 0}
Explanation: The portfolio already matches the target, so no trades are needed.
Input: ({'MSFT': 10, 'AAPL': 20, 'TSLA': 30}, {'TSLA': 35, 'AAPL': 15, 'MSFT': 10})
Expected Output: {'MSFT': 0, 'AAPL': -5, 'TSLA': 5}
Explanation: The result keeps the key order from current_allocations: MSFT, AAPL, TSLA.
Input: ({}, {})
Expected Output: {}
Explanation: An empty portfolio requires no trades.
Hints
- For each asset symbol, the required trade is just target minus current.
- To preserve order, build the result by iterating through current_allocations, not target_allocations.