Capping Portfolio Weights
Updated
Problem statement
You manage a portfolio of N different stocks. For stock i, its ticker is stockTickers[i], you hold numShare[i] shares, and each share costs pricePerShare[i].
The value of a stock is its number of shares times its price per share. Its weight is its value divided by the total value of all N stocks. The weights always add up to 100%.
A new rule says that no stock may have a weight above maxWeight. To follow the rule, change the weights step by step:
- Take the stock with the highest weight. If its weight is not above
maxWeight, stop. If several stocks share the highest weight, take the one whose ticker comes first alphabetically. - Cut that stock's weight down to exactly
maxWeight. The stock is now capped, and its weight does not change again. - Give the weight you cut away to the stocks that are not capped yet, in proportion to their current weights. A stock with twice the weight of another gets twice as much of the extra.
- Go back to step 1, looking only at the stocks that are not capped yet.
The portfolio must stay fully invested: the weights always add up to exactly 100%, and no money is kept as cash.
Return the final weights as decimals, where 0.1 means 10%. List them from highest to lowest and round each value to 4 decimal places.
If capping is impossible, return [-1]. This happens exactly when N * maxWeight < 1: even with every stock at the cap, the weights would add up to less than 100%.
Function
capWeights(stockTickers: String[], numShare: int[], pricePerShare: double[], maxWeight: double) → double[]Examples
Example 1
stockTickers = ["A","B","C"]numShare = [40,10,30]pricePerShare = [5.00,50.00,10.00]maxWeight = 0.37return = [0.37,0.37,0.26]The values are A = 40 × 5.00 = 200, B = 10 × 50.00 = 500 and C = 30 × 10.00 = 300. The total is 1000, so the starting weights are A 20%, B 50% and C 30%.
- B has the highest weight and is above 37%. Cut it to 37%. The 13% that was cut goes to A and C in the ratio 20 : 30, so A gets 5.2% and C gets 7.8%. Now A is 25.2% and C is 37.8%.
- C is now above 37%. Cut it to 37%. The extra 0.8% goes to A, the only stock that is not capped. A becomes 26%.
- No stock is above 37%. From highest to lowest the weights are 37%, 37% and 26%.
Example 2
stockTickers = ["AMD","BA","CAT","DIS","F","GS","HD"]numShare = [12,5,8,3,40,2,6]pricePerShare = [150.00,210.50,330.25,95.00,11.80,480.00,390.75]maxWeight = 0.05return = [-1.0]There are 7 stocks and each may hold at most 5%. Even with every stock at the cap, the weights add up to only 7 × 5% = 35%, not 100%. Capping is impossible, so the answer is [-1].
Example 3
stockTickers = ["XOM","KO","PEP","T"]numShare = [20,50,25,30]pricePerShare = [15.00,6.00,10.00,5.00]maxWeight = 0.28return = [0.28,0.28,0.275,0.165]The values are XOM 300, KO 300, PEP 250 and T 150. The total is 1000, so the weights are 30%, 30%, 25% and 15%.
- XOM and KO tie for the highest weight. KO comes first alphabetically, so cut KO to 28%. The 2% goes to XOM, PEP and T in the ratio 30 : 25 : 15. XOM becomes about 30.857%, PEP about 25.714% and T about 15.429%.
- XOM is now above 28%. Cut it to 28%. The extra, about 2.857%, goes to PEP and T in the ratio 25 : 15. PEP becomes 27.5% and T becomes 16.5%.
- No stock is above 28%. The answer is
[0.28,0.28,0.275,0.165].
Example 4
stockTickers = ["NVDA","AMZN","META"]numShare = [70,4,2]pricePerShare = [10.00,50.00,50.00]maxWeight = 0.5return = [0.5,0.3333,0.1667]The values are NVDA 700, AMZN 200 and META 100, so the weights are 70%, 20% and 10%.
- Cut NVDA to 50%. The 20% that was cut goes to AMZN and META in the ratio 2 : 1. AMZN becomes 33.333...% and META becomes 16.666...%.
- Rounded to 4 decimal places, the weights are 0.5, 0.3333 and 0.1667.
Constraints
1 <= N <= 1000, whereNis the length ofstockTickers,numShareandpricePerShare.- Each
stockTickers[i]has 1 to 5 uppercase English letters, and all tickers are different. 1 <= numShare[i] <= 10^60.01 <= pricePerShare[i] <= 10^4, with at most 2 digits after the decimal point.0.0001 <= maxWeight <= 1, with at most 4 digits after the decimal point.N * maxWeightis never exactly1.- No final weight lies exactly halfway between two 4-decimal values, so rounding up or down at a tie never matters.
More Capstone Investment Advisors Online Assessment questions
Similar questions at other companies
- Macaron Order PriceOpenAI · OAOpenAI interview questions
- Beam Search and Nucleus DecodingMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Daily Settlement for Long and Short PositionsBelvedere Trading · OA
- Sorted Cyclic Shift Difference SumsCapital One · OACapital One interview questions
- Optimize Package Order by Sorting One WindowAmazon · OA
- Linear InterpolatorTwo Sigma · OATwo Sigma interview questions