Practice: Stock Price Calculation

This is a premium course, which you can purchase below.

Buy Now

This is a real interview question that comes straight from Amazon. You'll be writing this one by hand so remember your strategies.


OK grab your journal and dive in with me! I'll be solving this question by hand I urge you to do the same. I'll show you the solution near the end - but give it your best shot!

This question is from InterviewCake - thanks to Parker Phinney for permission to use. You can solve it there right now, if you like, as it’s part of the free questions.

Here’s the question:

Write an efficient function that takes stockPricesYesterday and returns the best profit I could have made from 1 purchase and 1 sale of 1 Apple stock yesterday.

Starter code:

//The values are the price in dollars of Apple stock at that time.
var stockPricesYesterday = [10, 7, 5, 8, 11, 9];

getMaxProfit(stockPricesYesterday);
// returns 6 (buying for $5 and selling for $11)