Skip to main content

121. Best Time to Buy and Sell Stock

easyAsked at Goldman Sachs

Given an array of stock prices by day, find the maximum profit from a single buy-then-sell. As an investment bank, Goldman Sachs uses this exact problem framing in nearly every SWE and Strats loop — the answer is a single-pass O(n) with running minimum.

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

Source citations

Public interview reports confirming this problem appears in Goldman Sachs loops.

  • Glassdoor (2026-Q1)Goldman Sachs SWE and Strats reports overwhelmingly cite Best Time to Buy and Sell Stock as the single most-asked problem in their loops.
  • LeetCode Discuss (2025-12)This problem is the #1 most-asked on LeetCode's Goldman Sachs company tag.
  • Blind (2025-11)Multiple Goldman Sachs candidates report this on the HackerRank assessment AND the virtual onsite.

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 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 possible.

Approaches

1. Brute-force every pair

Try every (buy, sell) pair where buy < sell.

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: Correct but O(n^2) — times out at n = 10^5. Mention briefly, name the cost, move to single-pass.

2. Running minimum (optimal)

Track the minimum price seen so far. For each day, compute price - minSoFar and update best.

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

Tradeoff: Single pass, constant space. The invariant — minPrice always holds the best 'buy' available before today — is the part Goldman wants you to articulate.

Goldman Sachs-specific tips

Goldman Sachs interviewers will follow this with 'what if you could make multiple transactions' (LC #122), then 'at most two transactions' (LC #123). Each tier requires a different state-machine. Be ready to extend on the fly. The phrase Goldman wants to hear: 'I'll track the running minimum and the best profit-so-far in a single pass'. That sentence alone gets you past the rubric line.

Common mistakes

  • Returning negative profit when the array is monotonically decreasing — the problem says return 0.
  • Iterating in O(n^2) on the assumption it'll pass — Goldman tests with n = 10^5 and the brute force times out.
  • Confusing this with 'max difference' (which allows buy > sell index) — here buy must come before sell.

Follow-up questions

An interviewer at Goldman Sachs may pivot to one of these next:

  • What if you can make unlimited transactions? (LC #122 — sum every positive day-to-day delta.)
  • What if you can make at most k transactions? (LC #188 — DP with k states.)
  • What if there's a transaction fee per trade? (LC #714.)

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 does Goldman ask this so often?

Because it's literally the firm's business — buy low, sell high — and the algorithmic version maps to running-minimum-tracking which generalizes to every 'best deal seen so far' problem in trading systems. The phrasing also tests whether you understand the temporal constraint (sell must come after buy).

Why is the if-else more efficient than two separate ifs?

If price < minPrice, we just lowered the floor — there's no point checking whether (price - minPrice) is a new best because price === minPrice gives a profit of 0. The else-if saves one comparison per iteration. Constant-factor speedup, but Goldman notices.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →