Best Time to Buy and Sell Stock

Asked atPayPalSwiggy
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Set minimum price to the first seen value.
  2. 02Scan prices from left to right.
  3. 03Compute today profit against prior minimum.
  4. 04Update best profit and then minimum price.
  5. 05Return zero when no profitable transaction exists.

Reference answer

Then expect these follow-ups

  • How does the solution change with unlimited transactions?

    Tests: constraint adaptation

  • How would you return the buy and sell indices?

    Tests: implementation extension

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question