## Flipkart Interview Question for Senior Software Development Engineers

• 1
of 1 vote

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 - arr;
int min = arr;
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;
}``````

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

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)

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

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.

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

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

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

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

it has o(n^2) complexity

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.