## Interview Question for Software Engineer / Developers

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

here's the most straightforward solution
first = second = 0;
for (int index =0; index < size; index++)
{
if(array[index] > first)
{
second = first; first = second;
}
if(array[index] > second && array[index] < first)
{
second = array[index];
}
}

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

you mean ...
in the first If statement block..

second = first; first = array[index];

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

i think code by amit will not work for all negative numbers. (-1 -2 -3 -4 -5). Check out this code.

``````big = secbig = a[0]
for(i=1; i<n; i++ {
if(a[i] >= big) {
if(a[i] == big) continue;
secbig = big;
big = a[i];
} elseif (a[i] > secbig || big == secbig) {
secbig = a[i];
}
}``````

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

@Anonymous: how would you sort an array in a single for loop..

better be keep track of 2 variables, max1 and max2.

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

use bitmap for space and time efficiency...

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

I would ask firstly if this array already sorted and wat to do in case if array size = 1.

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

sort and return the n-1 element if n is the largest

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

void findMax1Max2(int arr[],int &max1,int &max2,int size)
{
max1 = arr[0];
max2 = arr[0];
for(int i =1;i<=size;i++)
{
if(max1<arr[i])
{
max2 = max1;
max1 = arr[i];
}
}
}

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

Fails for {3, 2} returns 3 instead of 2

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.