121. Best Time to Buy and Sell Stock
easyAsked at eBayeBay's marketplace handles dynamic pricing at enormous scale — think 'what is the maximum arbitrage on a collectible card across hourly price snapshots?' This classic single-pass problem is a direct analog, and eBay interviewers use it to test greedy thinking and whether you can reframe a min-prefix question without extra storage.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in eBay loops.
- Glassdoor (2025-12)— Reported as a common eBay phone-screen problem, often framed around dynamic pricing or auction data.
- Blind (2025-10)— eBay SWE threads list this as a standard easy greedy problem expected to be solved in under 10 minutes.
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 unit and choosing a different day in the future to sell it. 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. Buying on day 1 (price=7) and any later day yields no profit.
Example 2
prices = [7,6,4,3,1]0Explanation: Prices are strictly decreasing — no transaction is profitable, so return 0.
Approaches
1. Greedy single pass
Track the minimum price seen so far and the maximum profit achievable. For each price, update the minimum, then compute the profit if sold today and update the 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;
} else if (price - minPrice > maxProfit) {
maxProfit = price - minPrice;
}
}
return maxProfit;
}Tradeoff: O(n) time, O(1) space — optimal. The key insight is that the best buy price to the left of any sell day is the running minimum. Selling before buying is impossible because we update minPrice before computing profit.
eBay-specific tips
eBay interviewers appreciate framing: 'At each day, the best I could have done is buy at the lowest price I've seen so far.' This greedy insight is the entire solution. Draw a price chart on the whiteboard, mark the minimum and the peak after it. Expect the follow-up: 'What if you could make unlimited transactions?' (LC 122 — sum of all ascending runs). 'What if you could make at most k transactions?' (LC 188 — DP). Know these variations exist even if you don't code them.
Common mistakes
- Trying to compare all pairs — O(n²) and unnecessary once you recognize the running-minimum pattern.
- Returning a negative profit — the problem guarantees 0 if no profitable trade exists; initialize maxProfit to 0, not -Infinity.
- Updating minPrice and profit in the wrong order — computing profit before updating minPrice gives a same-day buy/sell (price - price = 0), which is fine but may confuse your explanation.
- Confusing this problem (at most 1 transaction) with the unlimited-transactions variant.
Follow-up questions
An interviewer at eBay may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions; sum all positive day-over-day gains.
- Best Time to Buy and Sell Stock III (LC 123) — at most 2 transactions; DP with state tracking.
- How would you compute a rolling maximum profit over a sliding window of k days?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why initialize minPrice to Infinity?
Infinity is always greater than any real price, so the first element will always set minPrice correctly on the first iteration without a special case.
What if all prices are equal?
profit = price - minPrice = 0 on every iteration, so maxProfit stays 0 — the correct answer.
Can I solve this with Kadane's algorithm?
Yes — compute the daily differences array (prices[i] - prices[i-1]) and apply Kadane's max-subarray on it. It produces the same result. Worth mentioning as a connection to maximum subarray.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other eBay interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →