15. Best Time to Buy and Sell Stock
easyAsked at AdobeGiven an array of stock prices, find the maximum profit from one buy-sell transaction. Adobe uses this to test single-pass sliding window thinking and the candidate's ability to track a running minimum — skills applicable to range normalization and histogram equalization in image processing.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Adobe loops.
- Glassdoor (2026-01)— Adobe SDE-I and SDE-II candidates report stock problems in both phone and onsite rounds.
- Blind (2025-10)— Adobe interviewers use this as a test for greedy single-pass intuition.
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) and sell on day 5 (price=6), profit = 6-1 = 5.
Example 2
prices = [7,6,4,3,1]0Explanation: No transaction yields profit; return 0.
Approaches
1. Brute force nested loops
Try every (buy, sell) pair where buy < sell, track the maximum difference.
- Time
- O(n^2)
- Space
- O(1)
function maxProfit(prices) {
let max = 0;
for (let i = 0; i < prices.length; i++) {
for (let j = i + 1; j < prices.length; j++) {
max = Math.max(max, prices[j] - prices[i]);
}
}
return max;
}Tradeoff: Quadratic — immediately identifiable as the naive approach; Adobe expects you to reach O(n) within the first minute.
2. Single-pass greedy with running minimum
Iterate once, tracking the minimum price seen so far. At each step, compute profit as current price minus running minimum and update the global maximum. The insight is that the best sell day for any buy day is the highest price after that buy day, so we can greedily update as we scan left-to-right.
- 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 {
maxProfit = Math.max(maxProfit, price - minPrice);
}
}
return maxProfit;
}Tradeoff: O(n) time, O(1) space. The running-minimum pattern is the key insight and applies broadly to range normalization problems Adobe encounters in color processing.
Adobe-specific tips
Adobe interviewers draw a parallel between this problem and dynamic range compression in image processing — tracking the global minimum and computing the maximum lift is structurally identical to finding the best tone-mapping window. After the solution, expect follow-ups about buying and selling multiple times (LC 122) and the impact on the algorithm design.
Common mistakes
- Initializing maxProfit to a negative number — the problem guarantees you can always return 0 for no profit.
- Updating minPrice and computing profit in the same branch — you must check profit before updating the minimum.
- Using the else-branch pattern incorrectly so a new global minimum also contributes a profit of 0.
Follow-up questions
An interviewer at Adobe may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122): unlimited transactions.
- Best Time to Buy and Sell Stock III (LC 123): at most 2 transactions.
- How would you solve this if the prices are too large to fit in memory (streaming input)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why initialize minPrice to Infinity?
Any real price will be less than Infinity, so the first element always sets the initial minimum correctly. Initializing to prices[0] is also valid but requires a range check.
What if all prices are the same?
Every day computes profit = 0, minPrice stays the same, and the function correctly returns 0.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Adobe interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →