Amazon Interview Question for Analysts






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

<pre lang="" line="1" title="CodeMonkey85273" class="run-this">public class FindTheGuyAppearingEvenTimes {

public static int find(int[] arr) {
Set<Integer> set = new HashSet<Integer>();

// Due to HashSet's nature, the set will contain every number once after
// the loop
for (int i : arr) {
set.add(i);
}

// Check all numbers, if a number exists in the set already, remove it,
// otherwise add it to the set
for (int i : arr) {
if (set.contains(i)) {
set.remove(i);
} else {
set.add(i);
}
}

// Now the set contains only one number, which appears even times
return set.toArray(new Integer[0])[0];
}

public static void main(String[] args) {
int[] arr = { 5, 5, 5, 6, 6, 9, 9, 9, 9, 9, 123, 250, 560, 720, 720,
720 };
System.out.println(find(arr));
}
}
</pre><pre title="CodeMonkey85273" input="yes">
</pre>

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

can we use hashmap?Store the integers as keys and number of times it occurs as values.and get the key whose value %2 == 0.
time complexity will be o(n) for put and o(1) for get.

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

I am also thinking why no body is talking about hash map..

- Aks September 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am curious about how get has O(n) complexity, don't we need to traverse through the keys sho it should be O(n), isn't it ?

- rk.karn32 January 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

IF the question is like this that all number are even time and one is odd time..
It will become very easy...;)
just take XOR

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

yes. But this is in other way.
I have suggested binary tree , where , if node is missing , will add node in that position. If element is there , will remove that element.

So at the end , the tree contains all elements , except the even count number.

Once tree is built , we will check for the missing element .

But I think complexity for missing element is O(nlogn) .. (please correct me if complexity is wrong)

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

If we are going for o(nlogn).
Than why don't we just sort the array and find out the required element.

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

Is the range of the integers given?

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

should be binary SEARCH tree i thinkā€¦

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

I gave O(nlogn) algo , but the person is expecting o(n) complexity algo.

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

<pre lang="" line="1" title="CodeMonkey50645" class="run-this">Actually the code above can be improved by using xor after first loop.

public class FindOnlyEvenAppearanceAmongOddOnse {

public static int findTheGuyAppearingEvenTimes(int[] arr) {
Set<Integer> set = new HashSet<Integer>();

// Due to HashSet's nature, the set will contain every number once after the loop
for (int i : arr) {
set.add(i);
}

int ret = 0;
for (int i : set.toArray(new Integer[0])) {
ret ^= i;
}
for (int i : arr) {
ret ^= i;
}

return ret;
}

public static void main(String[] args) {
int[] arr = { 5, 5, 5, 9, 9, 9, 9, 9, 123, 250, 560, 720, 250, 720, 720 };
System.out.println(findTheGuyAppearingEvenTimes(arr));
}
}</pre><pre title="CodeMonkey50645" input="yes">
</pre>

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

+1

First scan the array to obtain unique elements, then XORing additionally these elements converts the problem of ODD-EVEN to EVEN-ODD problem again.

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

do the XOR of input array as part of first iter, will save you one full pass!

Btw, if you are adding the elem to the hashset, why not count the no. of times it occurs, and check for even/odd in next iteration over hash set?
You are doing a O(n) + O(k) + O(n), the above will do it in O(n) + O(k) where k is the set of unique no.s

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

1. OR all the numbers
2. XOR all the numbers
3. XOR the result of 1 and 2 you will get the answer

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

Did you try your solution before you posted?

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

e.g 2,5,5

2 = 010
5 = 101
5 = 101

OR all of them = 010|101|101 = 111 = R1
XOR all of them = 010^101^101 = 010 = R2
XOR R1 , R2 = 111^010 = 101 = 5 which occurs even number of times.

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

XORing a number even times result in 0 and 0 XOR any number is equal to original number.
But, note that XOR can not distinguish 1 ^ 2 from 3
ie. arr = [ 1, 2, 3, 5, 5, 6]

OR = 7
XOR = 6
OR ^ XOR = 1 <= wrong answer.

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

what if we maintain 2 lookups? but we need to know the maxi number in the array.. or use realloc to increase the sizes of these array dynamically.

lookup1 - puts a 1 against the number when it appears once.. if it appears again then puts 0
lookup2 - turns on(1) when the number changes from 1-0(even) in lookup1 and viceversa when 0-1.

so ultimately when we are traversing the array and updating these lookups O(n).. and lookup2 will have only one entry which will have 1 in it.. so go thru each element in the array and check for this 1.. Therefore O(2n) = O(n)

but the space complexity issue arises if the numbers are large.. the lookups will increase..

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

what if we maintain 2 lookups? but we need to know the maxi number in the array.. or use realloc to increase the sizes of these array dynamically.

lookup1 - puts a 1 against the number when it appears once.. if it appears again then puts 0
lookup2 - turns on(1) when the number changes from 1-0(even) in lookup1 and viceversa when 0-1.

so ultimately when we are traversing the array and updating these lookups O(n).. and lookup2 will have only one entry which will have 1 in it.. so go thru each element in the array and check for this 1.. Therefore O(2n) = O(n)

but the space complexity issue arises if the numbers are large.. the lookups will increase..

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

consider array 1 2 2 3 4 3 4 4 3
algorithm
int a[4]={-1}//all elements
for i=0 to end of array
if(a[array[i]]<0)a[array[i]++ //add if odd
else a[array[i]-- //sub if even

now which ever element in array "a" has -1 as its value is the number which is repeated even time.. time complexity is O(n)

- viresh a s August 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Get binary equivalent of the number.
2) all odd number have 1 at end , even number have 0.
3) Check if the number has 0 in end. That's the number.
e.g 8 -> 1000.

- Shrikant December 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{}

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

Well, can we make it a little bit more complicated: if there are more than one numbers repeated even numbers of time. Then how to print those?

- diganta4 July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

(1) xor all the elements and say the result of xor be in 'ans' variable.
i.e
int ans = 0;
for(int i = 0; i < n; i++)
{
ans = ans ^ a[i];
}

(2) Then xor ans with each element seperately and check if the value of ans changes. If it doesn't change then that is the corresponding number i.e

for(int i = 0; i < n; i++)
{
if( ans ^ a[i] == ans )
return a[i];
}

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

this piece of code does not work :P

- software engineer November 06, 2011 | Flag


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