Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Say if k = 3, then find elements that appear n/3 times in the array which means 1/3 of the size of the array. e.g 1,1,1,2,2,3,4,4,4,4,6,7 . So here 12 elements are there and 4 appears 4 times and that is what you need to print

- sowmya October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

is the list sorted?

- REAMS.AL October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No

- sowmya October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can it not be fixed by using simple hash map?
Start traversing from 0...to ...n elements and store the numbers in the hash map.Once you find the same element again just increment your count in the hashmap and do this step for all the elements.In the end just check the hasp map whose count matches the n/k.
Simple example:
hash[1]-count 1 input array[1]
hash[2]-count 1 input array[2]
hash[3]-count 1 input array[3]
hash[4]-count 1 input array[4]
hash[2]-count 2 input array[2]
In the end just scan the hash map looking for count == n/k

- anon October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Question is not clear, 1/k will always be less than 1,

- sourav October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

i gezz has map is best way to do..
Moore's voting algorithm if N/2 would have been the case !!

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

1/k? does not look correct logically...or else it should be k..

- REAMS.AL October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the question is k times not 1/k.

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

If it's k, you can easily do it using hashtables.

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

Sorry n/k times

- sowmya October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

here is the algo for this....
1:Sort the array.
2:initialize iCounter = 0, Value = 0;
3:for i = 0 to n-1 ( to traverse all the elements)
if (value == SortedArray[i])
{
iCounter++;
}
else
{
if( iCounter >= n/k )
{
store the variable & counter value....
increase Total number of variables by one.....(At the top of loop, you should initialize by zero)
}

iCounter = 0;
value = SortedArray[i];
}
4:Print all the variables with occurs atleast n/k time.

- Kuber Saini October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

answer is counting sort

- Amitesh Madhur October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi, I do not really get counting sort, can you do me a favor to explain this?

- amyue.xing November 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is the algo for this....
1:Sort the array.
2:initialize iCounter = 0, Value = 0;
3:for i = 0 to n-1 ( to traverse all the elements)
if (value == SortedArray[i])
{
iCounter++;
}
else
{
if( iCounter >= n/k )
{
store the variable & counter value....
increase Total number of variables by one.....(At the top of loop, you should initialize by zero)
}

iCounter = 0;
value = SortedArray[i];
}
4:Print all the variables with occurs atleast n/k time.

- Kuber Saini October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code in java , tested - Working

int n =12;
		int k=3;
		int m = n/k; 
		int i=0,j=0,c=0;
		int a[] = {1,1,1,2,2,3,4,4,4,4,2,6};
		
		for(i=0;i<n;i++)
		{
			c=0;
			
			for(j=0;j<n;j++)
			{
				 if(a[i]==a[j])
				 {
					 c++;
				 }
			}
			
			if(c==m)
			{
				System.out.println("The number that appears n/k = "+a[i]);
				break;
			}
		}

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

If we solve it using HashMap space complexity is O(n) and time complexity is O(n). If we don't use extra memory, Sort the array and linear traversal through the array to find the number. O(nLogn). Are there any other techniques which can allow the problem is solved in less than O(nLogn) without using extra space.

- Eswar October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//n is the length, k is any number is array a, max is the max integer number in array a[]
int findNKtimesNumber(int a[], int k, int max, int n)
{
int *temp=NULL;
temp = (int *)malloc(sizeof(int)*(max+1));
memset(temp, 0, sizeof(int)*(max+1));

int i;
int rtimes = n/k;

for(i=0; i<n; i++)
{
temp[a[i]] = temp[a[i]] + 1;
}

for(i=1; i<=max; i++)
{
if(temp[i] == rtimes)
printf("%d appears %d times\n\r", i, rtimes);
}
}

- Bin October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def nthTimes(a:Array[Int],k: Int): Array[Int] = {
 val n = a.length/k
 a.filter(e => a.count(elem => e == elem) == n)
}

scala> val a = nthTimes(Array(8,3,5,6,1,6,9,0,-1, 78,45),5)
a: Array[Int] = Array(6, 6)

- rbsomeg February 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use BST

- Vaibhav February 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is variation of Majority Element Algorithm.

- Naveen May 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

hi

- RK October 25, 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