Flipkart Interview Question for Senior Software Development Engineers


Team: digital
Country: India
Interview Type: Phone Interview




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

int maxProfit(int arr[])
{
  int max_profit = arr[1] - arr[0];
  int min = arr[0];  
  for(int i = 1; i < arr.length; i++)
  {      
    if((arr[i] - min) >max_profit)                              
      max_profit = arr[i] - min;
    if(arr[i] < min)
         min = arr[i];                    
  }
  return max_profit;
}

- Vishal S Kumar February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void findMaxStockProfit(int[] arr) {
        int maxSum = Integer.MIN_VALUE;
        int maxSumStartIndx = 0;
        int maxSumEndIndx = 0;

        int lastMinValueIndx = 0;
        int curIndx = 0;

        while(curIndx < arr.length) {
            int diff = arr[curIndx] - arr[lastMinValueIndx];
            if(diff > maxSum) {
                maxSum = diff;
                maxSumStartIndx = lastMinValueIndx;
                maxSumEndIndx = curIndx;
            }

            if(arr[lastMinValueIndx] > arr[curIndx]) {
                lastMinValueIndx = curIndx;
            }
            curIndx++;
        }
        System.out.println("MaxSum = " + maxSum + ". StartIndx = " + maxSumStartIndx + ". EndIndx = " + maxSumEndIndx);
    }

- Saumesh March 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Very interesting problem. Lets scan the array and find out if we sold the share next day what would be our profit(+) or loss(-). So the new array is
-65,-11,18,83,-60,-42.-3,85,-93,95,5,-96
Now scan the array from left to right and keep summing continuous +ives and store it in a variable max. If the next continuous +ive sum is > max then store that value in max. At the end of the scan we will have the max output. Complexity O(n)

- kr.neerav February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Slight change in the logic:
The logic of summing continuous +ives won't work for all cases. So instead maintain 2 variables max and runningSum. Now in the previous logic use runningSum instead of sum of continuous +ives.

- kr.neerav February 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how is answer 101?

int maxprofit(int stockvalue[ ], int n)
{
	int max = 0;
	for(int i=1; i<n; i++)
	{
			if ( ( a[i] - a[i-1] ) > max )
				max = a[i] - a[i-1];
	}
	return max;
}

- confused_banda February 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

{
private static void calcDiff2(int[] input) {
Arrays.sort(input);
int maxDiff = 0;
for (int i = 0; i < input.length; i++) {
for (int j = i + 1; j < input.length; j++) {
int diff = input[j] - input[i];
if (diff > maxDiff) {
maxDiff = diff;
}
}
}
System.out.println("Max Difference is (2) --> " + maxDiff);
}

private static void calcDiff1(int[] input) {
int maxDiff = 0;
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < input.length; j++) {
int diff = input[i] - input[j];
if (diff > maxDiff) {
maxDiff = diff;
}
}
}
System.out.println("Max Difference is (1) --> " + maxDiff);
}
}

- SharathB February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it has o(n^2) complexity

- Vishal S Kumar February 12, 2014 | 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