Skip to main content

121. Best Time to Buy and Sell Stock

easyAsked at Citadel

This problem is more at home at Citadel than anywhere else — it's literally what the firm does. Interviewers use it to see whether you recognize the 'running minimum' pattern and can articulate why a single-pass scan is sufficient. Expect follow-ups on multiple transactions and transaction costs.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Citadel loops.

  • Glassdoor (2026-Q1)Citadel SWE and quant SWE candidates frequently report Best Time to Buy and Sell Stock variants across phone and on-site rounds.
  • Blind (2025-11)Citadel threads note the stock series (LC 121, 122, 123) appears with increasing difficulty across interview rounds.

Problem

You are given an array prices where prices[i] is the price of a given stock on the i-th day. You want to maximize your profit by choosing a single day to buy one share of stock and 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^5
  • 0 <= prices[i] <= 10^4

Examples

Example 1

Input
prices = [7,1,5,3,6,4]
Output
5

Explanation: Buy on day 2 (price=1), sell on day 5 (price=6). Profit = 6-1 = 5.

Example 2

Input
prices = [7,6,4,3,1]
Output
0

Explanation: Prices only decrease — no profitable transaction possible.

Approaches

1. Single pass with running minimum

Track the minimum price seen so far and the maximum profit achievable at each step. Update both in one pass.

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; // new lowest buy point
    } else if (price - minPrice > maxProfit) {
      maxProfit = price - minPrice; // new best profit
    }
  }
  return maxProfit;
}

Tradeoff: O(n) time, O(1) space. A single scan is all you need because you must buy before selling — any candidate sell price is only evaluated against the minimum price seen before it. This is the canonical answer.

Citadel-specific tips

At Citadel, frame the solution in terms they use daily: 'I maintain a running minimum bid price and compute the spread at each ask price. The global maximum spread is the answer.' This language maps directly to order-book thinking. Expect the follow-up: 'What if you can make at most k transactions?' (LC 188 — DP with k states). Also be ready for: 'What if there is a transaction fee?' (LC 714). Knowing the family of stock problems cold separates SWE candidates at Citadel.

Common mistakes

  • Trying to use max − min directly — this ignores the constraint that buy must come before sell.
  • Initializing minPrice to prices[0] but then starting the loop at index 0 — you'd check day 0 vs day 0, which is a same-day trade (profit = 0 always).
  • Returning a negative number instead of 0 when no profit is possible.
  • Attempting an O(n²) scan of all pairs — interviewers at Citadel will stop you before you finish writing it.

Follow-up questions

An interviewer at Citadel may pivot to one of these next:

  • Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions; greedy on every upward slope.
  • Best Time to Buy and Sell Stock with Transaction Fee (LC 714) — DP with fee subtracted at sell.
  • Best Time to Buy and Sell Stock IV (LC 188) — at most k transactions; DP with k states.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is the greedy running-minimum approach correct?

Because sell must come after buy, the optimal buy price for any sell day is the minimum price in all previous days. Tracking it as a running minimum captures this exactly without looking back.

Does this extend to two transactions?

Yes, but you need two states: the best profit after the first sell, and the best profit after the second sell. LC 123 covers this with a four-variable DP.

What does 'profit = 0 minimum' represent in the real world?

Not trading at all. In financial models this is equivalent to holding cash — the floor return is zero.

Practice these live with InterviewChamp.AI

Drill Best Time to Buy and Sell Stock and other Citadel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →