Skip to main content

121. Best Time to Buy and Sell Stock

easyAsked at Atlassian

Best Time to Buy and Sell Stock is an Atlassian warm-up that tests whether you notice the linear single-pass pattern. Given an array of daily prices, return the maximum profit from one buy then one sell. The trap is brute-forcing every (buy, sell) pair when one pass suffices.

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

Source citations

Public interview reports confirming this problem appears in Atlassian loops.

  • Glassdoor (2026-Q1)Atlassian SWE-I phone-screen reports cite Best Time to Buy/Sell Stock as a recurring warm-up.
  • Blind (2025-12)Atlassian interview threads list this as a 10-minute opener before the harder DP variants (II, III, IV).

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) and sell on day 5 (price=6), profit=5.

Example 2

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

Explanation: Prices strictly decreasing; no transaction is profitable.

Approaches

1. Brute O(n^2)

For every (i, j) with i < j, track max(prices[j] - prices[i]).

Time
O(n^2)
Space
O(1)
function maxProfitBrute(prices) {
  let max = 0;
  for (let i = 0; i < prices.length; i++) {
    for (let j = i + 1; j < prices.length; j++) {
      const profit = prices[j] - prices[i];
      if (profit > max) max = profit;
    }
  }
  return max;
}

Tradeoff: Times out at n=10^5 (10^10 ops). Useful only to motivate the single pass.

2. Single-pass running minimum (optimal)

Walk left-to-right tracking the minimum price seen so far. At each price, candidate profit = price - minSoFar. Track max candidate.

Time
O(n)
Space
O(1)
function maxProfit(prices) {
  let min = prices[0];
  let max = 0;
  for (let i = 1; i < prices.length; i++) {
    if (prices[i] < min) min = prices[i];
    else if (prices[i] - min > max) max = prices[i] - min;
  }
  return max;
}

Tradeoff: The Atlassian-canonical answer. The if/else (instead of two separate ifs) is the micro-optimization to mention: if today's price is a new min, no candidate profit can be computed today, so skip the second comparison.

Atlassian-specific tips

Atlassian uses this as a 10-minute opener. State the pattern out loud: 'I'll track the minimum price seen so far and at each day compute the candidate profit if I sold today.' Then write. After you finish, expect 'now what if you could do multiple buy/sells?' (LeetCode 122) or 'at most 2 transactions' (LeetCode 123). Don't get stuck explaining the simple version — they're just clearing throat before the real DP problem.

Common mistakes

  • Initializing max to prices[0] - prices[0] = 0 but starting the loop at i=0 — you compare prices[0] - prices[0] = 0 which is harmless but indicates muddled control flow. Start at i=1.
  • Returning prices[end] - prices[start] when start > end means buying after selling — must enforce i < j.
  • Forgetting to return 0 when prices is strictly decreasing — the algorithm handles it (max stays 0), but candidates sometimes return a negative number.

Follow-up questions

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

  • Best Time to Buy and Sell Stock II (LeetCode 122) — unlimited transactions; sum positive day-to-day deltas.
  • Best Time to Buy and Sell Stock III (LeetCode 123) — at most 2 transactions; DP with forward + backward arrays.
  • Best Time with cooldown / transaction fee — classic state-machine 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

Is this DP?

It's the simplest case of stock-DP. Tracking minSoFar IS the state (cost of best buy so far). The 'real' DP framing helps when you generalize to 2+ transactions; for this problem the linear scan is the natural fit.

Does Atlassian still ask this?

Yes as a SWE-I phone-screen opener. Almost never standalone at SWE-II+; expect a follow-up to one of the harder variants in those rounds.

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 Atlassian interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →