← All questions
Coding
Best Time to Buy and Sell Stock
Asked at
PayPal
Swiggy
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+
- 01Set minimum price to the first seen value.
- 02Scan prices from left to right.
- 03Compute today profit against prior minimum.
- 04Update best profit and then minimum price.
- 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