Amazon Interview Question for Software Engineer in Tests


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
50
of 52 vote

1. XOR all the n numbers.
2. Result will be knocked out for all the even pairs as a^a=0 The result now contains only XOR of the two odd out numbers.
3. Find the first bit position in the result that is 1. Definitely this bit position both the odd numbers have different bit values. i.e. one has a 0 and another has a 1 at this position. Let this position be x
4. XOR the elements that have 1 at x bit position and XOR the elements that have 0 at x bit position. The two XOR results would give the two odd count numbers.

- Expressions March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 votes

last line of algorithm does not lead to result...so problem is, how could we find two numbers by knowing their XOR.

- minku March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 votes

Hey Minku,
The algo dfinitely leads us to result. You will be XORing two different sets and each of them will have the two numbers separately. I am posting my implementation.

public class Main {

	public static void main(String[] args) {
		int a[]={2,3,2,3,3,4,5,4,2,2,5,6};
		Integer XOR = null;
		for(int i=0;i<a.length;i++){
			if(XOR==null)
				XOR=a[i];
			else
				XOR^=a[i];
		}
		int position=findFirstBitWithSetBit(XOR);
		
		Integer XOR0=null, XOR1=null;
		for(int i=0;i<a.length;i++){
			if(getBitAtPosition(a[i], position)==0){
				if(XOR0==null)
					XOR0=a[i];
				else
					XOR0^=a[i];				
			}else{
				if(XOR1==null)
					XOR1=a[i];
				else
					XOR1^=a[i];						
			}				
		}		
		
		System.out.println(XOR0);
		System.out.println(XOR1);
	}
	
	public static int getBitAtPosition(int x, int position){
		return (x>>position)&;1;
	}
	
	public static int findFirstBitWithSetBit(int x){
		int position=0;
		while((x&;1)!=1){
			position++;
			x=x>>;1;			
		}
		return position;
	}
}

Time Complexity: O(n)
Space Complexity: O(1)

- Expressions March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution

- siva.sai.2020 March 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude, Hats off to solution

- Anonymous June 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

It took me a few minutes to grasp how he dragged the numbers out of the XORed bits.

The trick to it is that because we're trying to find the two numbers that was set in an ODD number of times.

So, if the bit was a "1". 1 XOR 1 = 0 XOR 1 = 1
So if there is a 1 in the XORed bit set, and because the sum of two odd numbers is always even. Anything XORed an even number of times. = 0

Thus, for all 0's in the XORed bit set, there must have been 1 in the original bits. And for all 1's in the XORed bit set, both original bit sets have to contain a 0 or 1 respectively.

- Anon September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What are the semicolons in "((x&;1)!=1)" and "x=x>>;1" ?????
Are they typos?

- gee February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Beautiful!

- pretentious.bastard February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int x(0), y, a(0), b(0);
    for (auto i : A)
        x = x ^ i;
    y = (x & -x);
    for (auto i : A)
        if (y & i) a = a ^ i;
        else b = b ^ i;
    cout << a <<' '<< b << endl;

Time Complexity: O(n)
Space Complexity: O(1)

- This algo is beautiful and extremely easy to implement December 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, I think your solution is wrong,
array: 3 5 2 4 3 5
Now, XOR of 3,5 = 6 and, XOR of 2,4 is also 6.
Your algorithm will fail miserably.

- Hasit Bhatt February 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
19
of 19 vote

Simpler solution at the cost of space:

