Skip to main content

17. Best Time to Buy and Sell Stock

easyAsked at Reddit

Find the max profit from one buy + one sell of a stock-price array. Reddit uses this as a streaming-window template — the same single-pass shape used to find the best upvote burst within a post's lifetime.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit data-platform phone screen.
  • LeetCode Discuss (2025-10)Reported as a common warmup for ranking/streaming questions.

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

Example 2

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

Explanation: No profitable trade exists.

Approaches

1. All pairs

Nested loop: for every (buy, sell), compute profit.

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: Quadratic. TLE for 10^5 input.

2. Single-pass min-so-far (optimal)

Track the lowest price seen so far. At each day, candidate profit = today's price - min so far.

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

Tradeoff: One pass, O(1) memory. Reddit's hot-burst detector uses the same min-tracker over a windowed timeline.

Reddit-specific tips

Reddit interviewers grade on the invariant: 'at every step, the best profit so far assumes we sold today.' Bonus signal: relate this to their hot-score logic — track the minimum upvote count in a window, then find the best upvote burst since.

Common mistakes

  • Returning a negative profit instead of 0 (must clamp).
  • Initializing best to -Infinity (the problem says return 0 if no profit).
  • Using two separate passes when one suffices.

Follow-up questions

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

  • II (LC 122): multiple transactions — greedy sum of positive deltas.
  • III (LC 123): exactly 2 transactions — DP.
  • IV (LC 188): at most k transactions — 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 does this not need DP?

There's only one transaction; the best 'buy' is always the running minimum. DP becomes necessary when k > 1.

Does the buy-sell constraint matter?

Yes — sell must be strictly after buy. The single-pass naturally enforces this because we only consider sells after a buy candidate.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →