Facebook Interview Question
Solutions ArchitectsCountry: United States
public class Profit {
public int minDiff(int [] ar) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i=0; i<ar.length; i++) {
map.put(ar[i], ar[i]);
}
return map.lastKey()-map.firstKey();
}
public static void main(String[] args) {
int [] input = {19,22,15,35,40,10,20};
Profit profit = new Profit();
int output= profit.minDiff(input);
System.out.println("Profit: "+ output);
}
}
public static void main(String[] args) {
int[] sales={19,22,15,35,40,10,20};
int minimum= sales[0];
int end=1;
int profit=Integer.MIN_VALUE;
while(end<sales.length){
if(sales[end]>= minimum && sales[end]-minimum>profit)
profit= sales[end]-minimum;
if(sales[end]<minimum)
minimum=sales[end];
end++;
}
System.out.println(profit);
}
}
public static void main(String[] args) {
int[] sales={19,22,15,35,40,10,20};
int minimum= sales[0];
int end=1;
int profit=Integer.MIN_VALUE;
while(end<sales.length){
if(sales[end]>= minimum && sales[end]-minimum>profit)
profit= sales[end]-minimum;
if(sales[end]<minimum)
minimum=sales[end];
end++;
}
System.out.println(profit);
}
}
public static void main(String[] args) {
int[] sales={19,22,15,35,40,10,20};
int minimum= sales[0];
int end=1;
int profit=Integer.MIN_VALUE;
while(end<sales.length){
if(sales[end]>= minimum && sales[end]-minimum>profit)
profit= sales[end]-minimum;
if(sales[end]<minimum)
minimum=sales[end];
end++;
}
System.out.println(profit);
}
}
public static void main(String[] args) {
int[] sales={19,22,15,35,40,10,20};
int minimum= sales[0];
int end=1;
int profit=Integer.MIN_VALUE;
while(end<sales.length){
if(sales[end]>= minimum && sales[end]-minimum>profit)
profit= sales[end]-minimum;
if(sales[end]<minimum)
minimum=sales[end];
end++;
}
System.out.println(profit);
}
public static void main(String[] args) {
int[] sales={19,22,15,35,40,10,20};
int minimum= sales[0];
int end=1;
int profit=Integer.MIN_VALUE;
while(end<sales.length){
if(sales[end]>= minimum && sales[end]-minimum>profit)
profit= sales[end]-minimum;
if(sales[end]<minimum)
minimum=sales[end];
end++;
}
System.out.println(profit);
}
}
public void maxProfit(int[] stocks) {
int min = 0;
int profit = 0;
int buy = 0;
int sell = 0;
for (int i = 0; i < stocks.length; i++) {
if (stocks[i] < stocks[min])
min = i;
int diff = stocks[i] - stocks[min];
if (diff > profit) {
buy = min;
sell = i;
profit = diff;
}
}
System.out.println("Buy: " + stocks[buy] + ", Sell: " + stocks[sell] + ", Profit: " + profit);
}
public static int getMaximumProfit(int []stockQuotes){
if(stockQuotes.length==1){
return 0;
}
int minimumPrice = stockQuotes[0];
int maximumProfit = Integer.MIN_VALUE;
for(int i=0;i<stockQuotes.length;i++){
if(stockQuotes[i] - minimumPrice > maximumProfit){
maximumProfit=stockQuotes[i] - minimumPrice;
}
if(stockQuotes[i]<minimumPrice) {
minimumPrice = stockQuotes[i];
}
}
return maximumProfit;
}
We can do this only with min and profit. Also, if its mandatory to return negative profit, that also needs to consider. e.g {22, 19}, should this return as -3? I assumed we dont have to buy and sell if there is negative profit and return 0 in that case. if interviewer insist, we can change last line to return negative.
public int findMaxProfit(int[] estimatedStocks) {
int min = estimatedStocks[0];
int profit = Integer.MIN_VALUE;
for(int i=1; i< estimatedStocks.length; i++) {
if(profit < estimatedStocks[i] - min)
profit = estimatedStocks[i] - min;
if(estimatedStocks[i] < min)
min =estimatedStocks[i];
}
return profit > 0 ? profit : 0;
}
package com.arnab.recursion;
public class StockMarket {
public static void maxProfit() {
int arr[] = {19,25,7,35,40,10,90,0};
int profit = 0;
int buy = arr[0];
int sale = 0;
for(int i = 1; i < arr.length - 1 ; i++) {
if(buy > arr[i]) {
buy = arr[i];
}
if(arr[i] > arr[i + 1]) {
sale = arr[i];
profit = sale - buy;
}
}
System.out.println(profit);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StockMarket.maxProfit();
}
}
package com.arnab.recursion;
public class StockMarket {
public static void maxProfit() {
int arr[] = {19,25,7,35,40,10,90,0};
int profit = 0;
int buy = arr[0];
int sale = 0;
for(int i = 1; i < arr.length - 1 ; i++) {
if(buy > arr[i]) {
buy = arr[i];
}
if(arr[i] > arr[i + 1]) {
sale = arr[i];
profit = sale - buy;
}
}
System.out.println(profit);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StockMarket.maxProfit();
}
}
public static void maxProfit() {
int arr[] = {19,25,7,35,40,10,90,0};
int profit = 0;
int buy = arr[0];
int sale = 0;
for(int i = 1; i < arr.length - 1 ; i++) {
if(buy > arr[i]) {
buy = arr[i];
}
if(arr[i] > arr[i + 1]) {
sale = arr[i];
profit = sale - buy;
}
}
System.out.println(profit);
}
Just need to keep track of the current min number
public class Question
{
public static void main(String[] args)
{
int[] transactions = {19, 22, 15, 11, 40, 10, 20};
int[] transactions2 = {50, 40, 30, 25, 22, 10, 2};
System.out.println(largestProfit(transactions));
System.out.println(largestProfit(transactions2));
}
public static int largestProfit(int[] transactions){
if(transactions == null || transactions.length < 2){
return 0;
}
int currentMin = transactions[0];
int largestProfit = Integer.MIN_VALUE;
for(int i = 1; i < transactions.length; i++){
int currentProfit = transactions[i] - currentMin;
if(currentProfit > largestProfit){
largestProfit = currentProfit;
}
if(transactions[i] < currentMin){
currentMin = transactions[i];
}
}
return largestProfit;
}
}
def find_max_profit(mylist):
i=0
lst = list(mylist)
diff_list=list()
lst.sort()
max_item=lst[len(mylist) - 1]
#print "Max item is:",max_item
index_of_item=mylist.index(max_item)
#print "Index of max item:",index_of_item
for i in range(index_of_item):
diff = mylist[index_of_item] - mylist[i]
diff_list.append(diff)
diff_list.sort()
max_profit= diff_list[len(diff_list) - 1]
return max_profit
if __name__=="__main__":
mylist = [19, 22, 15, 35, 40, 10, 20, 60 ]
max_profit=find_max_profit(mylist)
print "Max profit is:",max_profit
def find_max_profit(mylist):
i=0
lst = list(mylist)
diff_list=list()
lst.sort()
max_item=lst[len(mylist) - 1]
#print "Max item is:",max_item
index_of_item=mylist.index(max_item)
#print "Index of max item:",index_of_item
for i in range(index_of_item):
diff = mylist[index_of_item] - mylist[i]
diff_list.append(diff)
diff_list.sort()
max_profit= diff_list[len(diff_list) - 1]
return max_profit
if __name__=="__main__":
mylist = [19, 22, 15, 35, 40, 10, 20, 60 ]
max_profit=find_max_profit(mylist)
print "Max profit is:",max_profit
#include <stdlib.h>
#include <stdio.h>
static int squote[] = {19, 22, 15, 35, 40, 10, 20};
static int nsquote = 7;
int
maxprofit(int *a, int n)
{
int i, c, lo, r, maxr;
if(n < 1)
return 0;
lo = a[0];
maxr = 0;
for(i = 0; i < n; i++){
c = a[i];
if(c < lo)
lo = c;
r = c - lo;
if(r > maxr)
maxr = r;
}
return maxr;
}
int
main(int argc, char* argv[])
{
printf("%d\n", maxprofit(squote, nsquote));
return 0;
}
Everyone really needs to run this through a test of [19, 22, 15] to see how their algorithm checks out. From what I see, just getting the min and max of the list isn't going to get you the right number.
I'm going to use 3 running numbers: buy, sell, and prospective_buy. I'm going to iterate through the array once (O(n)) to determine my actual buy and sell price, while comparing against my prospective_buy and the value of the current sell.
int getMaxProfit(int[] prices) {
int buy = prices[0];
int sell = prices[0];
int prospective_buy = prices[0];
for (int i = 1; i < prices.length; i++) {
if (prices[i] - prospective_buy > sell - buy) {
buy = prospective_buy;
sell = prices[i];
break;
}
if (prices[i] > sell) {
sell = prices[i]; break;
}
if (prices[i] < buy) {
prospective_buy = prices[i];
}
}
return sell - buy;
}
With [19, 22, 15]
buy = 19
sell = 19
prospective_buy = 19
i = 1
buy = 19
sell = 22
prospective_buy = 19
i = 2
buy = 19
sell = 22
prospective_buy = 15
returned profit = 3
With [19, 22, 15, 35, 40 10, 20]
buy = 19
sell = 19
prospective_buy = 19
i = 1
buy = 19
sell = 22
i = 2
prospective_buy = 15
buy = 19
sell = 22
i = 3
buy = 15
sell = 35
prospective_buy = 15
i = 4
buy = 15
sell = 40
prospective_buy = 15
i = 5
buy = 15
sell = 40
prospective_buy = 10
i = 6
buy = 15
sell = 40
prospective_buy = 10
return profit of 25
Need to reset the max price whenever min is encountered and keep a separate variable for max_profit and check against that only when we get a new max price quote.
def max_profit(quotes):
min_quote = max_quote = quotes[0]
max_profit = 0
for quote in quotes:
if quote < min_quote:
min_quote = max_quote = quote
elif quote > max_quote:
max_quote = quote
if (max_profit < (max_quote - min_quote)):
max_profit = (max_quote - min_quote)
return max_profit
Need to reset max price when encountering a min price. and keep a record of max profit and check against then only when we reset max price.
def max_profit(quotes):
min_quote = max_quote = quotes[0]
max_profit = 0
for quote in quotes:
if quote < min_quote:
min_quote = max_quote = quote
elif quote > max_quote:
max_quote = quote
if (max_profit < (max_quote - min_quote)):
max_profit = (max_quote - min_quote)
return max_profit
{{
- Anas February 13, 2017public class Profit {
public int minDiff(int [] ar) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int i=0; i<ar.length; i++) {
map.put(ar[i], ar[i]);
}
return map.lastKey()-map.firstKey();
}
public static void main(String[] args) {
int [] input = {19,22,15,35,40,10,20};
Profit profit = new Profit();
int output= profit.minDiff(input);
System.out.println("Profit: "+ output);
}
}
}}