Problem · Array

Calculate Portfolio Rebalancing Deltas

Learn this problem
EasyAkuna Capital logoAkuna CapitalOA

Problem statement

A current portfolio and a target portfolio assign an integer allocation to each asset. The source method iterates over the assets in the current portfolio and records, for each asset, the target allocation minus the current allocation.

For this exercise, the current portfolio is represented by parallel arrays assetIds and currentAllocations, and targetAllocations[i] is the target allocation for assetIds[i]. Return a two-column String[][] in the same order as assetIds. Each row must be [assetId, delta], where delta is the decimal string for targetAllocations[i] - currentAllocations[i].

Function

rebalancePortfolio(assetIds: String[], currentAllocations: int[], targetAllocations: int[]) → String[][]

Examples

Example 1

assetIds = ["AAPL","BOND","CASH"]currentAllocations = [40,35,25]targetAllocations = [50,20,30]return = [["AAPL","10"],["BOND","-15"],["CASH","5"]]

The target-minus-current differences are 50 - 40 = 10, 20 - 35 = -15, and 30 - 25 = 5. Each result remains paired with its asset ID.

Example 2

assetIds = ["US_EQ","INTL_EQ"]currentAllocations = [50,50]targetAllocations = [50,50]return = [["US_EQ","0"],["INTL_EQ","0"]]

Each target allocation equals its current allocation, so both rebalancing deltas are 0.

Constraints

  • assetIds.length == currentAllocations.length == targetAllocations.length.
  • Every value in assetIds is nonempty and unique.
  • -10^9 <= currentAllocations[i], targetAllocations[i] <= 10^9.

More Akuna Capital problems

drafts saved locally
public String[][] rebalancePortfolio(String[] assetIds, int[] currentAllocations, int[] targetAllocations) {
  // write your code here
}
assetIds["AAPL","BOND","CASH"]
currentAllocations[40,35,25]
targetAllocations[50,20,30]
expected[["AAPL", "10", "BOND", "-15", "CASH", "5"]]
checking account