Skip to main content

11. Best Time to Buy and Sell Stock

easyAsked at Intuit

Given an array of stock prices by day, find the maximum profit from a single buy/sell transaction. Intuit asks this to test single-pass min/max tracking and to probe whether you handle financial values as integer cents instead of floats.

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

Source citations

Public interview reports confirming this problem appears in Intuit loops.

  • Glassdoor (2026-Q1)Intuit Mint team phone screen, framed around portfolio P/L calculations.
  • LeetCode Discuss (2025-10)QuickBooks SWE intern screen cited Buy/Sell Stock as warm-up.

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
  • Prices are integers representing cents to avoid float drift.

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: Prices only fall; no profitable transaction exists.

Approaches

1. Brute force double loop

Try every (buy, sell) pair where sell > buy and track the maximum difference.

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 — fine to mention but propose the single-pass version immediately.

2. Single-pass min tracking (optimal)

Walk left-to-right, tracking the minimum price seen so far. For each day, the best sell-today profit is price - minSoFar. Keep the running max.

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: Linear time, constant space. Single pass without DP overhead.

Intuit-specific tips

Intuit cares that you store prices as integer cents, not floats — if the interviewer hands you '7.25' inputs, ask whether you can convert to cents first. They also probe edge cases: an empty array (return 0), monotonically decreasing prices (return 0), and whether you can buy/sell on the same day (no — sell strictly after buy). Bonus signal: mention that this generalizes to Best Time II/III/IV with state-machine DP.

Common mistakes

  • Initializing best profit to Infinity instead of 0 — returns negative on monotonically decreasing arrays.
  • Using floats for cents — accumulates precision drift over millions of transactions.
  • Allowing buy and sell on the same day, which violates the problem constraint.

Follow-up questions

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

  • Best Time to Buy and Sell Stock II — unlimited transactions (LC 122).
  • Best Time to Buy and Sell Stock III — at most two transactions (LC 123).
  • Add a per-trade fee or cooldown day and re-derive the 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 track minSoFar instead of all previous prices?

The best buy day for any sell day is always the cheapest price before it. We don't need history; just the running minimum collapses the inner loop.

How would you handle real currency inputs?

Convert to integer cents (or 4-decimal micro-units for FX) at the API boundary. The algorithm is identical; the data type prevents IEEE-754 drift across many transactions.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →