121. Best Time to Buy and Sell Stock
easyAsked at RobinhoodGiven an array of daily stock prices, return the maximum profit from a single buy and sell. This is the most thematically on-brand question Robinhood asks — and they ask it specifically to test whether you reach for the one-pass min-tracking trick versus a brute-force pair search.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Robinhood loops.
- Glassdoor (2026-Q1)— Robinhood SWE phone-screen reports list this as the single most thematically appropriate warm-up.
- Reddit r/cscareerquestions (2025-12)— Multiple Robinhood new-grad trip reports cite this and its harder variants.
- Blind (2026-Q1)— Robinhood SWE-I/II loop reports include this and follow-ups like 'with at most k transactions' and 'with cooldown'.
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. 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 = 5.
Example 2
prices = [7,6,4,3,1]0Explanation: Prices only fall; no profit possible.
Approaches
1. Brute-force pairs
Try every (i, j) pair with i < j and track the max of prices[j] - prices[i].
- Time
- O(n^2)
- Space
- O(1)
function maxProfitBrute(prices) {
let best = 0;
for (let i = 0; i < prices.length; i++) {
for (let j = i + 1; j < prices.length; j++) {
best = Math.max(best, prices[j] - prices[i]);
}
}
return best;
}Tradeoff: Quadratic, doesn't scale to 10^5. Use it as the verbal warm-up: 'the question is which (i, j) pair maximizes prices[j] - prices[i] given i < j.'
2. Single pass with running min (optimal)
Track the minimum price seen so far. At each day compute price - min and update the best.
- Time
- O(n)
- Space
- O(1)
function maxProfit(prices) {
let minPrice = Infinity;
let best = 0;
for (const p of prices) {
if (p < minPrice) minPrice = p;
else if (p - minPrice > best) best = p - minPrice;
}
return best;
}Tradeoff: O(n) time and O(1) space — the canonical answer. The trick is the invariant: at day i, the best profit using day i as the sell day is prices[i] - min(prices[0..i]).
Robinhood-specific tips
Robinhood interviewers will let you start with brute-force as the verbal warm-up, but they expect you to reach the one-pass min-tracking solution within five minutes. Articulate the invariant out loud: 'At each day, the best sell-on-this-day profit is the current price minus the running min of prior days.' Then code. Be prepared for the follow-up about multiple transactions (LeetCode 122).
Common mistakes
- Initializing best to -Infinity instead of 0 — you should return 0 when no profitable trade exists, not the smallest negative.
- Tracking min and max independently and returning max - min — fails when min comes after max (e.g. [7,1] returns 6 incorrectly).
- Forgetting that buy must strictly precede sell (different day).
Follow-up questions
An interviewer at Robinhood may pivot to one of these next:
- Best Time to Buy and Sell Stock II — unlimited transactions, sum every positive day-over-day delta.
- Best Time to Buy and Sell Stock III — at most 2 transactions, requires 2D DP.
- Best Time to Buy and Sell Stock IV — at most k transactions, generalized DP.
- With Cooldown / With Fee — state-machine DP variants.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does running the min work?
Because the profit on day i depends only on the lowest buy price in days [0..i-1]. You don't need to look at every prior price — just the best one to have bought at.
What if no profit is possible?
Return 0. The problem statement says 'if you cannot achieve any profit, return 0' — which matters because Math.max(0, negative) is the correct default for best.
Free learning resources
Curated free links for this problem.