Skip to main content

121. Best Time to Buy and Sell Stock

easyAsked at Intel

Given a price-per-day array, find the maximum profit from a single buy-then-sell pair. Intel reaches for this in SWE-II rounds to grade whether you spot the running-minimum invariant — the same trick that powers single-pass min/max tracking in streaming sensor pipelines.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Intel loops.

  • Glassdoor (2026-Q1)Intel SWE phone-screen reports cite stock-buy-sell-I as a recurring single-pass-DP warm-up.
  • Levels.fyi (2025-09)Intel SWE interview reports describe the running-min approach explicitly.
  • LeetCode discuss (2025-11)Intel-tagged in the LC company-frequency listing.

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^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) and sell on day 5 (price = 6), profit = 6-1 = 5.

Example 2

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

Explanation: No profitable transaction exists.

Approaches

1. Nested loops (brute)

For every i, scan every j > i and track max(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 — times out at n = 10^5. Useful to verbalize as the obvious approach, then immediately replace.

2. Running minimum (optimal)

Walk left-to-right. Track the lowest price seen so far. For each day, the best profit if you sell today is today - minSoFar. Return the max over all days.

Time
O(n)
Space
O(1)
function maxProfit(prices) {
  let minSoFar = Infinity;
  let best = 0;
  for (const p of prices) {
    if (p < minSoFar) minSoFar = p;
    else if (p - minSoFar > best) best = p - minSoFar;
  }
  return best;
}

Tradeoff: Single pass, branch-friendly, cache-friendly, constant space. The reason it works: a sell on day i is optimal only if you bought at the min before day i — no need to remember which specific earlier day, just the min.

Intel-specific tips

Intel interviewers want the invariant articulated: 'I don't need to remember which earlier day I bought; I only need to remember the cheapest day so far.' That sentence is the senior-IC signal. The else-if instead of two separate ifs is a micro-optimization Intel hardware reviewers occasionally point out — branch-predictor-friendly.

Common mistakes

  • Returning a negative max-profit when all prices are decreasing. The problem says return 0 in that case; initialize `best = 0` not `best = -Infinity`.
  • Updating min and best in the wrong order. If you update min first then compute today - min, you may use today as the buy price (zero profit). Use `if (p < minSoFar) ... else ...` so you never sell on the same day you bought.
  • Allocating a separate minLeft[] array — wastes O(n) space, never necessary.

Follow-up questions

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

  • Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions, sum every up-step.
  • Best Time to Buy and Sell Stock III (LC 123) — at most two transactions, fancier DP.
  • Best Time to Buy and Sell Stock IV (LC 188) — at most k transactions, full DP table.
  • What if you also pay a transaction fee per trade? (LC 714 — adjusted DP.)

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 running-minimum approach correct?

Any optimal sell day i has a corresponding buy day j < i. For that fixed sell day, the optimal buy is the min over [0, i-1]. So scanning sell days and using the running min covers every (buy, sell) pair without nesting.

Could I solve this with Kadane's algorithm?

Yes. The 'daily delta' array d[i] = prices[i] - prices[i-1] turns this into Kadane on d. The max subarray sum of d equals the max profit. Same Big-O, different framing — useful to mention if the interviewer asks for an alternative.

Free learning resources

Curated free links for this problem.

Companies that also ask Best Time to Buy and Sell Stock