Stock Trading with a Fee and a Cooldown
Updated
Problem statement
You are given prices, where prices[i] is the price of one share of a stock on day i. A trade means buying one share on one day and selling it on a later day. You may make as many trades as you like, but you can hold at most one share at a time, so you must sell before you buy again.
Two extra rules can apply:
- Fee: every completed trade costs
fee, paid once per buy-and-sell pair. - Cooldown: when
cooldown = 1, you cannot buy on the day right after a sale. Whencooldown = 0, there is no waiting day.
Return the largest total profit you can make. Making no trades gives 0.
The problem comes in four parts, and one function answers all of them:
- Part 1: no fee and no cooldown (
fee = 0,cooldown = 0). - Part 2: a fee on every trade (
cooldown = 0). - Part 3: a cooldown day after every sale (
fee = 0,cooldown = 1). - Part 4: both rules at once.
Function
maxProfit(prices: int[], fee: int, cooldown: int) → intExamples
Example 1
prices = [3,6,1,5,2,8]fee = 0cooldown = 0return = 13Part 1. Take every rise: buy at 3 and sell at 6, buy at 1 and sell at 5, buy at 2 and sell at 8. The profit is 3 + 4 + 6 = 13.
Example 2
prices = [3,6,1,5,2,8]fee = 2cooldown = 0return = 7Part 2. The same three trades still pay after a fee of 2 each: 1 + 2 + 4 = 7. Merging trades loses money here: buying at 1 and selling at 8 gives only 5, plus 1 from the first trade, which makes 6.
Example 3
prices = [3,6,1,5,2,8]fee = 0cooldown = 1return = 9Part 3. Buy at 3 and sell at 6. The next day, with price 1, is the cooldown. Skip the 5, buy at 2 and sell at 8. The profit is 3 + 6 = 9. Buying at 1 and selling at 8 would give only 7.
Example 4
prices = [3,6,1,5,2,8]fee = 2cooldown = 1return = 5Part 4. Two plans tie at 5. One is to buy at 3, sell at 6, then buy at 2 and sell at 8, for (3 - 2) + (6 - 2) = 5. The other is to buy at 1 and sell at 8, for 7 - 2 = 5.
Constraints
1 <= prices.length <= 5 * 1041 <= prices[i] <= 5 * 1040 <= fee <= 5 * 104cooldown is 0 or 1
More Citadel Onsite Interview questions
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Removal PoolONSITE INTERVIEW · Seen Sep 2026
- Centered K-by-K Submatrix SumsONSITE INTERVIEW · Seen Aug 2026
- Search Suggestions with a Prefix TreeONSITE INTERVIEW · Seen Aug 2026
- Traverse Interior Matrix Cells in Row-Major OrderONSITE INTERVIEW · Seen Aug 2026
- Limit Order Book Matching EnginePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Fixed-Capacity Circular BufferONSITE INTERVIEW · Seen Jun 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2026
- 2048 Simulation and Board CodecONSITE INTERVIEW · Seen Jan 2026
Similar questions at other companies
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Distinct Subset SumsGoogle · ONSITE INTERVIEW
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEW
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEW
- Coin ChangeMicrosoft · ONSITE INTERVIEW
- Maximum SubarrayUpstart · ONSITE INTERVIEW
- Smallest Sufficient TeamExpedia · ONSITE INTERVIEW
- Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEW
- Word BreakMoveworks · ONSITE INTERVIEW
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences