Adobe Interview Question
Testing / Quality AssurancesCountry: United States
Interview Type: Phone Interview
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];
}
if the interviewer ask u to extend the code to get the Kth of that array,what will u do?
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;
}
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;
}
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];
}
}
#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;
}
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);}
}
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);
}
}
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. :)
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);
// C#
// This code should look after duplicate values and minimum array size.
Sample array {0, -1, -3, -1, 0} after it's sorted {-3,-1,-1,0,0}
Answer = 0;
public void SecondLargest(int[] nums)
{
if (nums.Length < 2) Console.Write("invalid input");
int secondMax=nums[0], max=nums[0];
for (int i=0; i < nums.Length; i++)
{
if (nums[i]>secondMax) secondMax=nums[i];
if (secondMax > max)
{
int temp = max;
max = secondMax;
secondMax = temp;
} //end if
} //end for loop
Console.Write("Second Largest Number: {0}", secondMax);
} //end method
- cooldaa January 30, 2012