Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

If space complexity is not an issue, why not store the occurrence frequency in an array at the index matching the number we are processing. Thats O(n). Next in another parse, print out the numbers that have even frequencies. Thats the second O(n) operation.

- kapnik November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in first iteration count the frequency and store in a hash.
print the value from hash where frequency is even.

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

To count the frequency for every number by simple traversing, that will take O(n^2) . Even if you use hash after that, still the code wont be optimal.

This whole program can be made in O(n).

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

O(n) for finding frequencies and O(n) for printing
can't we do better than this?

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

Rather than counting the frequency, we can flip a boolean on each occurrence of the integer. (saves space in the hash, and the time for mod 2 operation later on)

I also cannot think of an algo better than O(n).

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

yup, that good idea

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

fist sort the array with O(n logn) complexity.
then just traverse the array and by counting adjacent occurrences you can print the integer with even frequency in O(n) complexity.

this thing also gives O(n) complexity but without using extra data structure.

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

But this method takes time for sorting.

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

if you use a algorithm which sort the array in (n log n)
and traverse the array to print consecutive even occurrences then it will take O(n).
so over all complexity becomes O(n)+O(n log n)
which is equivalent to O(n).

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

one question, how to print these numbers? print numbers together with same frequency or just print the number with its frequency?
if it is required to print the numbers with the same frequency together, we need to store these numbers in an bucket which is indexed by frequency, and traverse this array to print.

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

initialize a count variable in loop increment it with each same occurrence when no changes check for the count if it is even print the no if it is odd then reset the count and start from next element.

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

@vishu: I think that the complexity would be the worse of O(n logn) and O(n). Therefore the complexity of your solution is O(n logn). What do you think?

- sethia November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I doubt O(n)+O(nlogn) will be equivalent to O(n). It should be O(nlogn)

- pradegup November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sry i made a mistake there its n log n.

- vishu November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you cannot boil O(n) + O(n log n) to O(n).
It stays that O(n) + O(n log n).
You can convert O(10 n) to O(n), but I dont think you can do it as I mentioned above.

- Illusion November 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is order n solution

{

private static Set<Integer> getEvenFrequencyNumber(int[] arr) {
		// TODO Auto-generated method stub
		HashMap<Integer, Boolean> hashMap = new HashMap<>();
		for(int a : arr){
			Boolean x = hashMap.get(a);
			if(x == null)
				hashMap.put(a, false);
			else
				hashMap.put(a, !x);
		}
	 Set<Integer> ar = hashMap.keySet();
	 
	 Iterator<Integer> iterate = ar.iterator();
	 while(iterate.hasNext()){
		 if(!hashMap.get(iterate.next()))
		 	 iterate.remove();
	
	 }
	 System.out.println(ar);
	 	return ar;
	}

}

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

/* simple hashing and uses counting sort */
#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

int main()
{
int i;
int a[] = {3, 4, 3, 4, 3};
static int count[SIZE]; /* static is just for zeroing */

/* find out the frequency of the elements and store in it's
respective indexes such as if the number is 4 it will go
to 4 index and so on and once this is done we will have all
the frequency of the numbers in it's corresponding index stored*/
for(i = 0; i < SIZE;i++)
count[a[i]]++;

for(i = 0; i < SIZE;i++)
printf("%d\n", count[i]);

printf("\n");

for(i = 0; i < SIZE;i++) {
if(count[i] && count[i]%2 == 0)
printf("%d\n", i);
}
return 0;
}

- anish[1] October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if we have negative numbers .

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

It won't work. Try a[5] = {0, 100, 10000, 1000000000000}, how could you define an array of 1000000000000000?????

- peter tang October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java: Use a hashset, traverse the array, if we can find it, remove it, otherwise add it to the set, then print all numbers in the set.

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

In the end It will print number with odd frequencies

- yogi October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

get the sorted array a[]={1,1,2,2,3,4,4,4};
int freq = 0;
for(i=0; i<size-1; i++)
{
	if(a[i]==a[i+1])
		freq++;
	else if(freq>1 && (freq&1 == 1) // more then once then only frequency is taken
		printf("%d\t", freq);freq=0;

}

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

I doubt if HASH table will work or not. It works only if the hash function is so good that there will be no collision for any number.

The safe way is to sort first then count the frequency. If use quicksort, average O(nlog(n)) + O(n), sorting plus traverse.

- peter tang October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if we assign a unique prime number to every unique integer in the array and store that in a table

for eg. arr[] = {2,3,4,5,4,6,9};
2 == 11,
3 == 13,
4 == 17,
5 == 19,
6 == 23,
9 == 29;

initialize mul ==1;
for each i = 1 to N
divide mul by a[i];
if divisible, then mul = mul/a[i];
else mul = mul*a[i];

finally, traverse from j = 1 to N to check if
a[j] divides mul, then it is odd frequency else if it does not, then even frequency.

- abhishek November 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not sure how you do "assign a unique prime number to every unique integer in the array"?

Think about an long array and with wide range and each integer has one hit. To do "assign a unique prime number to every unique integer in the array" will be a big job.

- peter tang November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<conio.h>
int main()
{
	int a[20],n;
	printf("Enter your size of array\n");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&a[i]);
	for(int j=0;j<n;j++)
	{
		int c=1;
		if(a[j]==-1)
			j++;
		for(int k=j+1;k<n;k++)
		{
			if(a[j]==a[k])
			{
				c++;
				a[k]=-1;
			}
			
		}
		if(c%2==0)
			printf("%d",a[j]);
	}
return 0;
}

- Anonymous October 31, 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