Start by clarifying the problem constraints (e.g., array size, price range) and edge cases (empty array, decreasing prices). Then explain a one-pass solution that tracks the minimum price seen so far and computes the maximum profit at each step, achieving O(n) time and O(1) space. Finally, walk through a small example to demonstrate correctness.
Pro tip: Mention that this is a classic 'best time to buy and sell stock' problem and that the one-pass approach is optimal; also note that returning 0 for no profit is a common edge case that interviewers expect you to handle explicitly.
Ask about input constraints (e.g., array length, price range) and confirm that a single buy-sell transaction is required. Discuss edge cases like empty array, single element, or strictly decreasing prices.
Briefly mention the O(n^2) solution of checking all pairs of buy and sell days to establish a baseline, then explain why it's inefficient for large inputs.
Describe iterating through the array while keeping track of the minimum price seen so far and the maximum profit. At each day, update the minimum and compute potential profit if selling today.
State that the algorithm runs in O(n) time and O(1) space. Explain why it correctly finds the maximum profit by considering each possible selling day with the lowest preceding buying day.
Use a small array (e.g., [7,1,5,3,6,4]) to trace the algorithm and show the profit calculation. Explicitly mention returning 0 when no profit is possible.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.