As reported: Microsoft Senior SWE (Bengaluru, Mar 2025 drive), round 1 (coding): best time to buy and sell a stock with at most one transaction; follow-up: print the buy and sell days; second follow-up: at most two transactions.
You are given an array prices where prices[i] is a stock's price on day i. Choose one day to buy and a later day to sell. Return the maximum profit; if no profit is possible, return 0.
This is LeetCode 121 — Best Time to Buy and Sell Stock.
Example 1
prices = [7,1,5,3,6,4] -> 5 (buy on day 1 at 1, sell on day 4 at 6)
Example 2
prices = [7,6,4,3,1] -> 0
Constraints: 1 ≤ prices.length ≤ 10^5; 0 ≤ prices[i] ≤ 10^4.
Code runner format: one JSON value per line: prices (LeetCode's test-case format). Implement maxProfit(prices); the harness prints the result.
Follow-ups
- Also return the buy day and the sell day.
- Allow at most two transactions (sell before buying again).