Skip to main content

121. Best Time to Buy and Sell Stock

easyAsked at Twilio

Best Time to Buy and Sell Stock is Twilio's running-min phone-screen probe: given an array of daily prices, find the maximum profit from a single buy and a later sell. The grading bar is whether you produce the linear single-pass solution, not the O(n^2) double loop.

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

Source citations

Public interview reports confirming this problem appears in Twilio loops.

  • Glassdoor (2026-Q1)Reported in Twilio phone-screen sessions for SWE-1 candidates.
  • LeetCode Discuss (2025-12)Listed in Twilio coding-screen reports as a common warm-up.

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 = 6 - 1 = 5.

Example 2

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

Explanation: No transaction yields profit, max profit = 0.

Approaches

1. Brute-force double loop

For every buy day i, check every later sell day j > i and track the maximum profit.

Time
O(n^2)
Space
O(1)
function maxProfitBrute(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: At n = 10^5 this is 10^10 ops — TLE territory. Twilio interviewers want you to name this then immediately reach for the running-min insight.

2. Single-pass running minimum (optimal)

Track the minimum price seen so far. At each day, the best profit ending today is price - runningMin.

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

Tradeoff: One pass, O(1) memory. The insight: you only need the min of the prefix and the current price — anything earlier can't influence today's optimal sell. This is the foundational pattern for the whole 'best time' family (LC 121, 122, 123, 188, 309, 714).

Twilio-specific tips

Twilio reviewers grade the running-min insight, not the code itself. Mention DP framing (state: min price so far + best profit so far) even though the code is just two variables — that signals you can extend to the harder follow-ups (LC 122 unlimited transactions, LC 123 at most two transactions). Stating 'this is the base case for a DP family' is a strong signal.

Common mistakes

  • Selling on the same day you buy — the problem requires a strictly LATER day.
  • Returning a negative profit when no positive transaction exists — must return 0, not max(losses).
  • Computing minPrice from a separate first pass instead of folding into the single pass.

Follow-up questions

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

  • What if you could buy and sell multiple times (LC 122)? (Sum all positive (price[i] - price[i-1]) deltas.)
  • What if you had a transaction cooldown (LC 309)? (DP state with three actions: hold, sold, rest.)
  • What if there was a per-transaction fee (LC 714)? (Same DP, deduct fee on sell transitions.)

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 the running-min approach work?

Because the optimal sell day depends only on the minimum buy price BEFORE it. Any candidate buy day other than the running min can't beat the running min on the same sell day.

Is this dynamic programming?

It is — the state is (minPriceSoFar, maxProfitSoFar) and the transition is constant time. We just compress the DP table to two scalars because the recurrence only looks at the immediate prior state.

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

Practice these live with InterviewChamp.AI →