Maximize Profit From at Most K Round-Trip Trades

Last updated: November 29, 2026

Quick Overview

Given a series of daily prices for one stock and an integer k, find the maximum profit achievable with at most k buy-then-sell round trips, holding at most one share at a time.

SIG
Coding & Algorithms
Software Engineer
SIG
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a series of daily prices for one stock and an integer k, find the maximum profit achievable with at most k buy-then-sell round trips, holding at most one share at a time.

SIG interviewers like dynamic programming problems whose states map onto a trader's position (flat or long), and this one tests whether you can define states cleanly instead of guessing. They check the recurrence, the O(nk) bound, and the shortcut when k is large.

What the Interviewer Expects
  • Define states as cash when holding vs. flat after j trades
  • Write the recurrence and explain the order of updates
  • Bound time at O(nk) and space at O(k)
  • Handle k at least n/2 with the greedy sum of rises
  • Cover k = 0 and short arrays
Key Topics to Cover
dynamic programming
state machine
stock trading
space optimization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How does the answer change with a fixed fee per round trip?
  • What if you must wait one day after selling before buying again?
  • Can you recover the actual buy and sell days?
  • How would you allow short selling?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: prices (length n) and k. A round trip is one buy followed later by one sell. At most one share held, at most k round trips. Return the maximum total profit, at least 0....

High-Level Design

Track two arrays over j = 1..k. buy[j] is the best cash balance while holding a share opened as the j-th trade. sell[j] is the best cash while flat after completing j trades. On each price p: `buy...


Submit Your Answer
Markdown supported