Round 1
Questions: You are given two lists:
price[]: A list of integers representing the prices of different stocks.
futurePrice[]: A list of integers representing the future prices of those stocks.
principle: An integer representing the amount of money you have available to invest.
You can buy as many stocks as you want, but the total cost of the stocks you buy must not exceed your principle.
Task:
Write a function that calculates the maximum profit you can make by buying stocks, where:
- The profit for each stock is calculated as the difference between its futurePrice and price (i.e., futurePrice[i] - price[i]).
- You can buy a stock only if its price is less than or equal to the available principle.
Constraints:
You can buy any number of stocks, as long as the total price of the selected stocks is less than or equal to principle.
If you can't buy any stock (because all stock prices exceed the principle), the maximum profit is 0.
Example:
price = [10, 50, 30, 40, 70] futurePrice = [100, 40, 50, 30, 90] principle = 100
Output:
Maximum profit: 180
Candidate's Approach
The candidate approached the problem by first calculating the profit for each stock as the difference between its future price and current price. Then, they filtered the stocks that could be purchased within the given principle. The candidate implemented a greedy algorithm to maximize profit by selecting stocks with the highest profit margins first, ensuring that the total cost did not exceed the available principle.
Interviewer's Feedback
The interviewer appreciated the candidate's clear understanding of the problem and the efficient approach to maximize profit. They suggested considering edge cases, such as when all stock prices exceed the principle, and encouraged the candidate to discuss potential optimizations for larger datasets.