## Adobe Interview Question Testing / Quality Assurances

• 0

Write program to find 2-nd largest element in an array with single loop.

Country: United States
Interview Type: Phone Interview

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

``````int secondMaxEle(int a[], int len) {
int max = INT_MIN, secondMax = INT_MIN, i;
for(i = 0; i < len; i++) {
if(a[i] > max) {
secondMax = max;
max = a[i];
}
else if(a[i] > secondMax && a[i] < max)
secondMax = a[i];
}
return secondMax;
}``````

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

public int secondMaxElem(int[] ary}
{
if (ary == null || ary.size == 0 || ary.size == 1) return ERROR;

int max[] = new int[2];
max[0] = a[0]; max[1] = a[1];
for (int i = 2; i < ary.size; i++)
{
if (ary[i] > max[0] || ary[i] > max[1]
{
max[0] < max[1] ? max[0] = ary[i] : max[1] = ary[i];
}
}
return max[0] < max[1] ? max[0] : max[1];
}

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

why not array.size<=1 return ERROR

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

if the interviewer ask u to extend the code to get the Kth of that array,what will u do?

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

use heap to extend..

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

usage of heap will destroy the basic premise of "Single Loop".

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

This is the stupidest code segment I've ever seen!

int max[] = new int[2];
max[0] = a[0]; max[1] = a[1];
for (int i = 2; i < ary.size; i++)

really??

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

``````int secondMaxEle(int a[], int len) {
int max = a[0], secondMax = a[0];
for(int i = 1; i < len; i++) {
if(a[i] > max) {
secondMax = max;
max = a[i];
}
else if(a[i] > secondMax)
secondMax = a[i];
}
return seconMax;
}``````

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

{5,4,3,2,1} return will be 5....

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

{5,4,3,2,1} return will be 5....

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

Thanks for pointing the bug. The correction is that I initialize both max and secondMax to the lowest possible integer value and then traverse the loop from the first element.

``````int secondMaxEle(int a[], int len) {
int max = INT_MIN, secondMax = INT_MIN, i;
for(i = 0; i < len; i++) {
if(a[i] > max) {
secondMax = max;
max = a[i];
}
else if(a[i] > secondMax && a[i] < max)
secondMax = a[i];
}
return secondMax;
}``````

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

Failing in case of arr[] = {1,1};

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

Failing in case of arr[] = {1,1};

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

What is the complexity??

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

``````int a[5]={2,5,4,6,7};
int i,j;

for(i=0;i<5;i++)
{
int j=i+1;
if(a[i]>a[j]){
int temp=a[j];
a[j]=a[i];
a[i]=temp;

}
}
pf("%d",a[1]);``````

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

``````int len=100;
int a[len] = {3,7,8,2,9}
int max=a[0], smax=a[1];

//Getting max & second max between first 2 numbers
if(a[0]<a[1])
{
temp = smax; //maximum number
smax=a[1]; //second max number
max=temp;
}
//Comparing max, smax & swapping it when required in a loop
for(i=2;i<len;i++)
{
if(a{i}>max)
{
smax=max;   //bcoz max is already greater than smax
max=a[i];
}
else if(a[i]>smax)
{
smax=a[i];
}
}``````

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

#include<stdio.h>
#include<stdlib.h>
int select(int a[],int left,int right,int k)
{
if(left>=right) return a[left];
int i=left+1;
int j=right;
int p=a[left];
int t;
while(j>=i)
{
while(a[i]>p)i++;
while(a[j]<p)j--;
if(i>=j)break;
else{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
if(j-left+1==k)
return p;
t=a[left];
a[left]=a[j];
a[j]=t;
if(j-left+1<k)
return select(a,j+1,right,k-j+left-1);
else
return select(a,left,j-1,k);
}
int main() {
int a[20],k,n,i,d;
printf("Enter no of elements\n");
scanf("%d",&n);
printf("Enter n elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the value of k\n");
scanf("%d",&k);
d=select(a,0,n-1,k);
printf("Kth largest element is %d\n",d);
return 0;
}

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

public class Second_largest {
public static void main(String[] args) {
int array[]={5,4,3,2,1};
int max1=array[0];
int max2=array[1];
if(array.length<2){
System.out.println("not able to find second largest number");
}
else{
for(int i=1;i<array.length;i++){
if(array[i]>max1){
max2=max1;
max1=array[i];
}

}
System.out.println(max2);}
}

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

sorry mistake on above code
public class Second_largest {
public static void main(String[] args) {
int array[]={1,2};
if(array.length<2){
System.out.println("not able to find second largest number");
return;
}
int max1=array[0];
int max2=array[1];

for(int i=1;i<array.length;i++){
if(array[i]>max1){
max2=max1;
max1=array[i];
}

}
System.out.println(max2);
}

}

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

In a given array if you are trying to get the nth largest element.

Than take the nth node from the last as pivot element and than apply the partition logic of quick sort. which will get the nth element from the last in place. Left subarray (unsorted) will be the values less than the element and right subarray(unsorted) will be the values greater than the element.
But you will get the nth largest element from the last in the array. :)

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

int len = 12;

int a[] = {2,183,8,2,1,91,23,100,4,2,52,530};
int max=a[0], smax=a[1];
int temp,i;

//Getting max & second max between first 2 numbers
if(a[0]<a[1])
{
temp = smax; //maximum number
smax=a[0]; //second max number
max=temp;
}

printf("%d\n",max);
printf("%d\n",smax);

//Comparing max, smax & swapping it when required in a loop
for(i=2;i<len;i++)
{
if(a[i] > max)
{
smax=max; //bcoz max is already greater than smax
max=a[i];
}
else if(a[i]>smax)
{
smax=a[i];
}
printf("%d\n",max);
printf("%d\n",smax);
}

printf("%d",smax);

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### 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.