How do you maximize profit in a single stock transaction scenario?
Candidates must find the maximum profit from buying and selling a stock once. This tests greedy algorithm application and tracking minimums.
Why Interviewers Ask This
This is a standard problem to evaluate a candidate's ability to apply greedy strategies. Interviewers look for the insight that keeping track of the minimum price seen so far allows calculating potential profit in a single pass. It demonstrates logical reasoning and optimization skills.
How to Answer This Question
Explain traversing the price list while maintaining the minimum price encountered so far. Calculate the profit for the current day by subtracting the minimum price from the current price. Update the maximum profit if the current profit is higher. Conclude with O(n) time complexity.
Key Points to Cover
- Track minimum price
- Calculate daily profit
- Update maximum profit
- Single pass traversal
Sample Answer
To maximize profit with one transaction, I iterate through the prices while keeping track of the lowest price seen so far. For each day, I calculate the potential profit by subtracting the minimum price from the current…
Common Mistakes to Avoid
- Trying all pairs (O(n^2))
- Ignoring the buy-before-sell constraint
- Not updating minimum correctly
Sound confident on this question in 5 minutes
Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.