121. Best Time to Buy and Sell Stock
easyAsked at GustoFind the maximum profit from a single buy-sell transaction. Gusto's payroll domain gives this a natural framing — think 'lowest payroll-cost day so far vs. current revenue day.' They want the greedy single-pass insight, not dynamic programming.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Gusto loops.
- Glassdoor (2025-10)— Reported as an easy warm-up in Gusto SWE phone screens with a follow-up asking about the multiple-transaction variant.
- Blind (2025-08)— Gusto prep threads list this problem as a greedy baseline that tests clean variable naming and one-pass thinking.
Problem
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Constraints
1 <= prices.length <= 10^50 <= prices[i] <= 10^4
Examples
Example 1
prices = [7, 1, 5, 3, 6, 4]5Explanation: Buy on day 2 (price=1), sell on day 5 (price=6). Profit = 6 − 1 = 5.
Example 2
prices = [7, 6, 4, 3, 1]0Explanation: Prices only decrease — no profitable transaction exists, so return 0.
Approaches
1. Greedy single pass
Track the minimum price seen so far. At each price, compute the profit if you sold today. Update the running maximum profit.
- Time
- O(n)
- Space
- O(1)
function maxProfit(prices) {
let minPrice = Infinity;
let maxProfit = 0;
for (const price of prices) {
if (price < minPrice) {
minPrice = price; // found a better buy day
} else if (price - minPrice > maxProfit) {
maxProfit = price - minPrice; // found a better sell day
}
}
return maxProfit;
}Tradeoff: Single pass, O(1) space. Greedily the minimum buy price is always updated before computing profit, so sell always comes after buy.
Gusto-specific tips
Gusto interviewers often ask you to explain why the greedy approach is correct — specifically, why you don't need to consider every buy-sell pair. Articulate that the minimum price up to day i is always a valid buy date (it occurred before today), so profit = price[i] − minSoFar is always a valid transaction. Mention the no-profit (all-decreasing) edge case before coding.
Common mistakes
- Computing profit using a future minimum price — you can only sell after you buy, so minPrice must be tracked forward.
- Returning a negative profit — the problem asks for 0 if no profit is possible; initialise maxProfit to 0.
- Checking if the array has fewer than 2 elements when the constraints guarantee length ≥ 1 — a single-element array simply returns 0 naturally.
- Using nested loops for an O(n²) solution without recognising the greedy insight.
Follow-up questions
An interviewer at Gusto may pivot to one of these next:
- What if you can complete as many transactions as you like? (LC 122 — greedy: sum all positive daily diffs.)
- What if you can complete at most k transactions? (LC 188 — dynamic programming.)
- How does your solution behave on a flat prices array (all equal prices)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why initialise minPrice to Infinity rather than prices[0]?
Either works, but Infinity is idiomatic and avoids an empty-array access. The first iteration will always update it to prices[0].
Can the buy and sell happen on the same day?
Technically the problem says 'a different day in the future', so no. But profit would be 0 in that case anyway, which doesn't beat any real positive profit.
Is this a dynamic programming problem?
It can be framed as DP, but the single-transaction constraint reduces it to a greedy O(n) scan. DP is only necessary for the k-transactions variants.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Gusto interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →