121. Best Time to Buy and Sell Stock
easyAsked at GleanGlean screens for greedy and sliding-window reasoning here — the same mindset used when scanning a time-series of document relevance scores to find the best retrieval window.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Glean loops.
- Glassdoor (2026-Q1)— Reported as a common warm-up in Glean phone screens, often paired with a follow-up on multiple transactions.
- Blind (2025-10)— Glean threads confirm this appears in early technical screens alongside array-traversal problems.
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.
Approaches
1. One-pass greedy (track min price)
Track the minimum price seen so far. At each day, compute the potential profit if you sold today. Update the max profit accordingly.
- 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;
} else if (price - minPrice > maxProfit) {
maxProfit = price - minPrice;
}
}
return maxProfit;
}Tradeoff: O(n) time, O(1) space. This is the canonical one-pass solution. Glean interviewers value the ability to recognize greedy patterns that avoid extra allocations.
Glean-specific tips
Frame your thinking out loud: 'At each day I either update the cheapest buy price I've seen, or check if selling today beats my best profit so far.' Glean values a clear mental model narrated before code — it shows you think like a search-system engineer who reasons about streaming data one element at a time.
Common mistakes
- Initializing minPrice to prices[0] then indexing from 1 — works but is fragile. Initializing to Infinity and iterating from 0 is cleaner.
- Allowing buy and sell on the same day — sell must happen strictly after buy; the problem guarantees this since you check price - minPrice only after updating minPrice.
- Returning a negative profit instead of 0 — always return Math.max(maxProfit, 0) or initialize maxProfit to 0.
- Using a two-pointer approach that allows sell < buy — ensure the left pointer (buy) always stays to the left of the current index.
Follow-up questions
An interviewer at Glean may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions; how does the strategy change?
- Best Time to Buy and Sell Stock with Cooldown (LC 309) — add a 1-day cooldown after selling.
- What if you must hold at most k transactions? (LC 188)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why doesn't this need dynamic programming?
Because you can only make one transaction and buy must precede sell. The optimal buy price up to any day is the running minimum — a simple greedy decision at each step without needing to look ahead.
How does this extend to unlimited transactions?
In the unlimited case (LC 122), you add profit whenever prices[i] > prices[i-1], capturing every upward slope. The single-transaction constraint is what makes greedy here different.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Glean interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →