Amazon Interview Question for Software Engineer in Tests






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

#include <stdio.h>

int main()
{
int n,f_l,s_l,t_l,i;
int a[30];
printf("no of elem in the array \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

f_l = a[0];
s_l = a[0];
t_l = a[0];

for(i=1;i<n;i++)
{
if (f_l < a[i]){
t_l = s_l;
s_l = f_l;
f_l = a[i];
}
else if (s_l < a[i]){
t_l = s_l;
s_l = a[i];
}
else if (t_l < a[i]){
t_l = a[i];
}
}

printf("the third largest is %d",t_l);
return 0;
}

- Anonymous August 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

excellent simple way to find 3rd element, thank you very much.

- venkat March 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't work when numbers in decreasing order.
Lots of corner cases not cosidered.

- Bipul April 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take 3 variables and keep updating them..!!!

- Anonymous August 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

keep bubbling!

- songofsp3 August 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Something like this ??

void 3rdlargest(int [] a) {
		int n=3,lar;
		for(int i=0;i<a.length;i++){
			int k=1;
			for(int j=0;j<a.length;j++){
				if(a[i]<a[j])
					++k;
				if((k==n) && (j==a.length-1)){
					lar=a[i];
					break;
				}
			}
		}
              return lar;
	}

- krantboy August 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in hash let them increase the corresponding count.. and then do %2 to check which is even..

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

will fail for { 9, 1, 2, 3}
I think we need to use quick-select, runs in O(n)

- Davinc August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

take array of size three nd insert first three element in that array nd sort them.
nw if u get any element lesser then array[0] then do nothing nd if element is > array[0] then insert those element in the array nd it up 2 end. array[0] will be the third greatest element.

- Anonymous August 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ith largest element = (n-i+1)th smallest element. Use select algorithm to find it in linear time.

- Anonymous September 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct Element
{
int value;
int pos;
};

void Helper(Element* elements, int val, int position)
{
int pos=0;
for(int i=1;i<3;i++)
{
if(elements[i].value<elements[pos].value)
pos=i;
}

if(val>elements[pos].value)
{
elements[pos].value=val;
elements[pos].pos=position;
}
}

int FindThirdLargest(int* arr, int len)
{
Element elements[3];
int pos=0;

if(arr==NULL||len<3)
return -1;

for(int i=0;i<3;i++)
{
elements[i].value=arr[i];
elements[i].pos=i;
}

for(int i=3;i<len;i++)
{
Helper(elements, arr[i], i);
}

for(int i=1;i<3;i++)
{
if(elements[i].value<elements[pos].value)
pos=i;
}

return elements[pos].pos;
}

void main()
{
int arr[7]={102, -5, 238, 999, 781, 6, 7};
int pos=FindThirdLargest(arr,7);
printf("pos %d:value %d",pos,arr[pos]);
getchar();
}

- Anonymous September 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int thirdLargest(int a[], int n)
{
int a3 = -1 * INT_MAX;
int a2 = a3 + 1;
int a1 = a2 + 1;

for(int i = 0; i < n; i++)
{
if(a[i] > a1)
{
swap(&a3, &a2);
swap(&a2, &a1);
a1 = a[i];
}
else if(a[i] > a2)
{
swap(&a3, &a2);
a2 = a[i];
}
else if(a[i] > a3)
{
a3 = a[i];
}
}
return a3;
}

- Anonymous October 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int FindThirdLargest(int[] arr)
        {
            if (arr.Length < 3)
                throw new ArgumentException("Array size less than 3");

            int max1 = arr[0];
            int max2;
            int max3;
            if(arr[1] > arr[0])
            {
                max2 = max1;
                max1 = arr[1];
            }
            else
            {
                max2 = arr[1];
            }

            if(arr[2] <= max2)
            {
                max3 = arr[2];
            }
            else if (arr[2] > max1)
            {
                max3 = max2;
                max2 = max1;
                max1 = arr[2];
            }
            else
            {
                max3 = max2;
                max2 = arr[2];
            }

            for (int i = 3; i < arr.Length; i++)
            {
                int val = arr[i];

                if (val > max1)
                {
                    max3 = max2;
                    max2 = max1;
                    max1 = val;
                }
                else if (val > max2)
                {
                    max3 = max2;
                    max2 = val;
                }
                else if (val > max3 && val < max2)
                {
                    max3 = val;
                }
            }

            return max3;
        }

- Deepa October 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this will return the third largest in a single pass and works for both positive and negative numbers.

- Deepa October 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how to write program in c to this below output

P P
R R
O O
G
R R
A A
M M

- kumar pp August 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort that array store it in list so that it can be used in hashset .....hashset does not contain duplicate element so print the value at 2nd index.

- raman April 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Program to find the third largest

a=[1212121, 1,6,4,7,2,5,8,9,11,101,3,56,45,6,7,234,7,456]

for x in range(0,3):
print(x)
for i in range(len(a)-1):
if a[i] > a [i+1]:
b=a[i]
a[i] = a[i+1]
a[i+1] = b

print("Third largest is : ", a[len(a)-3])

- Nishant Raj May 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[1212121, 1,6,4,7,2,5,8,9,11,101,3,56,45,6,7,234,7,456]

for x in range(0,3):
    print(x)
    for i in range(len(a)-1):
        if a[i] > a [i+1]:
            b=a[i]
            a[i] = a[i+1]
            a[i+1] = b

print("Third largest is : ", a[len(a)-3])

- Anonymous May 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[1212121, 1,6,4,7,2,5,8,9,11,101,3,56,45,6,7,234,7,456]

for x in range(0,3):
    print(x)
    for i in range(len(a)-1):
        if a[i] > a [i+1]:
            b=a[i]
            a[i] = a[i+1]
            a[i+1] = b

print("Third largest is : ", a[len(a)-3])

- Nishant Raj May 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[1212121, 1,6,4,7,2,5,8,9,11,101,3,56,45,6,7,234,7,456]

for x in range(0,3):
    print(x)
    for i in range(len(a)-1):
        if a[i] > a [i+1]:
            b=a[i]
            a[i] = a[i+1]
            a[i+1] = b

print("Third largest is : ", a[len(a)-3])

- rajnishant.nk May 02, 2016 | Flag Reply


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