Katkat

Maximize one stock trade profit

MetaMachine Learning Engineer · Onsite

Last 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(

  1. 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.