Maximize Rebalancing Profit With Per-Trade Transaction Costs
Last updated: November 29, 2026
Quick Overview
Given daily prices of one asset and a fixed cost charged on every round trip, you may hold at most one unit at a time; return the maximum achievable profit.
Voleon
November 29, 20260
0
0 solved
Given daily prices of one asset and a fixed cost charged on every round trip, you may hold at most one unit at a time; return the maximum achievable profit.
Transaction costs are what separate a paper signal from a tradeable one at a firm like Voleon, so this classic state-machine DP is framed around them. The interviewer checks whether you define clean states, write a correct O(1)-space recurrence, and explain why greedy rules fail once costs are present.
What the Interviewer Expects
- Define states for flat and long positions
- Write the recurrence and the order of updates
- Explain why summing every up-move is wrong with costs
- Reduce memory to O(1)
- Trace a small example by hand
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How does the answer change if you can also go short one unit?
- What if the cost is proportional to price rather than fixed?
- How would you limit the solution to at most K round trips?
- How would you recover the actual buy and sell days?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Prices p[0..n-1], cost c >= 0 paid once per completed round trip. Position is 0 or 1 unit. Return the max realized profit, which is at least 0 because doing nothing is allowed....
High-Level Design
Track two values after each day: flat, the best cash if we end the day with no position, and long_, the best cash if we end the day holding one unit. Transitions: stay flat or sell today (`long_ +...