Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 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.

- Poozmak February 22, 2020 | Flag Reply
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.

- fz February 14, 2020 | Flag Reply
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
			{
				transactions.add(new Tuple<int, int>(currentStart, i-1))
			}

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

- Nelson Perez February 14, 2020 | Flag Reply
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;
}

- Tat February 14, 2020 | Flag Reply
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;
}
int buyPrice = transactions.get(0);
for(int currentPrice : transactions){
if(currentPrice>buyPrice){
profit = profit + (currentPrice - buyPrice);
buyPrice = currentPrice;
}else {
buyPrice = currentPrice;
}
}
return profit;
}

- VS February 20, 2020 | Flag Reply
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;
        }
        int buyPrice = transactions.get(0);
        for(int currentPrice : transactions){
            if(currentPrice>buyPrice){
                profit = profit + (currentPrice - buyPrice);
                buyPrice = currentPrice;
            }else {
                buyPrice = currentPrice;
            }
        }
        return profit;
    }

- VS February 20, 2020 | Flag Reply
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;
        }
        int buyPrice = transactions.get(0);
        for(int currentPrice : transactions){
            if(currentPrice>buyPrice){
                profit = profit + (currentPrice - buyPrice);
                buyPrice = currentPrice;
            }else {
                buyPrice = currentPrice;
            }
        }
        return profit;
    }

- VS February 20, 2020 | Flag Reply
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;
        }
        int buyPrice = transactions.get(0);
        for(int currentPrice : transactions){
            if(currentPrice>buyPrice){
                profit = profit + (currentPrice - buyPrice);
                buyPrice = currentPrice;
            }else {
                buyPrice = currentPrice;
            }
        }
        return profit;
    }

- sinhavis February 20, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

l = [7,5,6,3,5,5,6,4,6,7,1]
import random
#l= [random.randrange(1,10) for l in range(10)]

ser=[]

#i=0
i=0
s=[l[i]]
mp=0 #stores highest possible profit for whole session
lp=0 #stores profit for a single series

print ('i=%d l[i]=%d lp=%d mp=%d' %(i,l[i],lp, mp))

#0< i < len -1
while i<len(l)-1:
    i+=1
    if l[i-1]<=l[i]:        
        s.append(l[i])
        lp+=l[i]-l[i-1]
        mp+=l[i]-l[i-1]     
    else:
        ser.append((lp,s))  
        s=[l[i]]        
        lp=0
    
    print ('i=%d l[i]=%d lp=%d mp=%d' %(i,l[i],lp, mp))

#i=len-1
ser.append((lp,s))       
print ('i=%d l[i]=%d lp=%d mp=%d' %(i,l[i],lp, mp))
print(ser) #list of buys series with local profits

- MR February 29, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def maxProfit(arr):
    ''' Return an array of (buy_day, sell_day) tuples that maximize profit '''
    if len(arr) == 0 or len(arr) == 1:
        return []

    l = []
    p = 0
    q = 1
    in_position = False

    while True:
        if arr[q]>arr[q-1]:
            if in_position:
                q+=1
            else:
                in_position = True
                q+=1

        else:
            if in_position:
                in_position = False
                l += [(p,q-1)]

            p = q
            q += 1

        if q == len(arr):
            if in_position:
                l += [(p,q-1)]
                in_position = False

            break

    return l

- avicii March 01, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def maxProfit(arr):
    ''' Return an array of (buy_day, sell_day) tuples that maximize profit '''
    if len(arr) == 0 or len(arr) == 1:
        return []

    l = []
    p = 0
    q = 1
    in_position = False

    while True:
        if arr[q]>arr[q-1]:
            if in_position:
                q+=1
            else:
                in_position = True
                q+=1

        else:
            if in_position:
                in_position = False
                l += [(p,q-1)]

            p = q
            q += 1

        if q == len(arr):
            if in_position:
                l += [(p,q-1)]
                in_position = False

            break

    return l

- avicii March 01, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The level of skill on this website is pathetic compared to websites like leetcode...

public int maxProfit(int[] prices) {
if (prices.length < 2) return 0;
int res = 0;
for (int i = 1; i < prices.length; i ++) {
res += Math.max(prices[i] - prices[i - 1], 0);
}
return res;
}

- anony March 03, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ynkk

- Anonymous March 09, 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