Interview Questions

Stock maximum profit

The question is as follows:

You will be given a list of stock prices for a given day and your goal is to return the maximum profit that could have been made by buying a stock at the given price and then selling the stock later on. For example if the input is: [45, 24, 35, 31, 40, 38, 11] then your program should return 16 because if you bought the stock at $24 and sold it at $40, a profit of $16 was made and this is the largest profit that could be made. If no profit could have been made, return -1.

You need to be a premium member to see the rest of this question and code.

With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

- 200+ Coding Challenges
- Mock Interview Questions
- 500,000+ Code Solutions
- Algorithm Tutorials
- Interview Prep Courses

mrdaniel
published this on 1/3/16 **|**

4

I am confused as to why the "continue" keyword is used here. It seems to me that the code would (and does) execute just fine without it?
Was there a specific reason it was left in there?
Thanks so much!

gabewr

commented on 05/28/16

1

This is how i did it.
var profitArray is map of the agumented array.
It takes every value a turns it into the profit one would make if he bought the stocks that day and sold them on the most expensive day left.
profitArray.sort(function(a, b){return b-a})[0] simply returns the highest value of profit opportunities

function maxProfit(array) { var profitArray = array.map(function(currentValue, index) { return array.slice(index, array.length) .sort(function(a, b){return b-a})[0] - currentValue }) return profitArray.sort(function(a, b){return b-a})[0]; } console.log(maxProfit([45, 24, 35, 31, 40, 38, 11]));

niclaflamme

commented on 06/28/16

0

To display code include the following tags:

function maxStock(arr){ if(arr[0] > arr[1]){ var redundant = arr.shift(); var newArr = arr; var maxPrice = Math.max.apply(null, newArr); var minPrice = Math.min.apply(null, newArr); if(newArr.indexOf(maxPrice) > newArr.indexOf(minPrice)){ var maxProfit = maxPrice - minPrice; } else { while(newArr.indexOf(minPrice) > newArr.indexOf(maxPrice)){ newArr.splice(newArr.indexOf(minPrice), 1); } var newMin = Math.min.apply(null, newArr); maxProfit = maxPrice - newMin; } } return maxProfit; }

bpaksoy

commented on 12/09/16

0

To display code include the following tags:

function maxStock(arr){ if(arr[0] > arr[1]){ var redundant = arr.shift(); var newArr = arr; for(var i = 0; i < newArr.length; i++){ var maxPrice = Math.max.apply(null, newArr); var minPrice = Math.min.apply(null, newArr); if(newArr.indexOf(maxPrice) > newArr.indexOf(minPrice)){ var maxProfit = maxPrice - minPrice; } else { while(newArr.indexOf(minPrice) > newArr.indexOf(maxPrice)){ newArr.splice(newArr.indexOf(minPrice), 1); } var newMin = Math.min.apply(null, newArr); maxProfit = maxPrice - newMin; } } } return maxProfit; }

bpaksoy

commented on 12/09/16

0

The question is a bit misleading. It says "by buying a stock at the given price and then selling the stock later on". The buy price is not given. Would be much clearer to say "buy once and sell once".

romanov

commented on 08/13/17

0

I tried this way...

function stock(array){ var min= array[0]; var max= -1; var profit= 0; for(var idx in array){ if(array[idx]<min){ min= array[idx]; max= -1; } if(array[idx]>max){ max= array[idx]; if(max-min>profit){ profit= max-min; } } } return profit; } console.log(stock([44, 30, 24, 32, 35, 30, 40, 38, 15]));

JibranGarcia

commented on 06/05/16

0

I'd like to suggest that using array.reduce() is the most streamlined approach for such a problem:

function StockPicker(arr) { let trueMax = 0; arr.reduce(function(prev, item){ let max = Math.max(item - prev, 0); max > trueMax ? trueMax = max : null; if (max === 0) { return item; } else { return prev; } }); if (trueMax === 0) { return -1; } return trueMax; }

thedragon

commented on 07/16/16

-4

My version, but I am not quite convinced by this problem.

var maxArray = arr; maxArray.shift(); var minArray = arr; minArray.pop(); var min = Math.min.apply(null, minArray); var max = Math.max.apply(null, maxArray); return max-min;

natseg

commented on 07/01/16

Login to submit a comment