Interview Questions
function StockPicker(arr) { var max_profit = -1; var buy_price = 0; var sell_price = 0; // this allows our loop to keep iterating the buying // price until a cheap stock price is found var change_buy_index = true; // loop through list of stock prices once for (var i = 0; i < arr.length-1; i++) { // selling price is the next element in list sell_price = arr[i+1]; // if we have not found a suitable cheap buying price yet // we set the buying price equal to the current element if (change_buy_index) { buy_price = arr[i]; } // if the selling price is less than the buying price // we know we cannot make a profit so we continue to the // next element in the list which will be the new buying price if (sell_price < buy_price) { change_buy_index = true; continue; } // if the selling price is greater than the buying price // we check to see if these two indices give us a better // profit then what we currently have else { var temp_profit = sell_price - buy_price; if (temp_profit > max_profit) { max_profit = temp_profit; } change_buy_index = false; } } return max_profit; } StockPicker([44, 30, 24, 32, 35, 30, 40, 38, 15]);
def StockPicker(arr): max_profit = -1 buy_price = 0 sell_price = 0 # this allows our loop to keep iterating the buying # price until a cheap stock price is found change_buy_index = True # loop through list of stock prices once for i in range(0, len(arr)-1): # selling price is the next element in list sell_price = arr[i+1] # if we have not found a suitable cheap buying price yet # we set the buying price equal to the current element if change_buy_index: buy_price = arr[i] # if the selling price is less than the buying price # we know we cannot make a profit so we continue to the # next element in the list which will be the new buying price if sell_price < buy_price: change_buy_index = True continue # if the selling price is greater than the buying price # we check to see if these two indices give us a better # profit then what we currently have else: temp_profit = sell_price - buy_price if temp_profit > max_profit: max_profit = temp_profit change_buy_index = False return max_profit print StockPicker([44, 30, 24, 32, 35, 30, 40, 38, 15])
gabewr
commented on 05/28/16
romanov
commented on 08/13/17
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
function StockPicker(arr) { let lissf = arr[0] let profit = -1 for(let i = 1; i < arr.length; i++) { if(arr[i] > lissf) { profit = Math.max(profit, arr[i] - lissf) } else { lissf = arr[i] } } return profit }
nagolyhprum
commented on 12/21/18
def StockPicker(arr): bp=0 sp=0 mp=-1 for r in arr: if arr[arr.index(r)+1] > r: bp=r break sp=max(arr[_] for _ in range(arr.index(bp),len(arr))) if sp-bp > mp: return sp-bp else: return mp print(StockPicker([44, 30, 24, 32, 35, 30, 40, 38, 15]))
kumarvinod002
commented on 01/23/19
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
function stock(arr){ var buy = -1, sell =-1, profit = -1; var changeSellPrice = true; for(var i=0; i< arr.length; i++){ // assigning "buy" value inside the array because if array is empty, buy would be -1 if(buy===-1){ buy= arr[i]; } if(buy> arr[i]){ buy = arr[i]; changeSellPrice = true; } if(changeSellPrice || sell< arr[i+1] ){ sell = arr[i+1]; if(profit< (sell-buy)){ profit = sell - buy; changeSellPrice = false; } } } return profit; }
yzarina
commented on 03/20/18
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
def stockExchange(stockPrices): buy = stockPrices[0] sell = 0 profit = -1 for n in range(0,len(stockPrices)-1): x = stockPrices[n] if x < buy: buy = x sell = 0 elif x > sell: sell = x profit = sell - buy return profit
bterryjack
commented on 09/29/17
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
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