- Create a hash set
- Loop the integers
-- If the element is in the hash set, remove it
-- Otherwise, add it (since it's not already present)
- The answer is the contents of the hash set (which scales to any number of odd numbers)

Complexity: O(n) (due to an O(1) get/put hash set).

- jay March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correct...
in case we do not want to us collections....
we can have an Array of size [unique occurrence of elements]
with a simple logic to check 'If the element is not present in Array add it otherwise remove the occurrence in Array.

- PKT March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 5 votes

@PKT
Your solution has a space complexity of O(n) and time complexity of O(n^2).

- Expressions March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Eugene
The comment was for PKT's solution where an array is being used and looked up every time.

- Expressions March 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Next code in javascript returns array of two needed numbers (8,4)

var inn=[1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,9,1,2,3,4,1,2,3];
var out = {};
for(var i=0; i<inn.length; i++)    {
	if(out[inn[i]]==null)
	   out[inn[i]]=inn[i];
    else
        delete(out[inn[i]]);
    }

- klec@speroteck.com July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public List<Integer> findNumbers(int[] a) {
		List<Integer> list = new ArrayList<Integer>();
		
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		
		for(int i=0; i<a.length; i++) {
			Integer key = new Integer(a[i]);
			if (map.containsKey(key)){
				Integer count = new Integer(map.get(key).intValue() + 1);
				map.put(key, count);
			} else {
				map.put(new Integer(a[i]), new Integer(1));
			}
		}
		
		for(Integer key: map.keySet()) {
			Integer value = map.get(key);
			if (value.intValue()%2 != 0) {
				list.add(key);
			}
		}
		
		return list;
		
	}

- lin March 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How would you solve this in JAVA?

- satyajeettripathy March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hey Satyajeet here you go. Should work without any change. Please let me know as I could not verify this.

public class Main {

	public static void main(String[] args) {
		int a[]={2,3,2,3,3,4,5,4,2,2,5,6};
		Integer XOR = null;
		for(int i=0;i<a.length;i++){
			if(XOR==null)
				XOR=a[i];
			else
				XOR^=a[i];
		}
		int position=findFirstBitWithSetBit(XOR);
		
		Integer XOR0=null, XOR1=null;
		for(int i=0;i<a.length;i++){
			if(getBitAtPosition(a[i], position)==0){
				if(XOR0==null)
					XOR0=a[i];
				else
					XOR0^=a[i];				
			}else{
				if(XOR1==null)
					XOR1=a[i];
				else
					XOR1^=a[i];						
			}				
		}		
		
		System.out.println(XOR0);
		System.out.println(XOR1);
	}
	
	public static int getBitAtPosition(int x, int position){
		return (x>>position)&1;
	}
	
	public static int findFirstBitWithSetBit(int x){
		int position=0;
		while((x&1)!=1){
			position++;
			x=x>>1;			
		}
		return position;
	}
}

Time Complexity: O(n)
Space Complexity: O(1)

- Expressions March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What does ;1 means in above code?? Plz ignore my ignorance..I am new to programming...

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

That is a typo that came in automatically. remove the semicolons at both the places.
It stays even after editing and updating.

- Expressions March 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My java solution in O(n):

import java.util.*;

public class Array_Odd_Occ {
	public static void main(String[] args) {
		int a[]={2,3,2,3,3,4,5,4,2,2,5,6};
		findNumbers(a);
	}
	
	private static void findNumbers(int [] a) {
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		
		for (int i = 0; i < a.length; i++) {
			Integer freq = map.get(a[i]);
			map.put(a[i], (freq == null) ? 1 : freq + 1);
		}
		
		for(Map.Entry<Integer, Integer> nums : map.entrySet()) {
			if(nums.getValue() % 2 != 0)
				System.out.println(nums.getKey());
		}
	}
}

Output:

3 6

- Sam March 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void find_odd_repeating_numbers(int[] array) 
	{
		Map<Integer, Integer> nums = new HashMap<Integer, Integer>();
		for(int i : array)
		{
			if(nums.containsKey(i))
				nums.remove(i);
			else
				nums.put(i, 0);	
		}
		System.out.println(nums);
	}

- mksuraj83 April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. XOR all the numbers to get c = a^b
2. reiterate the array and get d = c^arr[i] & e = c^arr[i]'
if (d = e')
arr[i] is one number
use the concept:
d = (a^b)^a = b
e = (a^b)^a' = b'

- Ajit April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package linearData;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class OddOneOut {
	public static void main(String[] args) {
		int a[] = {1,2,3,4,2,1,3,6,6,6};
		int length = a.length;
		HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
		
		for(int i = 0; i < length; i++ )
		{
			if(hm==null)
			{
				hm.put(1, a[i]);
			}
			else
			{
				if(hm.containsKey(a[i]))
				{
					int val = hm.get(a[i]);
					val++;
					hm.put(a[i], val);
				}
				else
					hm.put(a[i], 1);
			}
		}
		
		Set s = hm.entrySet();
		//Map<T, E> map 
		for (Entry<Integer, Integer> entry : hm.entrySet()) {
	         if (entry.getValue()%2!=0) {
	        	 System.out.println(entry.getKey());
	         }
	     }

	}
}

- ersandeshsharma April 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#You are given an integer array, where all numbers except for
#TWO numbers appear even number of times. 
#Q: Find out the two numbers which appear odd number of times.

intarr = [1,2,3,4,5,6,7,8,9,10,1,2,4,5,6,7,8,9]


def find_two_ints_not_even(intlist):
    odd_int={}
    retlist=[]
    for _int in intlist:
        if _int in odd_int:
            odd_int[_int]=odd_int[_int]+1
        else:
            odd_int[_int]=1
    for val1 in odd_int:
        if odd_int[val1] % 2 == 1:
            retlist.append(val1)
        if len(retlist)==2:
            return retlist
    return retlist

print find_two_ints_not_even(intarr)

- mikeolteanu September 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I had hard time understanding the solution(s) mentioned here. So if you guys are facing the same problem here is the link for better understanding. Upvote so everybody can see.
geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/

- Saurabh2816 July 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple solution in python!

def findOddElement(arr):
	D = [] 								#final list of items that will be returned
	for item in arr:
		if arr.count(item) % 2 != 0: 	#check if odd
			if D.count(item) != 0: 		#value was already added to list
				D.append(item)
	return D

- Shawn Garg October 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List<Integer> list = new ArrayList();
	for(int num: arr){
		if(list.contains(num){
			list.remove(num);
		}
		else{
			list.add(num);
		}
	}
	System.out.println(list);

- turukmakto1990 December 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep a hashmap with the integers as keys. Delete repeats (evens). O(n) time, but uses additional space, up to about almost n. There could be a lot of unique (1-count) entries.

function findOddAppearances(list) {
	var numMap = {};

	list.forEach(function(num) {
		if (numMap[num]) {
			delete numMap[num];
		} else {
			numMap[num] = true;
		}
	});

	return Object.keys(numMap);
}

var input = [0,0, 1,1, 2,2,2, 3,3, 4,4,4,4, 5,5,5, 6,6];

console.log(findOddAppearances(input)); // => [2,5];

- calvin.rachuy February 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How does this logic works?I mean what is the logic with finding first bit with 1 not the second, not the 3rd ?

- 27priancy.gahlot April 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = [2,3,2,3,3,4,5,4,2,2,5,6]
res = []
for v in a:
    if a.count(v) %2 != 0:
        res.append(v)

print(set(res))

- Callum , this could be the possible answer for python March 28, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using Javascript object

var array = [2, 3, 2, 3, 3, 4, 5, 4, 2, 2, 5, 6];
var count = {};
var result = {};
array.forEach(item => {
  if (count[item]) {
    count[item] = count[item] + 1;
  } else {
    count[item] = 1;
  }
  if (count[item] % 2 !== 0) {
    result[item] = count[item];
  } else {
    delete result[item];
  }
});

Time Complexity: O(n)

- Shahid ahmad February 13, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

1. XOR all the n numbers.
2. Result will be knocked out for all the even pairs as a^a=0 The result now contains only XOR of the two odd out numbers.
3. Find the first bit position in the result that is 1. Definitely this bit position both the odd numbers have different bit values. i.e. one has a 0 and another has a 1 at this position. Let this position be x
4. XOR the elements that have 1 at x bit position and XOR the elements that have 0 at x bit position. The two XOR results would give the two odd count numbers.

- Anonymous March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

last line of algorithm does not lead to result...so problem is, how could we find two numbers by knowing their XOR.

- minku March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you really find some answer helpful, please up-vote. It helps people in finding valid answers. :)

- Expressions March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Expressions: Unregistered users cannot vote I believe. But there is a bigger problem. This should be a comment on the answer for which it was intended.

- Loler March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad, failed to see the <anonymous/>

- Expressions March 27, 2013 | 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