Arrowstreet Capital | Senior Full Stack Software Engineer role
Anonymous User
964

Phone interview:

Assuming there must be a buy and a sell, return the index of the buying price and index of the selling price with maximum profit and minimum loss.

Also build your own test cases.

import math
prices = [100, 90, 95, 100, 110]

def get_max_profit(prices):
  buy_p = prices[0]
  sell_p = prices[1]
  profit = buy_p - sell_p
  buy, sell = 0, 1
  buy_in = 0

  for i in range(1, len(prices)):
    if prices[i] < buy_p:
      buy_p = prices[i]
      buy = i
    elif prices[i] - buy_p > profit:
      profit = prices[i] - buy_p
      sell = i
      buy_in = buy
  
  return [buy_in, sell]
 
print(get_max_profit(prices))
Comments (1)