Skip to main content

121. Best Time to Buy and Sell Stock

easyAsked at AMD

Find the maximum profit from a single buy-sell stock transaction. AMD tests this to check greedy single-pass thinking — the same left-to-right minimum-tracking technique applies when scanning performance counters or telemetry streams for the best baseline vs peak delta.

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

Source citations

Public interview reports confirming this problem appears in AMD loops.

  • Glassdoor (2026-Q1)AMD SWE candidates report this as a common greedy warm-up in phone screen rounds.
  • Blind (2025-10)Listed in AMD onsite preparation threads as a standard easy-tier greedy problem.

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), 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. Greedy single pass

Track the running minimum price seen so far and the running maximum profit. For each price, update profit as max(profit, price - minPrice), then update minPrice.

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;
    } else if (price - minPrice > maxProfit) {
      maxProfit = price - minPrice;
    }
  }
  return maxProfit;
}

Tradeoff: O(n) time, O(1) space. Optimal. The insight is that the best buy day for any given sell day is always the minimum price to its left.

AMD-specific tips

AMD performance engineers scan telemetry arrays for max-delta patterns (minimum power state vs peak throughput). Framing this as 'tracking a running minimum while computing the best gap so far' connects directly to that domain. When presenting the solution, articulate why you don't need to go back: once you see a lower price, any future sell profits more relative to that lower buy — so you never need the old minimum again.

Common mistakes

  • Returning a negative profit — if prices never rise, profit is 0, not negative.
  • Updating minPrice after computing profit for the same day — you can't buy and sell on the same day.
  • Using O(n^2) to check every pair — always ask 'can I track a running state?' before resorting to nested loops.
  • Initializing minPrice to 0 instead of Infinity — this causes wrong results when all prices are positive.

Follow-up questions

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

  • Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions.
  • Best Time to Buy and Sell Stock with Cooldown (LC 309) — one day cooldown after selling.
  • How does this pattern apply to finding the maximum amplitude in a performance-counter time series?

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 initialize minPrice to Infinity?

Any real price will be less than Infinity, so the first element always becomes the initial minimum. Initializing to 0 would incorrectly treat 0 as a possible buy price even when prices[0] > 0.

Does the order of the if-else matter?

Yes. Check for a new minimum first. If the current price is lower than minPrice, you wouldn't sell — you'd update your buy candidate instead.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →