Skip to main content

16. Best Time to Buy and Sell Stock

easyAsked at Snowflake

Short answer

  • Best approach: Track running min. Walk once. Keep track of min price seen so far. At each day, candidate profit = today - min so far. Update best.
  • Time O(n), space O(1)
  • How often Snowflake asks it: frequently asked · Verified

Find the max profit from one buy and one sell. Snowflake asks this to test single-pass min-tracking — the same one-pass min/max aggregation used to build column-chunk statistics during data ingestion.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2026-Q1)— Snowflake ingestion-team screens use this as the streaming-aggregate warm-up.
  • LeetCode Discuss (2025-12)— Recurring at Snowflake new-grad onsites.

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: No transactions are done; max profit = 0.

Approaches

1. Brute force every pair

For each i, try every j > i; track max 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. Mention only to reject.

2. Track running min (optimal)

Walk once. Keep track of min price seen so far. At each day, candidate profit = today - min so far. Update best.

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: Single pass, O(1) state. This is what Snowflake's micro-partition min stats look like under the hood — one streaming min per column.

Snowflake-specific tips

Snowflake interviewers want the streaming-min framing — running min so far, look at current. Bonus signal: connect to micro-partition statistics — each Snowflake micro-partition stores min/max per column, computed by exactly this streaming pass during ingestion.

Common mistakes

  • Returning a negative profit when no positive trade exists — should be 0.
  • Updating min and best in the same branch, missing the case where today is a new min.
  • Trying to do divide-and-conquer or DP — overkill for this problem.

Follow-up questions

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

  • Multiple transactions allowed (LC 122).
  • At most 2 transactions (LC 123).
  • How does Snowflake build min/max stats per column-chunk during ingestion?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why track min separately from best?

Best (profit) depends on the cheapest preceding price. Tracking min lets you compute profit in O(1) at each step without a nested loop.

How does this relate to micro-partition stats?

When Snowflake ingests rows into a micro-partition, it runs a single streaming pass to compute min, max, null-count, and a few HLL sketches per column. The pattern matches this problem exactly.

Originally published on InterviewChamp.AI

Companies that also ask Best Time to Buy and Sell Stock