PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Maximize stock profit with constraints states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Maximize stock profit with constraints

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an array prices of daily stock prices, you may make at most two complete transactions (buy then sell). After each sell, there is a mandatory one-day cooldown during which you cannot buy, and each sell incurs a transaction fee fee. Compute the maximum achievable profit in O(n) time and O( 1) additional space. Return 0 if no profitable trades exist.

Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Maximize stock profit with constraints states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

You are given an integer array `prices` where `prices[i]` is the price of a given stock on day `i`, and an integer `fee`. You may complete at most two transactions (a transaction is one buy followed later by one sell; you may not hold more than one share at a time, so you must sell before buying again). Two extra rules apply: 1. Cooldown: After you sell on day `i`, you may not buy on day `i + 1` (one mandatory cooldown day). You may buy again on day `i + 2` or later. 2. Fee: Each completed sale incurs a transaction `fee`, subtracted from that sale's proceeds. Return the maximum profit you can achieve. If no sequence of trades yields a positive profit, return `0`. Your algorithm must run in O(n) time using O(1) additional space.

Constraints

  • 0 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4
  • 0 <= fee <= 10^4
  • At most two complete transactions (buy then sell); you hold at most one share at a time.
  • After any sell on day i, buying is forbidden on day i + 1 (one-day cooldown).
  • Each completed sale subtracts `fee` from its proceeds.
  • Required: O(n) time and O(1) additional space.
  • Return 0 when no profitable sequence of trades exists.

Examples

Input: ([3, 3, 5, 0, 0, 3, 1, 4], 0)

Expected Output: 6

Explanation: fee=0. Buy at 0 (day 3), sell at 3 (day 5) -> +3; cooldown day 6, buy at 1 (day 6) is the cooldown day so skip, buy at 1 on day 6 forbidden; instead buy at 0 (day 4) sell at 3 (day 5), then buy at 1 (day 6) sell at 4 (day 7). Best two cooldown-separated trades total 6.

Input: ([1, 2, 3, 4, 5], 0)

Expected Output: 4

Explanation: fee=0, strictly increasing. One transaction buy at 1, sell at 5 captures the entire +4 run; splitting it would force a cooldown gap that loses value, so 4 is optimal.

Input: ([7, 6, 4, 3, 1], 0)

Expected Output: 0

Explanation: Prices only fall, so every possible sale loses money. Making no trade yields 0, which is the maximum.

Input: ([], 0)

Expected Output: 0

Explanation: Empty price series: no trades possible, profit is 0.

Input: ([5], 1)

Expected Output: 0

Explanation: A single day gives no chance to buy then sell, so profit is 0.

Input: ([1, 4, 2, 8], 1)

Expected Output: 6

Explanation: fee=1. Two trades buy 1/sell 4 (+2) then buy 2/sell 8 (+5) would need a cooldown after day 1 sell, but day 2 is the cooldown so buying at 2 on day 2 is forbidden; the single trade buy 1/sell 8 nets 8-1-1=6, which beats any fee-and-cooldown-constrained split.

Input: ([1, 4, 2, 8], 2)

Expected Output: 5

Explanation: fee=2. The larger fee makes a second transaction less attractive; the best single trade buy 1/sell 8 nets 8-1-2=5, and no cooldown-legal two-trade plan exceeds it.

Input: ([1, 3, 2, 8, 4, 9], 0)

Expected Output: 8

Explanation: fee=0. Buy 1/sell 3 (+2), cooldown on day 2, buy 2 forbidden on cooldown day so buy at the next eligible day; the optimal cooldown-separated pair of trades totals 8.

Hints

  1. Track four running states as you sweep the prices once: the best profit while holding the 1st share (buy1), after completing the 1st sale (sell1), while holding the 2nd share (buy2), and after the 2nd sale (sell2).
  2. The cooldown only matters between the first sale and the second buy. Buy the 2nd share using the value of sell1 from *yesterday*, not today, so a buy can never sit on the day immediately after a sale.
  3. Apply the `fee` at the moment of each sell: sell transitions use `+ price - fee`. Buy transitions use `- price`.
  4. The answer is max(0, sell1, sell2): zero transactions is always allowed, and one transaction may beat two when the fee or cooldown eats the second trade's margin.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimum Path Length Through a Grid With One Allowed Cell Conversion - Amazon (medium)
  • Circular Drone Hub Delivery Route - Amazon (hard)
  • Leaf Domain Cumulative Scores - Amazon (medium)
  • Kth Largest Perfect Binary Subtree - Amazon (medium)
  • Find Conflicting Events - Amazon (medium)