Compute max profit from single stock trade
MetaSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to design efficient array algorithms, reason about time and space complexity constraints, and handle edge cases related to single-transaction profit calculation.
Given an array prices where prices[i] is the stock price on day i, compute the maximum profit achievable with at most one transaction (one buy and one sell, buy before sell). Return 0 if no profit is possible. Aim for O(n) time and O(
- extra space. Explain the algorithm, prove correctness, and analyze time/space complexity. Optionally, also return the buy and sell indices.
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.