Maximize one stock trade profit
MetaMachine Learning Engineer · OnsiteLast updated
This question evaluates algorithm design skills for single-pass array processing, streaming data handling, and time/space complexity analysis within the Coding & Algorithms domain.
You're given an array prices of length n where prices[i] is the stock price on day i. You may complete at most one transaction (buy once, then sell later). Return the maximum possible profit; if no profit is possible, return 0. Constraints: 1 <= n <= 100000, 0 <= prices[i] <= 1e9. Requirements: O(n) time and O(
- extra space. Provide the algorithm, prove correctness, and analyze complexity. Follow-up: adapt your solution to a streaming setting where prices arrive one by one and you must output the current best profit after each new price.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.