Interview Query

Index Fund Return

1
Have you seen this question before?

You’re given two lists:

  • A dictionary of deposits and withdrawals into an index fund with timestamps.
  • A daily price of the index fund by date.

Write a function fund_return to calculate the total profit gained from investing in the index from the start to end date. You may only purchase and sell discrete shares of the index fund. For example, if you have $23\$23 and the price of the index is $5\$5, you may only purchase four shares.

For the purposes of this question, assume that the revenue (or loss) from the index fund is applied to the deposited funds at the beginning of every day based on the percentage increase in the price of the index and that the purchases (or withdrawals) are made before the end of each day.

Example:

Input:

purchases = {'date': ['2021-06-21', '2021-06-23', '2021-06-24'],
             'purchase': [10000.00, -1000.00, 1000.00]}

prices = {'date': ['2021-06-21', '2021-06-22', '2021-06-23',
          '2021-06-24', '2021-06-25'], 'price': [50.00, 55.00, 50.00,
          55.00, 60.50]}

Output:

def fund_return(purchases, prices) -> 1989.00

Explanation:

  • 2021-06-21: Purchase 200 shares for $10,000\$10,000 - `2021-06-23`: Sell 20 shares for $1,000\$1,000
  • 2021-06-24: Purchase 18 shares for $990\$990 now have spent $9990\$9990 total
  • 2021-06-25: Own 198 shares with value $60.50\$60.50 per share. 198 * 60.5 = 11,979 - We spent a total of $9,990\$9,990 on the index fund. So our total profit is 11,979-9990 = 1989
Next question: Index Fund Return
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code