Skip to main content

16. Best Time to Buy and Sell Stock

easyAsked at Salesforce

Find the maximum profit from a single buy-sell of a stock given daily prices. Salesforce uses this to test running-minimum tracking, the same pattern in their forecasting and quota-attainment dashboards.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Sales Cloud forecasting team uses this pattern in pipeline progression analysis.
  • Blind (2025-12)Recurring on Salesforce L4 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 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 = 5.

Example 2

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

Explanation: No transaction is done; max profit = 0.

Approaches

1. Brute force pairs

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

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 constraint of 10^5 elements. Salesforce wants O(n).

2. Single pass with running minimum

Track the minimum price seen so far; for each day, compute profit if we sold today, update best.

Time
O(n)
Space
O(1)
function maxProfit(prices) {
  let minPrice = Infinity, best = 0;
  for (const p of prices) {
    minPrice = Math.min(minPrice, p);
    best = Math.max(best, p - minPrice);
  }
  return best;
}

Tradeoff: O(n) time, O(1) space. The order of updates matters: update minPrice first so today's profit uses the historical min (today's buy is not allowed).

Salesforce-specific tips

Salesforce explicitly grades this on whether you spot the 'running minimum' invariant immediately. Bonus signal: explain that updating minPrice BEFORE computing today's profit naturally handles the constraint that buy must precede sell (since today's price is included in the min, today-today gives 0 profit, which is the correct floor).

Common mistakes

  • Initializing best to prices[0] instead of 0 — gives wrong answer when prices are monotonically decreasing.
  • Updating best before minPrice — incorrectly allows same-day buy-sell.
  • Trying to find min and max separately — fails on cases like [3, 1, 4, 1, 5] where the max comes before the second min.

Follow-up questions

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

  • Best Time to Buy and Sell Stock II (LC 122) — multiple transactions.
  • Best Time to Buy and Sell Stock III (LC 123) — at most two transactions.
  • Best Time to Buy and Sell Stock 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 initialize best to 0, not -Infinity?

Because the problem allows skipping the transaction entirely. If all prices are descending, the answer is 0 (no transaction), not a negative number.

What's the difference between LC 121 and LC 122?

121 = exactly one transaction. 122 = unlimited transactions. The unlimited version uses a different greedy (sum every up-step).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →