121. Best Time to Buy and Sell Stock
easyAsked at Juniper NetworksFind the maximum profit from a single buy-sell transaction. Juniper uses this problem to test whether candidates think in a single-pass O(n) sliding minimum pattern — the same mental model used to track minimum round-trip times or minimum queue depths in network monitoring systems.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Juniper Networks loops.
- Glassdoor (2025-Q4)— Juniper SWE phone-screen reports include this as a standard greedy/array warm-up.
- Blind (2025-09)— Cited in Juniper interview prep threads as a common easy-difficulty filter question.
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 possible.
Approaches
1. Brute force — O(n²)
Try all pairs (buy day, sell day) where buy day < sell day.
- 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: Too slow for 10^5 elements. Mention it to show you understand the problem, then optimize.
2. Single pass — track minimum price
Track the minimum price seen so far. At each day, compute the profit if we sold today. Update the global maximum profit.
- 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; // found a cheaper buy day
} else {
maxProfit = Math.max(maxProfit, price - minPrice);
}
}
return maxProfit;
}Tradeoff: O(n) time, O(1) space. Classic single-pass greedy. The key insight is: the optimal sell day is always to the right of the optimal buy day, so tracking the running minimum is sufficient.
Juniper Networks-specific tips
Draw the parallel to network monitoring: if you are tracking the minimum observed latency in a sliding window to detect improvement, you use the exact same running-minimum pattern. Juniper engineers appreciate domain-relevant analogies. Also clarify up front: you cannot buy and sell on the same day, and you must buy before selling — small details that show rigor.
Common mistakes
- Allowing sell before buy — the indices must satisfy buy_day < sell_day.
- Returning a negative profit — the problem says return 0 if no profitable trade exists; initialize maxProfit to 0.
- Updating minPrice after computing profit instead of before — can incorrectly use today's price as both buy and sell.
- Using prices[0] as the initial minimum instead of Infinity — works only if the array is non-empty; Infinity is safer.
Follow-up questions
An interviewer at Juniper Networks may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions; greedy valley-to-peak summation.
- Best Time to Buy and Sell Stock with Cooldown (LC 309) — dynamic programming with state.
- How would you track max profit in a real-time data stream with sliding window constraints?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why initialize minPrice to Infinity?
It guarantees the first price always updates minPrice, handling any possible price value including 0.
Can we sell on the same day we buy?
No — profit would be 0. The loop structure (tracking min before updating maxProfit) naturally prevents this.
What if all prices are equal?
minPrice stabilizes at that price, profit is always 0, and maxProfit remains 0 — correct.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Juniper Networks interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →