122. Best Time to Buy and Sell Stock II
mediumAsked at Goldman SachsMultiple buy-sell transactions allowed, can hold at most one share at a time — what's the max profit? Goldman Sachs uses this immediately after the single-transaction variant to test whether you can recognize the 'sum of positive day-to-day deltas' trick.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Goldman Sachs loops.
- Glassdoor (2026-Q1)— Goldman Sachs SWE candidates report this as the natural follow-up to LC #121 in the same loop.
- LeetCode Discuss (2025-12)— Best Time II is in the top-10 of LeetCode's Goldman Sachs company tag.
Problem
You are given an integer array prices where prices[i] is the price of a given stock on the i-th day. On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day. Find and return the maximum profit you can achieve.
Constraints
1 <= prices.length <= 3 * 10^40 <= prices[i] <= 10^4
Examples
Example 1
prices = [7,1,5,3,6,4]7Explanation: Buy day 2 (1), sell day 3 (5) → 4. Buy day 4 (3), sell day 5 (6) → 3. Total = 7.
Example 2
prices = [1,2,3,4,5]4Explanation: Buy day 1 (1), sell day 5 (5) — OR equivalently sum every up-day delta: 1+1+1+1 = 4.
Example 3
prices = [7,6,4,3,1]0Explanation: No profit possible.
Approaches
1. Dynamic programming (two states)
State[i][hold or not] = max profit. Transition: hold today comes from yesterday hold OR yesterday cash - price; cash today comes from yesterday cash OR yesterday hold + price.
- Time
- O(n)
- Space
- O(1)
function maxProfitDP(prices) {
let cash = 0;
let hold = -Infinity;
for (const price of prices) {
const prevCash = cash;
cash = Math.max(cash, hold + price);
hold = Math.max(hold, prevCash - price);
}
return cash;
}Tradeoff: Generalizes to LC #309 (cooldown) and LC #714 (transaction fee) with minimal changes. Linear time, O(1) space. The two-state framing is the Goldman-canonical answer for the 'Stock' family.
2. Sum of positive deltas (optimal one-liner)
Collect every consecutive (up) day's gain. Equivalent to capturing every positive slope segment.
- Time
- O(n)
- Space
- O(1)
function maxProfit(prices) {
let profit = 0;
for (let i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];
}
return profit;
}Tradeoff: Mathematically identical to summing every up-segment, because (peak - trough) on a segment equals the sum of consecutive positive deltas inside it. Linear time, constant space, fits on one screen. Goldman loves this version because it shows the telescoping insight.
Goldman Sachs-specific tips
Goldman Sachs interviewers want to hear 'I can break every up-segment into its consecutive deltas, and those telescope back to the segment's total'. That sentence wins the round. They will then ask LC #123 (at most 2 transactions) which requires the DP framing — be ready to extend. The DP is the more general answer for senior roles; the sum-of-deltas is sufficient for new-grad rounds.
Common mistakes
- Trying to find local minima and maxima explicitly — works but uses 3x the code of the sum-of-deltas trick.
- Forgetting that 'same-day buy and sell' is allowed in this variant — it just means you can chain transactions back-to-back, NOT that intraday is special.
- Using a sliding window when the question doesn't have a window — this is a greedy, not a window problem.
Follow-up questions
An interviewer at Goldman Sachs may pivot to one of these next:
- At most 2 transactions — LC #123. (Requires the DP framing.)
- At most k transactions — LC #188. (Generalize DP to k states.)
- With a 1-day cooldown after selling — LC #309.
- With a per-transaction fee — LC #714.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is summing positive deltas equivalent to picking peaks and troughs?
Telescoping. If you buy at the trough T and sell at the peak P, profit = P - T. But P - T = (P - P_{n-1}) + (P_{n-1} - P_{n-2}) + ... + (T_{+1} - T). Each consecutive delta inside an up-segment is positive, so their sum equals the segment's total.
Does this generalize to multi-day cooldowns?
No — the moment you add a cooldown, the simple sum-of-deltas breaks because not every up-day is reachable. Switch to the DP framing for any variant beyond 'unlimited, no constraint'.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock II and other Goldman Sachs interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →