121. Best Time to Buy and Sell Stock
easyAsked at BloombergGiven a daily price series, return the maximum profit from one buy + one sell. Bloomberg leans on this for finance-engineering candidates — it's a single-pass greedy that mirrors the real-world 'running max profit' calculation traders care about.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Bloomberg loops.
- Glassdoor (2026-Q1)— Bloomberg SWE phone-screen reports list this as a near-default question for fintech-team rounds.
- Blind (2025-11)— Bloomberg new-grad reports highlight it as common for finance-adjacent teams.
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 = 5.
Example 2
prices = [7,6,4,3,1]0Explanation: No profitable transactions; return 0.
Approaches
1. Single-pass running minimum (optimal)
Track the minimum price seen so far. On each day, profit = price - min. Return the max profit ever seen.
- 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: The canonical answer. Single pass, constant space, and the structure mirrors how traders mentally track 'best buy point so far'.
2. Brute-force two-pointer
For every buy day, scan all future sell days. Return the max diff.
- 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++) {
if (prices[j] - prices[i] > best) best = prices[j] - prices[i];
}
}
return best;
}Tradeoff: Easy to reason about. Bloomberg interviewers want you to NAME this first, then show the O(n) version.
Bloomberg-specific tips
Bloomberg interviewers grade this on the SINGLE-PASS INTUITION. State 'on each day I track the cheapest buy point so far' before coding. Avoid Kadane's-style DP framing — for the one-transaction case the running-min approach is more direct.
Common mistakes
- Returning a negative profit when no profit is possible (return 0 instead).
- Using max(prices) - min(prices) — that fails when the max comes before the min.
- Over-engineering with DP when O(1) state suffices.
Follow-up questions
An interviewer at Bloomberg may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions, greedy.
- Best Time to Buy and Sell Stock III (LC 123) — at most 2 transactions, DP.
- Best Time to Buy and Sell Stock IV (LC 188) — at most k transactions.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why running min, not running max?
Because each day's potential profit is current_price - cheapest_buy_so_far. You always want the cheapest buy point that comes BEFORE today.
Will Bloomberg ask the multi-transaction follow-up?
Often, yes. Have LC 122 (unlimited) ready — it's a greedy where you sum every up-step.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Bloomberg interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →