Amazon Interview Question for Software Engineer / Developers






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

a XOR a = 0
a XOR 0 = a
XOR all elements in array, the only number that occurs odd no. of times, will be the resultant

- Cuppanomics May 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That would give n^2 solution, right? (XOR)

- Oki May 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, it's O(n)

- gevorgk May 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how is it O(n) ?
array is not sorted..

- guest May 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

excellent solution @Cuppanomics
it is in fact O(n), the array need not be sorted for this.. try writing a function.

int func(int *A, int n)
{
    int ret = 0;
    for(int i=0;i<n;i++)
    {
        ret = ret^A[i];
    }
    return ret;
}

- Kartik May 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if the number just occur one time

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

it is irrelevant if the number occurs once or any odd number of times (3, 5, 7, ..). The important thing is that there is only one such number that occurs an odd number of times. When XOR'ing the numbers, all even number will have a pair which will cancel it out. It doesn't matter how many times that even number occurs as the important thing is that all of them can be paired and thus canceled out.

For odd numbers the same thing applies. There will always be one lone element that cannot be paired with anything else. If it occurs 1 time, that is our element. If it occurs 3 or more times, then there will be 2n pairs that cancel out, and the 1 remaining element will be in our answer.

Read up more about XOR and commutativity and associativity at Wikipedia if you still have doubts.

- Mishra.Anurag June 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Consider the following two input arrays:
1) {1,2,3,1,2,3,0,}
2) {1,2,3,1,2,3)
The XOR-ing algorithm will return 0 for both these inputs, but the correct output should be:
1) 0 occurs odd number of times.
2) No number found with odd number of occurences

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

Use a HAsh Table:
Algo:
1. While(i < arr.length)
{
if(HashTable.Contains(arr[i])
HashTable.Remove(arr[i]);
else
HashTable.Add(arr[i]);
i++;
}
if(HashTabl.isEmpty != TRUE)
{
return HashTable[arr[0]];
}
else
{ "NO ELEMENT WITH ODD OCCURENCE";
}

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

question itself states that no of elements are odd, so if exor comes 0 means answer is 0

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

public int FindOddTimesOccurrence(int[] ar)
        {
            HashSet<int> values = new HashSet<int>();
            foreach (int num in ar)
                if (values.Contains(num))
                    values.Remove(num);
                else
                    values.Add(num);
            return values.First();
        }

An exception will be thrown if there is number that occurs odd number of times. This solution requires an extra data structure- a set and runs in o(n). If there are ideas to this I would be glad to learn.

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

ok, I think there's another way of solving half of the problem:
sum up all the elements and if the sum is odd, we have an odd number odd number of times.
Divide the array in half and check the sum of one half and fetch the half whose sum is odd again. Keep doing this unless you get your number.
O(N) time and O(1) space.
But if your number is even, then we have to think of something else....

- Anonymous May 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it is the worst idea out here...

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

@Cuppanomics

Awesome idea ...

- Div May 21, 2010 | 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