## Interview Question for Software Engineers

Country: United States
Interview Type: In-Person

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

I assume that one can purchase stock only when s/he hasn't any stock on hand.

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

Pretty much its finding sequential periods where is consecutively going up

List<Tupple<int, int>> findMaxProfit(List<int> stockvalues) {
List<Tupple<int, int>> transactions = new List<Tupple<int, int>>()
int currentStart = 0;
for (int i=1; i < stockvalues.length; i++) {
if(stockvalues[i-1] > stockvalues[i]) {
if(currentStart != i-1) // If its not the same data
{
}

currentStart = i;
}
}

// Handling when even the last day was still going up
if(stockvalues[currentstart] < stockvalues[stockvalues.length - 1]) {
transactions.add(new Tuple<int, int>(currentstart, stockvalues.length - 1)
}

return transactions;
}

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

Assume profit is for the end of the day of the last purchased

public static long findMaxProfit(int[] stockPrices) {
int curStockPrice = stockPrices[stockPrices.length-1];
long profit = 0;

for (int pricePaid : stockPrices) {
profit += curStockPrice-pricePaid;
}

return profit;
}

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

public static int findProfit(List<Integer> transactions){
int profit = 0;
if(transactions==null || transactions.size()<2){
return profit;
}
for(int currentPrice : transactions){
profit = profit + (currentPrice - buyPrice);
}else {
}
}
return profit;
}

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

public static int findProfit(List<Integer> transactions){
int profit = 0;
if(transactions==null || transactions.size()<2){
return profit;
}
for(int currentPrice : transactions){
profit = profit + (currentPrice - buyPrice);
}else {
}
}
return profit;
}

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

public static int findProfit(List<Integer> transactions){
int profit = 0;
if(transactions==null || transactions.size()<2){
return profit;
}
for(int currentPrice : transactions){
profit = profit + (currentPrice - buyPrice);
}else {
}
}
return profit;
}

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

public static int findProfit(List<Integer> transactions){
int profit = 0;
if(transactions==null || transactions.size()<2){
return profit;
}
for(int currentPrice : transactions){
profit = profit + (currentPrice - buyPrice);
}else {
}
}
return profit;
}

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

You can see it as a path:

7
/
6                6
/  \               /
5     \     5     /
\   /  \  /
\ /    4
3

(Formatting is giving me a hard time.. but you know what i mean)

If the last transaction is BUY - as long as its going up you keep the stock and the moment it starts going down (arr[i] < arr[i-1]) you have to SELL (on the previous day - i-1)

If the last transaction is SELL - as long as its going down you wait and the moment it starts going up again (arr[i] > arr[i-1]) you BUY at the cheapest price (on the previous day - i-1)

An edge case is on the last day - if you still hold the stock and the last day is higher than the day before - you just sell it.

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.