121. Best Time to Buy and Sell Stock
easyAsked at DRWAt DRW, this is not just a coding question — it is a trading question in disguise. The optimal-entry / optimal-exit framing maps directly to how DRW thinks about position entry timing in its proprietary strategies. Expect the follow-up to jump from O(n) code to expected-value maximization under uncertainty.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in DRW loops.
- Glassdoor (2026-Q1)— DRW SWE and quant developer candidates report this problem as a near-universal phone-screen question, often with follow-ups about multiple transactions and transaction costs.
- Blind (2025-12)— DRW interview threads highlight this as the canonical 'trading-flavored easy' — used to transition naturally into probabilistic follow-ups.
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 share of stock and choosing a different day in the future to sell that share. 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: No profitable transaction possible. Return 0.
Approaches
1. One-pass (track running minimum)
Walk the array once. Track the minimum price seen so far. At each step, compute the profit if selling today (price − minSoFar) and update the global 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. Single pass, no extra memory. This is the only acceptable solution at DRW. The key insight is that you never need to look backward beyond the running minimum.
DRW-specific tips
DRW interviewers will pivot from the O(n) solution to: 'What if prices is a live stream arriving at 1 million ticks per second — how does your algorithm adapt?' The one-pass approach is already streaming-compatible; you maintain minPrice and maxProfit as running state. They may then ask about Best Time to Buy and Sell Stock II (unlimited transactions) and III (at most 2 transactions). Frame your answers around expected value: 'In a real strategy you wouldn't just maximize past profit — you'd weight each buy decision by the probability that a better entry exists later.'
Common mistakes
- Using O(n²) by computing every buy-sell pair — never acceptable at DRW's data volumes.
- Allowing selling before buying (sell index < buy index) — the single forward pass enforces this naturally, but explicit two-pointer approaches can get confused.
- Returning a negative profit instead of 0 when the array is strictly decreasing.
- Not handling a single-element prices array — profit is 0 by definition.
Follow-up questions
An interviewer at DRW may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions; a simple greedy sum of all positive daily deltas.
- Best Time to Buy and Sell Stock with Transaction Fee (LC 714) — model transaction cost as a probability-weighted drag on expected return.
- What is the expected profit if buy and sell days are chosen uniformly at random?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is this a 'trading question in disguise' at DRW?
DRW's core business is finding optimal entry and exit points. The algorithm you write here — track the minimum seen, compute margin — is structurally identical to how a simple trend-following strategy evaluates historical price series.
Can you solve this with dynamic programming?
Yes: dp[i] = max profit on day i = max(dp[i-1], prices[i] − minPrice). But the explicit DP table is unnecessary — the one-pass approach is its space-optimized equivalent.
Why O(1) space specifically matters at DRW?
High-frequency systems process arrays that may not fit in cache. An O(n) auxiliary structure causes cache misses; the running-state approach keeps everything in CPU registers.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other DRW interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →