Back to questions

Max Profit Trading Stocks [Google Python Interview Question]


You are given a list of stock , where the stock's price on the ithi^{th} day is stored as the ithi^{th} element of the prices list.

You want to maximize your profit trading the stock, but are only allowed to buy the stock once and sell it once on a future day.

Write a function called which takes in this list of stock prices, and computes the max profit possible. Return if you can't make any profit.

Example 1:

Input: prices = [9, 1, 3, 6, 4, 8, 3, 5, 5] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 6 (price = 8), profit=81=7profit = 8 - 1 = 7.

Note that buying on day 2 and selling on day 1 is not allowed because you have to buy the stock BEFORE you can sell it (unless your a time-traveller in which case just trade bitcoin).

Example 2:

Input: prices = [6, 4, 3, 3, 2] Output: 0 Explanation: In this case, no trades should be made since the stock is tanking like a brick. The max profit here is 0.