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

Hiring now

PayPal has 254 open roles right now

Also hiring: Swiggy

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Sr Data Scientist

    San Jose, California, United States of America

  • Senior Data Scientist

    San Jose, California, United States of America

  • Staff Data Scientist

    Chennai, Tamil Nadu, India

Or practice this question first

Next question