Facebook Interview Question for Solutions Architects


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
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);

}

}
}}

- Anas February 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);

	}

}

- Anas February 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] array = { 19, 22, 15, 35, 40, 10, 20 };

            int profit = 0;
            int buy = array[1];
            int sale = 0;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i]<buy)
                {
                    buy = array[i];
                    sale = array[i + 1];
                }
                if (array[i]>sale)
                {
                    sale = array[i];
                    profit = sale - buy;
                }
            }

- Daniel February 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think your solution is good but your min check on least will always return 0 because you've initialized it incorrectly. prob should init to the max integer value.

- Anonymous February 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

- Deep February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

- Deep February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);

}
}

- deep February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);

}

- deep February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);

}
}

- deep February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
	}

- Saurabh Rane February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Emre Bayram February 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxReturn(int[] input){
	int returnValue = -1;	
	int min  = 100;
    	int max = 0;
        for(int i= 0; i<input.length;i++){
            if(input[i] < min){
                min = input[i];
            }
            if(input[i] > max){
                max = input[i];
            }
        }
	returnValue = max - min;
	return returnValue;
}

- mannerh1 February 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
	}

- v32 February 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
}

}

- sarkar February 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
	}

}

- sarkar February 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}

- sarkar February 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function getMaxProfit(stocks) {
  var len = stocks.length,
    min = stocks[0],
    max = 0;
  
  for (var i = 1; i < len; i++) {
    min = Math.min(min, stocks[i]);
    max = Math.max(max, stocks[i] - min);
  }
  
  return max;
}

- Raymond February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function getMaxProfit(stocks) {
  var len = stocks.length,
    min = stocks[0],
    max = 0;
  
  for (var i = 1; i < len; i++) {
    min = Math.min(min, stocks[i]);
    max = Math.max(max, stocks[i] - min);
  }
  
  return max;

}

- Raymond February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function getMaxProfit(stocks) {
var len = stocks.length,
min = stocks[0],
max = 0;

for (var i = 1; i < len; i++) {
min = Math.min(min, stocks[i]);
max = Math.max(max, stocks[i] - min);
}

return max;
}

- Anonymous February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

?

- Anonymous February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function getMaxProfit(stocks) {
  var len = stocks.length,
    min = stocks[0],
    max = 0;
  
  for (var i = 1; i < len; i++) {
    min = Math.min(min, stocks[i]);
    max = Math.max(max, stocks[i] - min);
  }
  
  return max;
}

- Anonymous February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function getMaxProfit(stocks) {
  var len = stocks.length,
    min = stocks[0],
    max = 0;
  
  for (var i = 1; i < len; i++) {
    min = Math.min(min, stocks[i]);
    max = Math.max(max, stocks[i] - min);
  }
  
  return max;

}

- Raymond February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
  
  }
}

- DCIS February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

arr=[19,22,15,35,40,10,20]
mini=arr[0]
maxc=0
for i in arr:
	if i>mini:
		if i-mini>maxc:
			maxc=i-mini
	else:
		mini=i
print maxc

- sharadboni February 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Deep March 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Deep March 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- yarikos March 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- James March 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Aryn January 28, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Aryn January 28, 2020 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More