Skip to main content

16. Best Time to Buy and Sell Stock

easyAsked at Plaid

Find the maximum profit from buying and selling a stock once. Plaid asks this because computing max-drawdown style metrics over a price series is a familiar shape for their balance-history endpoints.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid backend screen.
  • Blind (2026)Plaid OA — variant on transaction-history max-gain.

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 at 1, sell at 6, profit 5.

Example 2

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

Explanation: Prices only fall — no profit possible.

Approaches

1. Check every (buy, sell) pair

Nested loops over all valid day pairs.

Time
O(n^2)
Space
O(1)
function maxProfit(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: TLE on the 10^5 bound. Only useful as the warm-up before optimizing.

2. Single-pass min-so-far

Walk once. Track the lowest price seen so far. At each day compute profit if sold today, update best.

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

Tradeoff: Linear, constant space, single pass. The min-so-far + best-so-far is the canonical 'rolling state' pattern for time-series.

Plaid-specific tips

Plaid grades this on whether you recognize the rolling-state pattern. Bonus signal: connect it to their use case — 'this is the same shape we'd use to compute a peak-to-trough drawdown over a balance series.' Be precise about whether sell must come strictly after buy (yes, here).

Common mistakes

  • Allowing sell == buy day (no profit possible) — strict 'after' is required.
  • Initializing best to -Infinity — should be 0 because no transaction yields 0, not negative.
  • Tracking max-so-far instead of min-so-far — backward.

Follow-up questions

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

  • Unlimited transactions (LC 122).
  • At most two transactions (LC 123) — hard.
  • With a cooldown day (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 init best to 0?

Because not transacting at all yields 0, and you're allowed to not transact. -Infinity would be wrong.

Why min-so-far and not min-overall?

Min-overall might be from a day after the current — that wouldn't be a valid buy. Min-so-far ensures the buy day precedes the candidate sell day.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →