17. Best Time to Buy and Sell Stock
easyAsked at WorkdayGiven daily stock prices, find the maximum profit from a single buy-and-sell. Workday uses this to test running-minimum tracking — the same pattern used for finding the best fiscal-year window in compensation grading.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Workday loops.
- Glassdoor (2025)— Workday SDE1/2 phone screen — classic DP intro.
- LeetCode Discuss (2026)— Workday compensation team interview.
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^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: No transaction yields profit.
Approaches
1. Brute force pairs
Try every buy-sell pair (i, j) with i < j.
- Time
- O(n^2)
- Space
- O(1)
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. Fine for n=100, dies at n=10^5.
2. Running minimum
Track the lowest price seen so far. For each day, profit if sold today = price - min. Keep the best.
- 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. The 'else if' guards against computing profit on the same day you set the new minimum (which is zero anyway, but cleaner).
Workday-specific tips
Workday wants you to explicitly say 'I'll track the running minimum and compute the gap'. Don't over-engineer with DP arrays for state[buy/sell] — those are LC 122/123. For one transaction, the one-pass minimum is canonical.
Common mistakes
- Returning the price difference instead of the profit (could be negative).
- Initializing minSoFar to 0 — fails when all prices > 0.
- Returning -1 on no-profit instead of 0.
Follow-up questions
An interviewer at Workday may pivot to one of these next:
- Best Time to Buy and Sell Stock II (LC 122) — unlimited transactions.
- Best Time to Buy and Sell Stock III (LC 123) — at most 2 transactions.
- Cooldown variant (LC 309).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why initialize minSoFar to Infinity?
Any real price will be smaller, so the first iteration correctly sets the minimum. Starting at 0 fails if all prices are positive.
What if I'm forced to make a transaction even if it loses money?
Different problem — return the smallest loss. But the prompt explicitly allows 0 profit (no transaction).
Practice these live with InterviewChamp.AI
Drill Best Time to Buy and Sell Stock and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →