Skip to main content

12. Best Time to Buy and Sell Stock

easyAsked at Notion

Find the max profit from a single buy/sell of one stock. Notion likes this because it introduces the running-min trick — useful in throttling and rate-limit calculations.

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

Source citations

Public interview reports confirming this problem appears in Notion loops.

  • Glassdoor (2026-Q1)Notion uses this as the canonical array-pass warmup.
  • Blind (2025)Reported in phone screens.

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 no 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 day 2 (price=1), sell day 5 (price=6); profit=5.

Example 2

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

Explanation: No profitable transaction.

Approaches

1. Brute force all pairs

For each buy day, scan all later days for sell.

Time
O(n^2)
Space
O(1)
function maxProfit(p) {
  let best = 0;
  for (let i = 0; i < p.length; i++)
    for (let j = i+1; j < p.length; j++)
      best = Math.max(best, p[j] - p[i]);
  return best;
}

Tradeoff: Too slow for 10^5 inputs.

2. Single pass with running min (optimal)

Track the minimum price seen so far. At each day, profit candidate = today - minSeen. Update best.

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

Tradeoff: Linear and constant space. The key insight: you can decide independently which day was the historical minimum.

Notion-specific tips

Notion grades this on whether you reach for the running-min trick within 30 seconds. Bonus: discuss the variants (multiple transactions, with cooldown) to show DP fluency without being asked.

Common mistakes

  • Initializing minSoFar to prices[0] but indexing from 0 — duplicates the first comparison harmlessly but is sloppy.
  • Returning negative profit — must clamp at 0.
  • Confusing this with 'sum of all positive deltas' (that's LC 122).

Follow-up questions

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

  • Multiple transactions (LC 122).
  • At most 2 transactions (LC 123).
  • K transactions with cooldown (LC 309).

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 update minSoFar BEFORE computing profit?

Because you must buy before you sell. If today is the new min, today's profit using today's price is 0 — that's fine.

What if the array is empty?

Constraint says length>=1. With 1 element, no transaction possible, return 0.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →