16. Best Time to Buy and Sell Stock
easyAsked at VercelGiven an array of stock prices, find the max profit from a single buy-then-sell. Vercel asks this because the 'track min so far' pattern is identical to how their edge analytics finds the cheapest-cost path over a time-series of latencies.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel screen warm-up; single-pass min-tracking expected.
- Blind (2026-Q1)— Listed in Vercel platform onsite recap.
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. If you cannot achieve any profit, return 0.
Constraints
1 <= prices.length <= 10^50 <= prices[i] <= 10^4
Examples
Example 1
prices = [7,1,5,3,6,4]5Explanation: Buy on day 2 (price = 1), sell on day 5 (price = 6), profit = 5.
Example 2
prices = [7,6,4,3,1]0Explanation: No transaction is done.
Approaches
1. Brute force pair enumeration
Try every (buy, sell) pair with buy < sell; track the 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 to contrast.
2. Single-pass min-tracking (optimal)
Walk left to right. Maintain the minimum price seen so far. At each step, profit if sold today = price - min. Track the max.
- Time
- O(n)
- Space
- O(1)
function maxProfit(prices) {
let minSoFar = Infinity;
let best = 0;
for (const p of prices) {
if (p < minSoFar) minSoFar = p;
else if (p - minSoFar > best) best = p - minSoFar;
}
return best;
}Tradeoff: Single pass, O(1) memory. The if/else skips the subtraction when we just updated the min — a small but measurable optimization.
Vercel-specific tips
Vercel watches for the single-pass insight. Bonus signal: the if/else structure (we never beat the best on the same step we lower the min) and noting that this is a 1D Kadane variant — the same shape as LC 53 but with 'reset on new min' instead of 'reset on negative prefix.'
Common mistakes
- Returning the absolute difference instead of profit — produces a non-zero answer for monotonically decreasing prices.
- Initializing minSoFar to prices[0] but not handling the empty array (constraint says >= 1 so it's fine, but flag).
- Storing all prices in a sorted structure — overengineered.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Multiple transactions allowed (LC 122).
- At most two transactions (LC 123).
- k transactions (LC 188) — DP.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why if/else instead of two unconditional updates?
When we just lowered minSoFar, profit-if-sold-today is exactly 0, never a new best. Skipping the subtraction is a tiny win and signals you noticed.
How does this relate to Kadane's algorithm?
It's the same shape: maintain a running 'best so far' and a 'current baseline.' Kadane resets when the running sum goes negative; this resets when a new low appears.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →