121. Best Time to Buy and Sell Stock
easyAsked at AkamaiFind the maximum profit from one buy and one sell transaction in a price array. Akamai frames this as a running-minimum scan — the same streaming pattern used to compute latency baselines across billions of edge-server events in a single pass.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Akamai loops.
- Glassdoor (2026-Q1)— Cited in Akamai software engineer interview reports as a common dynamic programming warm-up.
- Blind (2025-10)— Akamai threads confirm this appears in phone screens as a one-pass array reasoning check.
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 = 5.
Example 2
prices = [7,6,4,3,1]0Explanation: Prices only decrease; no profitable transaction possible.
Approaches
1. One-pass running minimum
Track the minimum price seen so far. At each day, compute the profit if we sold today (price − minSoFar) and update the global maximum. No extra space needed.
- 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 if (price - minPrice > maxProfit) {
maxProfit = price - minPrice;
}
}
return maxProfit;
}Tradeoff: Single pass, O(n) time, O(1) space. This is the expected solution. It is equivalent to the maximum subarray problem on the differences array — a connection Akamai interviewers may probe.
Akamai-specific tips
Akamai values the streaming framing: 'We can't look ahead, so we track the cheapest buy price we've seen and greedily compute today's profit.' Then connect to scale: 'At 10^9 price ticks per day, O(1) space means we never buffer — we process in real time.' This scale commentary consistently impresses Akamai interviewers.
Common mistakes
- Returning a negative profit when prices only decrease — the minimum is 0 (no trade made).
- Initializing minPrice to prices[0] and maxProfit to negative infinity — correct but needlessly complex; Infinity and 0 are cleaner.
- Using nested loops to try all pairs — O(n²) is correctly identified but offering the O(n) improvement should be immediate.
Follow-up questions
An interviewer at Akamai may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions; how does the greedy change?
- Best Time to Buy and Sell Stock with Cooldown (LC 309) — one-day cooldown after selling.
- How would you solve this if prices arrived as a live stream rather than a complete array?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is this equivalent to maximum subarray on the differences?
If you build a diff array where diff[i] = prices[i] - prices[i-1], then the maximum profit equals the maximum subarray sum of diff (Kadane's algorithm). Both algorithms are O(n) single-pass scans.
What if all prices are equal?
maxProfit stays 0 throughout. Correct — buying and selling at the same price yields zero profit.
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Akamai interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →