## Interview Question

Country: India

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

Simply find smallest and largest element in the array. ie answere

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

Here is the code. Please correct me if any mistake

void FindMaxDiff(int arr[], int arr_size)
{
int i, first, second;
int diff = 0;
/* There should be atleast two elements*/
if(arr_size < 2)
{
printf(" Invalid Input ");
return;
}
first = second = 0;
for(i = 0; i < arr_size ; i ++)
{
/*If current element is smaller than first then update both first and second */
if(arr[i] > first)
{
second = first;
first = arr[i];
if(diff < (first-second))
diff = (first-second);
}
/* If arr[i] is in between first and second then update second */
else if (arr[i] > second)
{
second = arr[i];
}
}
Printf(“ xxxx “);
}

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

int maxDiff( int* numbers, int size ) {
int max_diff = 0;
int min_ind = 0;
for( int i = 0 ; i < size; i++ ) {

if( numbers[i] < numbers[min_ind] ) {
min_ind = i;
}
else {
if ( ( numbers[i] - numbers[min_ind] ) > max_diff ) {
max_diff = numbers[i] - numbers[min_ind] ;
}
}
}

return max_diff;
}

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

For a descending input 10,8,7,5,3,1 the code will not work as max diff will never get updated in this case

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

Take two variables
min and maxdiff,
Keep track of min element found so far and take difference of current element and the min value found so far. keep storing difference in the maxdiff variable.

at the end it will return the max differnce.
and for index keep there track too.

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.