0% found this document useful (0 votes)
175 views

Apple Stocks (Practice Interview Question) - Interview Cake

The document describes an efficient function to find the maximum profit that could be made from buying and selling a single share of stock once. It provides an example stock prices array and maximum profit. It then breaks down the brute force approach of comparing all pairs of buy/sell times which runs in O(n^2) time. It hints that a better solution is possible that avoids negative profits and has better time complexity.

Uploaded by

iline
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views

Apple Stocks (Practice Interview Question) - Interview Cake

The document describes an efficient function to find the maximum profit that could be made from buying and selling a single share of stock once. It provides an example stock prices array and maximum profit. It then breaks down the brute force approach of comparing all pairs of buy/sell times which runs in O(n^2) time. It hints that a better solution is possible that avoids negative profits and has better time complexity.

Uploaded by

iline
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Interview Cake

Writing programming interview questions hasn't


made me rich yet ... so I might give up and start
trading Apple stocks all day instead.
First, I wanna know how much money I could have made yesterday if I'd been trading
Apple stocks all day.

So I grabbed Apple's stock prices from yesterday and put them in an array called
stockPrices, where:

The indices are the time (in minutes) past trade opening time, which was
9:30am local time.
The values are the price (in US dollars) of one share of Apple stock at that
time.

So if the stock cost $500 at 10:30am, that means stockPrices[60] = 500.

Write an ef cient function that takes stockPrices and returns the best pro t I could
have made from one purchase and one sale of one share of Apple stock
yesterday.

For example:

JavaScript
const stockPrices = [10, 7, 5, 8, 11, 9];

getMaxProfit(stockPrices);
// Returns 6 (buying for $5 and selling for $11)

No "shorting"—you need to buy before you can sell. Also, you can't buy and sell in the
same time step—at least 1 minute has to pass.

Breakdown
To start, try writing an example value for stockPrices and nding the maximum pro t "by hand."
What's your process for guring out the maximum pro t?

The brute force↴ approach would be to try every pair of times (treating the earlier time as the buy
time and the later time as the sell time) and see which one is higher.

JavaScript
function getMaxProfit(stockPrices) {
let maxProfit = 0;

// Go through every time


for (let outerTime = 0; outerTime < stockPrices.length; outerTime++) {

// For each time, go through every other time

for (let innerTime = 0; innerTime < stockPrices.length; innerTime++) {

// For each pair, find the earlier and later times

const earlierTime = Math.min(outerTime, innerTime);

const laterTime = Math.max(outerTime, innerTime);

// And use those to find the earlier and later prices

const earlierPrice = stockPrices[earlierTime];

const laterPrice = stockPrices[laterTime];

// See what our profit would be if we bought at the

// min price and sold at the current price


const potentialProfit = laterPrice - earlierPrice;

// Update maxProfit if we can do better

maxProfit = Math.max(maxProfit, potentialProfit);

return maxProfit;

But that will take O(n2 ) time,↴ since we have two nested loops—for every time, we're going
through every other time. Also, it's not correct: we won't ever report a negative pro t! Can we do
better?

Want more coding interview help?


Check out interviewcake.com for more advice, guides, and practice questions.

You might also like