Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

You mean the element which appears at least n/2 times? This is a classic problem...

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

@Itcecsa
what is dominator? can you explain the question better?

- vinodhian May 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a dominator of a array is an element which appears more than N/2 times, where N is the size of the array.

- Itcecsa May 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

BST?

- Mike May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't work for {5, 4, 5, 4, 5, 4, 5, 4, 5}

- Anonymous May 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't work for {5, 4, 5, 4, 5, 4, 5, 4, 5}

- Anonymous May 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

step1:: We can find dominator in O(n) time. & O(1) Space complexity
step2 :: array size 7
e.g 1) 1112222 dominator 2
e.g 2) 2222111 dominator 2
e.g 3) 1212122 dominator 2
e.g 4) 1231231 there is no dominator
e.g 5) 1231233 there is no dominator
e.g 6) 3321233 dominator is 3

***************************************
note:: my below solution, could not find dominator for e.g 6) case
***************************************
step3:: take a variable count, cout =1 , denom= A[0].
step4 :: for (i =1 ;i<n ; i++)
increase count by 1 when current element is equal to dom elemet
decrease count by 1 when current element is not equal to dom elemtn

step 5:: if count > 1 then there is denominator , else there is no denominator

// Code for this solution

dom = A[0]; // initially assume dominator is first element
domFirstIndex = 0; // dominator first index location
count = 1;

for( i=1; i< n; i ++ )
{
	if( A[i] != dom )
	{
		count --;
		if( count == 0 || count <0 )
		{
			dom = A[i] ;
			count = 0 ;
			domFirstIndex = i; 
		}
	}
	else
	{
		count ++;
	}
}

if( count > 0 && (domFirstIndex <= n/2))
{
	printf("\n dominator is %d", dom);
}
else
{
	printf("\n there is no dominator \n");
}

- siva.sai.2020 May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your approach seems correct. I did not undrstnd y r u checkin for domFirstIndex<=n/2??

- farhana.mmmec May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dominator presents in array atleast n/2 times. So its first index location should be on or before n/2 th location.

If we do not check this condition, the above algo will return 3 as dominator in I/P "112233".

- siva.sai.2020 May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks. I get it now. But u see according to the question asked above we hav to return index of any element which contains dominator.. So if we go by your approach we'll get index of the dominator in domFirstIndex.. n to check for the case you mentioned we can loop thru the array one more time to count if the dominator occurs more than n/2 times.. that will make the complexity O(2n)~O(n).. Plz comment..

- farhana.mmmec May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, you are right. we need to loop one more time to print dominator all indexes.

- siva.sai.2020 May 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't work for {5, 4, 5, 4, 5, 4, 5, 4, 5}

- Anonymous May 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Saw a solution for this question at codingissue.com. It is also known as "super majority".

- MenuIsAlert May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Siva.Sai I checked your code and tried to analyze.
Lets say the input array is [3,1,3], here dominator is clearly 3.
According to your code:
Initialize: dom = 3 (i.e A[0]), domFirstIndex=0
Iteration 1: i=1, (dom!=A[i]), therefore count--, then count=0, dom=1, domFirstIndex=1
Iteration 2: i=2, (dom!=A[i]) therefore count--, then count=0, dom=2, domFirstIndex=2

After the loop ends, if (count>0 && domFirstIndex <= n/2), both conditions fail here but still there was a dominator in the input array.

Please correct me if I understood anything wrong.

- Learner May 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My solution is not perfect. It does not work for all cases. in your example case it does not give correct solution.
As I mentioned earlier my sol does not work for below e.g 6

e.g 1) 1112222 dominator 2
e.g 2) 2222111 dominator 2
e.g 3) 1212122 dominator 2
e.g 4) 1231231 there is no dominator
e.g 5) 1231233 there is no dominator
e.g 6) 3321233 dominator is 3

***************************************
note:: my solution, could not find dominator for e.g 6) case
***************************************

- siva.sai.2020 May 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are almost there! The if-else order matters.
See stackoverflow.com/questions/3740371/finding-the-max-repeated-element-in-an-array
The algorithm works for case 6 as well.

- N.Shailesh May 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The Boyer-Moore Majority Vote Algorithm fails to find correct majority in the below input arrays

int numbers[SIZE] = {1,2,3,4,1,2,3,4,1,2,3,4};

int numbers[SIZE] = {1,2,3,4,1,2,3,4,1,2,3,4,1};

- siva.sai.2020 February 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@sivasai,

Wont your logic fail for this input { 3,6,3,5,3,4,3,2,3}?

Please correct me if i am wrong..

- Anonymous August 10, 2012 | 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