Microsoft Interview Question for Software Engineer / Developers


Team: Windows Live
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
8
of 10 vote

void StockProcess(const vector<int>& stocks,
                                  int* buyday,
                                  int* sellday)
{
        int min_index = 0;
        *buyday = 0;
        *sellday = 0;
        for (int i = 1; i < stocks.size(); ++i) {
              if (stocks[i] < stocks[min_index]) {
                    min_index = i;
              } else if ( (stocks[i] - stocks[min_index]) > (stocks[*sellday] - stocks[*buyday])) {
                   *buyday = min_index;
                   *sellday = i;
             }
        }
}

Time Complexity: O(n)

- nim January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

perfect

- Anonymous January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is incorrect.. With this you can get a solution such that buyday > sellday which is NOT a valid solution

- loveCoding January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is incorrect.. With this you can get a solution such that buyday > sellday which is NOT a valid solution

- loveCoding January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the above code, buyday will not bigger than sellday.

- nim January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tend to agree with Manan...

- thrw25 January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope. You won't get buyday > sellday. The only possibility is that you'll get an answer of 0 if buyday = sellday = 0, which could happen (the "only winning move is not to play" situation). But I consider that to be the preferred answer in that case anyway...

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I haven't thought through all the potential corner cases, but this seems correct to me.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

aap dude hain... correct solution. I certify it!

- kher.ajinkya February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I Think the solution is wrong. If the given series is decreasing then the buy and sell day will never get set to any day. How is the profit case in this scenario.:
10 9 8 7 6 5 4 3 2 1..??

- krishna July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming stock is always positive int. buy and sell are -1s if stock is always losing.

void BestDaysToSell(int []days, out int buy, out int sell)
{

	int buy = -1;
	int sell= -1;
	int currBuy = days[0];
	int currSell= int.MinValue;

	for(int i=1;i<days.Length; i++)
	{
		if(days[i] < currbuy)
		{
			currSell=days[i]
			currBuy = days[i];
		}
		else if(days[i] > currSell)
		{
			currSell = days[i];
			if(days[i] - currBuy > sell - buy)
			{
				buy = currSell;
				sell = currBuy;
			}
		}
	}
}

- Anonymous January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

maxProfit( int stockPrice[] , int N )
{
      int buyPrice = stockPrice[0];
      int sellPrice =-1;
      int profit =0 ;
      for( i=1;i<N;i++ )
      {
           int price = stockPrice[i];
           
           if( price - buyPrice > profit )
           {
               profit = price - buyPrice ;
               sellPrice = price ;
           } 
           
           buyPrice = min(buyPrice , price );
      } 
}

- Anonymous January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using a MinStack, that is, a stack with push, pop and min operations having O(1) complexity. This runs in O(n).

public class MaximizeProfit {

	public int maximize(Integer[] stockPrice){
		int maxProfit=0;

		MinStack<Integer> stack = new MinStack<Integer>();
		stack.push(stockPrice[0]);
		
		for(int i=1; i<stockPrice.length; i++){
			if( (stockPrice[i] - stack.min()) > maxProfit ) {
				maxProfit = stockPrice[i] - stack.min();
			}
			// stack.min() will return the minimum element from 0 to i-1
			stack.push(stockPrice[i]); 
		}
		return maxProfit;
	}
}

- thrw25 January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can even do it without a stack.

- eugene.yarovoi January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For people who are interested. The same problem in there in CLRS book, "maximum-subarray problem". O(nlgn) complexity.

- kanap008 January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def maximizeProfit(input):
    
    maxDiff = float("-inf") 
    currentDiff = float("-inf")
    bottom = input[0]
    max = 0
    for index in range(1,len(input)):
        currentDiff += input[index] - input[index-1]
        
        if (currentDiff > maxDiff):
            maxDiff = currentDiff
            max = input[index]
            
        if (bottom > input[index]):
            print(currentDiff)
            bottom = input[index]
            currentDiff = 0
        
    print("buy at :"+str(max -  maxDiff)+ " Sell at: "+str(max))

- Anonymous April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Refer cormen. Longest common sub sequence. O(NlogN)

- wirelesszzz January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

assuming profit is in positive and loss is in negative,this problem becomes same as of finding largest subarray in an array

- Abhi January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

All integers in the array will be positive as they correspond with the price for that day, not the change in price.

- nimbus January 17, 2012 | Flag


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