● videoPractice
Practice: Stock Price Calculation

Unlock this course
Subscribe for full access to every course, or buy this one on its own.
SECTION
Practice
NEXT UP
Practice: Product of All Numbers But This
COURSE
Coding Interview Bootcamp
17 lessons
About this lesson
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:
<span class="hljs-comment">//The values are the price in dollars of Apple stock at that time.</span>
<span class="hljs-keyword">var</span> stockPricesYesterday = [<span class="hljs-number">10</span>, <span class="hljs-number">7</span>, <span class="hljs-number">5</span>, <span class="hljs-number">8</span>, <span class="hljs-number">11</span>, <span class="hljs-number">9</span>];
<span class="hljs-title function_">getMaxProfit</span>(stockPricesYesterday);
<span class="hljs-comment">// returns 6 (buying for $5 and selling for $11)</span>
Unlock Coding Interview Bootcamp
Subscribe for full access to every course, or buy this one on its own.