Katkat

Implement Portfolio Trading Optimizer

Point72Data Engineer · Technical Screen

Last updated

This question evaluates algorithmic problem-solving, array processing, input validation via decorators, and the ability to produce concise transaction reports for maximizing non-overlapping buy/sell profits.

You are given an array prices, where prices[i] is the price of one stock on day i.

You may perform multiple buy/sell transactions over the period, but you may hold at most one share at any time. Each transaction must buy before it sells. Your goal is to maximize total profit and report the optimal set of non-overlapping transactions.

Input constraints:

  • 0 <= len(prices) <= 30000
  • 0 <= prices[i] <= 10000
  • Every price must be a non-negative integer.

Implement the following functions:

def validate_prices(func):
 """
 Decorator that validates the input prices list before calling func.
 It should reject invalid inputs according to the constraints.
 """
 pass

@validate_prices
def find_transactions(prices):
 """
 Return the optimal transactions as a list of tuples:
 (buy_day, buy_price, sell_day, sell_price, profit)

 If no profitable transaction exists, return an empty list.
 """
 pass

def generate_report(prices):
 """
 Return a human-readable report containing one line per transaction
 and a final line with total profit.
 """
 pass

Example:

prices = [7, 1, 5, 3, 6, 4]

One valid report is:

buy_day=1 buy_price=1 sell_day=2 sell_price=5 profit=4
buy_day=3 buy_price=3 sell_day=4 sell_price=6 profit=3
total_profit=7

Your implementation should be efficient enough for the maximum input size.