PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • Akuna Capital
  • Coding & Algorithms
  • Data Engineer

Compute Graph Spread and Portfolio Trades

Company: Akuna Capital

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

Answer both implementation tasks. ### Task 1: Maximum connected-component label difference Implement `maximumDifference(int gNodes, List<Integer> gFrom, List<Integer> gTo)`. You are given an undirected graph with nodes labeled from `1` to `gNodes`. For each index `i`, there is an undirected edge between `gFrom[i]` and `gTo[i]`. For every connected component, compute the difference between the largest node label and the smallest node label in that component. Return the maximum such difference across all connected components. An isolated node has difference `0`. Example: if the components are `{1, 2, 5}` and `{3, 4}`, their differences are `5 - 1 = 4` and `4 - 3 = 1`, so the answer is `4`. ### Task 2: Rebalance a portfolio Implement `PortfolioManager.rebalancePortfolio(IPortfolio currentPortfolio, IPortfolio targetPortfolio)`. `IPortfolio.getAllocations()` returns a map from asset symbol to an integer allocation amount. Assume both portfolios contain the same asset symbols. Return a map from each asset symbol to the trade amount needed to reach the target allocation: `tradeAmount = targetAllocation - currentAllocation` A positive value means buy more of that asset; a negative value means sell that asset. Preserve the iteration order of the current portfolio’s allocation map in the result.

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

Given an undirected graph with nodes labeled from 1 to g_nodes, compute the difference between the largest and smallest node label inside each connected component. Return the maximum such difference across all components. A component with a single isolated node has difference 0. If the graph has no nodes, return 0.

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

  1. Traverse the graph one connected component at a time using BFS, DFS, or Union-Find.
  2. While exploring a component, track only its minimum and maximum node labels instead of storing every node.

Part 2: Rebalance a Portfolio

You are given the current and target allocations for the same set of asset symbols. For each asset, compute the trade amount needed to reach the target allocation using: trade = target - current. Return a dictionary mapping each symbol to its trade amount. A positive result means buy, a negative result means sell, and zero means no trade. Preserve the key iteration order of current_allocations in the returned dictionary.

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

  1. For each asset symbol, the required trade is just target minus current.
  2. To preserve order, build the result by iterating through current_allocations, not target_allocations.
Last updated: May 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find minimum swaps to sort array with duplicates - Akuna Capital (hard)
  • Heapify an array into a max-heap - Akuna Capital (Medium)
  • Implement a ring buffer - Akuna Capital (Medium)
  • Compute max profit across dated stock quotes - Akuna Capital (Medium)
  • Break a palindrome to smallest non-palindrome - Akuna Capital (Medium)