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

}

}
}}

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

}

}``````

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 sale = 0;
for (int i = 0; i < array.Length; i++)
{
{
sale = array[i + 1];
}
if (array[i]>sale)
{
sale = array[i];
}
}``````

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.

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

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

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

}
}

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

}

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

}
}

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 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) {
sell = i;
profit = diff;
}
}
System.out.println("Buy: " + stocks[buy] + ", Sell: " + stocks[sell] + ", Profit: " + profit);
}``````

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

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

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

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 sale = 0;
for(int i = 1; i < arr.length - 1 ; i++) {
}
if(arr[i] > arr[i + 1]) {
sale = arr[i];
}
}
System.out.println(profit);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StockMarket.maxProfit();
}

}

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 sale = 0;
for(int i = 1; i < arr.length - 1 ; i++) {
}
if(arr[i] > arr[i + 1]) {
sale = arr[i];
}
}
System.out.println(profit);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StockMarket.maxProfit();
}

}``````

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 sale = 0;
for(int i = 1; i < arr.length - 1 ; i++) {
}
if(arr[i] > arr[i + 1]) {
sale = arr[i];
}
}
System.out.println(profit);
}

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

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

}

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

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

``?``

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

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

}

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;

}
}``````

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``````

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``````

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``````

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

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 sell = prices[0];
for (int i = 1; i < prices.length; i++) {
sell = prices[i];
break;
}
if (prices[i] > sell) {
sell = prices[i]; break;
}
}
}
}``````

With [19, 22, 15]

``````buy = 19
sell = 19

i = 1
sell = 22

i = 2
sell = 22

returned profit = 3``````

With [19, 22, 15, 35, 40 10, 20]

``````buy = 19
sell = 19

i = 1
sell = 22

i = 2
sell = 22

i = 3
sell = 35

i = 4
sell = 40

i = 5
sell = 40

i = 6
sell = 40

return profit of 25``````

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``````

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``````

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.

### 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.