Maximize Stock Trading Profits Using Dynamic Programming
Company: Citadel
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates proficiency in dynamic programming, state modeling for sequential decision problems, and algorithmic optimization related to constrained transaction planning.
Constraints
- 0 <= len(prices) <= 10000
- 0 <= k <= 1000
- 0 <= prices[i] <= 10^9
- At most one position at any time; buy before next sell
- Time target: O(k·n) and O(n) space; optimize to O(n) time if k >= n/2
Hints
- If k >= n/2, it is equivalent to unlimited transactions; sum all positive price differences.
- Use DP: for each t in [1..k], compute cur[i] = max(cur[i-1], prices[i] + best) where best = max(best, prev[i] - prices[i]).
- Roll arrays (prev, cur) to keep O(n) space.