Coding

Best Time to Buy and Sell Stock

You have access to an array where each element represents the price of a specific stock on a given day. Your objective is to maximize profit by selecting one day to purchase the stock and a different, future day to sell it. Determine and return the maximum profit achievable. If it's impossible to generate a profit, return 0.

Input: prices = [9,3,8,2,7,5]

Output: 5

Explanation: Purchase on day 2 (price = 3) and sell on day 5 (price = 7) for a profit of 7 - 3 = 4.

Was asked at

Practice this question with AI

First session is free - no credit card required.

Go Premium

More interviews, more skills, more success.

Community Answers

1 answer from the community

Expert Answer

Unlock Full Access

Answer any un-answered question from our database to unlock all community answers for 30 days.

Give & Take

Practice More Questions

3 answers published. Last candidate submission on Oct 19 2026, 4:12pm PDT. Interview question asked to Production Engineers, Engineering Managers, Machine Learning Engineers and other roles interviewing at Shopee, Stripe, Plaid and other companies. Original question asked: Best Time to Buy and Sell Stock. Question last reported by a candidate interviewing at HomeAway for a Machine Learning Engineer interview on Oct 21 2026, 8:33am PDT.