121. Best Time to Buy and Sell Stock
easyAsked at BroadcomFind the maximum profit from a single buy-sell window in a price series. Broadcom asks this to see whether you can recognise a sliding-minimum scan — the same algorithmic pattern used in latency-window analysis for network performance monitoring.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Broadcom loops.
- Glassdoor (2025-12)— Reported in Broadcom SWE infrastructure interview notes as an array-scan warm-up.
- Blind (2025-08)— Broadcom candidates mention this problem as a typical first question in the onsite coding round.
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^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 = 6-1 = 5.
Example 2
prices = [7,6,4,3,1]0Explanation: Prices only decrease. No profitable transaction is possible.
Approaches
1. Brute force (nested loops)
Try every (buy, sell) pair and track the maximum profit.
- Time
- O(n²)
- Space
- O(1)
function maxProfit(prices) {
let max = 0;
for (let i = 0; i < prices.length; i++) {
for (let j = i + 1; j < prices.length; j++) {
max = Math.max(max, prices[j] - prices[i]);
}
}
return max;
}Tradeoff: O(n²) — acceptable to mention as a baseline but never the final answer at Broadcom.
2. One-pass sliding minimum
Track the minimum price seen so far. At each step, compute profit as prices[i] − minPrice and update the global maximum.
- Time
- O(n)
- Space
- O(1)
function maxProfit(prices) {
let minPrice = Infinity;
let maxProfit = 0;
for (const price of prices) {
if (price < minPrice) {
minPrice = price;
} else {
maxProfit = Math.max(maxProfit, price - minPrice);
}
}
return maxProfit;
}Tradeoff: O(n) time, O(1) space. Single pass — the correct answer. The sliding-minimum technique generalises to time-series analysis in network telemetry dashboards.
Broadcom-specific tips
When presenting the O(n) solution at Broadcom, connect it to domain knowledge: 'This is analogous to finding the minimum round-trip latency in a packet-loss window — you scan once and keep track of the best baseline.' Broadcom interviewers appreciate candidates who can bridge algorithm patterns to real networking or telemetry problems.
Common mistakes
- Initialising minPrice to prices[0] but not handling an empty array check first.
- Trying to sell before buying — always ensure sell day > buy day (the one-pass approach handles this implicitly).
- Returning a negative profit instead of 0 when prices only decrease.
- Using a two-pointer approach that breaks when the minimum is at the last position.
Follow-up questions
An interviewer at Broadcom may pivot to one of these next:
- What if you can make at most k transactions (LC 188)?
- What if you can make unlimited transactions (LC 122)?
- How would you find the actual buy and sell days, not just the profit?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why initialise minPrice to Infinity?
It ensures that the first price encountered always updates minPrice correctly, regardless of the actual price range.
Does this work if all prices are the same?
Yes — profit stays 0 because price − minPrice = 0 on every step.
Can I use a deque / monotonic stack instead?
Yes, but it is overkill here since you only need one transaction. Mention it as a lead-in to the generalised multi-transaction variant.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Broadcom interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →