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

- Amit Priyadarshi July 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

second = first; first = array[index];

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

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

- ash October 20, 2010 | Flag
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.

- Anonymous July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use bitmap for space and time efficiency...

- PS November 09, 2010 | Flag Reply
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.

- vic December 02, 2014 | Flag Reply
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

- Anonymous July 10, 2010 | Flag Reply
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];
}
}
}

- amit March 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- kermidt October 20, 2011 | 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