Aggregate Trades Across Equivalent Symbols - Coding Interview
Question Details
Problem
A trading system uses multiple symbol aliases for the same underlying instrument (e.g., "BTC-USD", "BTCUSD", "XBT-USD" all refer to Bitcoin/Dollar). You are given:
1. A list of alias pairs [(sym_a, sym_b)] indicating the two symbols are equivalent.
2. A list of trades [(symbol, quantity, price)].
Aggregate all trades by canonical symbol (use the lexicographically smallest alias in each equivalence class as the canonical name) and return a dict mapping canonical symbol to total traded notional value (sum of quantity * price).
python
def aggregate_trades(
aliases: list[tuple[str, str]],
trades: list[tuple[str, int, float]]
) -> dict[str, float]:
...
Example:
aliases = [("BTCUSD","XBT-USD"), ("ETHUSD","ETH-USD")]
trades = [("XBT-USD",2,30000),("BTCUSD",1,31000),("ETH-USD",5,2000)]
**Output**: {"BTCUSD": 91000.0, "ETHUSD": 10000.0}
Follow-ups
- What data structure best represents the equivalence classes? (Union-Find vs. adjacency list + BFS)
- How would you handle a cycle in the alias graph (e.g., A=B, B=C, C=A)?
- Extend to support weighted aggregation where newer trades count more.
Full Details
Problem
A trading system uses multiple symbol aliases for the same underlying instrument (e.g., "BTC-USD", "BTCUSD", "XBT-USD" all refer to Bitcoin/Dollar). You are given:
1. A list of alias pairs [(sym_a, sym_b)] indicating the two symbols are equivalent.
2. A list of trades [(symbol, quantity, price)].
Aggregate all trades by canonical symbol (use the lexicographically smallest alias in each equivalence class as the canonical name) and return a dict mapping canonical symbol to total traded notional value (sum of quantity * price).
python
def aggregate_trades(
aliases: list[tuple[str, str]],
trades: list[tuple[str, int, float]]
) -> dict[str, float]:
...
Example:
aliases = [("BTCUSD","XBT-USD"), ("ETHUSD","ETH-USD")]
trades = [("XBT-USD",2,30000),("BTCUSD",1,31000),("ETH-USD",5,2000)]
**Output**: {"BTCUSD": 91000.0, "ETHUSD": 10000.0}
Follow-ups
- What data structure best represents the equivalence classes? (Union-Find vs. adjacency list + BFS)
- How would you handle a cycle in the alias graph (e.g., A=B, B=C, C=A)?
- Extend to support weighted aggregation where newer trades count more.