Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

If there is only one element that occurs odd number of times...
int value = 0;
for (int i =0; i<size;++i)
value = value ^element[i];
print value;

- Mars.LeeZm November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very nice idea. If there are more than one to find, we may need to use hash table.

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

Wow I wish I thought of that during the interview

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

Is it really working? Can you explain little bit more about how this program is working?
With the following [ 1, 2, 3, 3, 1, 3 ] input, output shows 1, instead of 3.

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

Syed: The example only works if there is one odd number 2 and 3 both occur an odd number of times so it wouldn't work in this case.

This works because when a number xor's with it self the result is zero.
1^1 = 0, 2^2=0,...
So if all values occur in pairs except for 1, the result is the odd one out.

- Kyle November 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

public static void occurOddTimes(int[] arr){
		Hashtable<Integer,Object> ht = new Hashtable<Integer,Object>();
		Object seenOdd = new Object();
		Object seenEven = new Object();
		Integer intr;
		
		for (int i = 0; i< arr.length ; i++)
		{
			intr = new Integer(arr[i]);
			if(!ht.containsKey(intr)){
				ht.put(intr, seenOdd);
			}
			else{
				if(ht.get(intr)  == seenOdd){
					ht.put(intr, seenEven);
				}
				else{
					ht.put(intr, seenOdd);
				}
			}
		}
			Set<Integer> set = ht.keySet();
			Iterator iter = set.iterator();
			while(iter.hasNext()){
				intr = (Integer) iter.next();
				if(ht.get(intr) == seenOdd){
					System.out.println(intr);
				}
			
		}
		
	}

Algorithm:

1) iterate through array.
if key doesn't exist in hashtable put it as seenOdd.
else toggle the values between seenOdd and seenEven

I have printed the values which occur odd number of times. You can add it to arraylist

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

Best approach to achieve O(N) and O(1) is to do XOR with 0. Even number of XORs will always return the number you are XORing with. For example, 0^6^6 = 0 and 0^6 = 6. So, code is -

public int findOdd(int[] input, int length) {
		int toReturn = 0;
		for (int i = 0; i < length; i++) {
			toReturn ^= input[i];
		}
		
		return toReturn;
	}

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

Hi Sam,

Imagine a case where we have multiple numbers occuring odd number of times...XOR will take the solution for a toss.. won't it...??

- Srivathsan November 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

For the case when there is more than one number occurring odd number of times, use the following algorithm

create a hash table 
for each x in array
    if the x is found in the hash table
        remove it from the HT
    else
        add it to the HT

The resulting hash table will eventually contain all the elements that occur odd number of times.

- ashot madatyan November 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we could use LinkedHashMap for faster iteration entries in the hash table

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

Best approach to achieve O(N) and O(1) is to do XOR with 0. Even number of XORs will always return the number you are XORing with. For example, 0^6^6 = 0 and 0^6 = 6. So, code is -

public int findOdd(int[] input, int length) {
		int toReturn = 0;
		for (int i = 0; i < length; i++) {
			toReturn ^= input[i];
		}

		return toReturn;
	}

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

from 1 to n just xor all the array elements ...resultant is the value which is repeated for odd no of times

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

if the array contains multiple elements with odd count, then to display the odd count elements with their no. of occurrences can be coded as:

int arr[] = {1,2,1,3,2,5,51,91,1,1,1};
		Map<Integer,Integer> oddCount = new HashMap<Integer,Integer>();
		Map<Integer,Integer> evenCount = new HashMap<Integer,Integer>();
		for(int i=0;i<arr.length;i++){
			if(oddCount.containsKey(arr[i])){
				evenCount.put(arr[i],oddCount.get(arr[i])+1);
				oddCount.remove(arr[i]);
			}else if(evenCount.containsKey(arr[i])){
				
				oddCount.put(arr[i],evenCount.get(arr[i])+1);
				evenCount.remove(arr[i]);
			}
			else{
				oddCount.put(arr[i],1);
			}
		}

Ankita Jain

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

int arr[] = {1,2,1,3,2,5,51,91,1,1,1};
		Map<Integer,Integer> oddCount = new HashMap<Integer,Integer>();
		Map<Integer,Integer> evenCount = new HashMap<Integer,Integer>();
		for(int i=0;i<arr.length;i++){
			if(oddCount.containsKey(arr[i])){
				evenCount.put(arr[i],oddCount.get(arr[i])+1);
				oddCount.remove(arr[i]);
			}else if(evenCount.containsKey(arr[i])){
				
				oddCount.put(arr[i],evenCount.get(arr[i])+1);
				evenCount.remove(arr[i]);
			}
			else{
				oddCount.put(arr[i],1);
			}
		}

- Ankita N November 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Example3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Integer[] input = { 1,1,2, 2, 3, 3,3 ,4};
		Map<Integer, Integer> output = new HashMap<Integer, Integer>();
		for (int i = 0; i < input.length; i++) {
			if(output.get(input[i]) != null) {
				continue;
			}
			int count = checkCount(input, i);
			output.put(input[i], count);
			if(count%2 != 0) {
			System.out.println("The Number:-"+input[i]+"Has Count of"+count);
			}			
		}
	}

	private static int checkCount(Integer[] input, int i) {
		int count = 1;
		for (int j = 0; j < input.length; j++) {
			if (j != i) {
				if (input[i] == input[j]) {
					count++;
				}
			}
		}
		return count;
	}

}

- Rahul Jain November 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution I am posting will return all elements that occur an odd number of times. I am not sure if that is what is entirely desired, but hey, food for thought. Also, there is no need for maps/hashmaps

public static ArrayList<Integer> findOdd(int[] a) {
		int max = 0;
		for (int i = 0;i<a.length;i++) {
			if (a[i] > max) {
				max = a[i];
			}
		}
		int[] counts = new int[max+1];
		for (int j = 0; j < a.length; j++) {
			counts[a[j]]++;
		}
		ArrayList<Integer> oddCounts = new ArrayList<Integer>();
		for (int k = 0;k<counts.length;k++) {
			if ((counts[k] % 2) == 1 ) {
				oddCounts.add(k);
				System.out.println(k);
			}
		}
		return oddCounts;
	}

- M.Zimmerman6 November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You do realize you are using the array counts as a hashmap, right?

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

Won't this approach cause a lot of memory wastage?
Suppose the array is {1,2,2,30099}, then according to this approach an array of size 30100 will be allocated, which indeed will result in a more unused space..

- SA January 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a Binary search tree of struecture containing counter of that digit. how many times it occurs. Trace counter for odd 1 and print it . Below is my solution

#include <stdio.h>
#include <stdlib.h>
// structue for node
typedef struct node
{
        int data;
        int count;
        struct node *right;
        struct node *left;
} NODE;

/*
This function is for inserting new node in binary tree.
*/
void insert(NODE **root, int data)
{
NODE *temp;
if(*root == NULL)
{
        temp=(NODE *)malloc(sizeof(NODE));
        if(temp == NULL)
        {
                printf("Unable to allocate memory");
        }
        else
        {
                temp->data=data;
                temp->count=1;
                temp->left=NULL;
                temp->right=NULL;
                *root=temp;
        }
}
else if(data < (*root)->data)
{
        insert(&(*root)->left,data);
}
else if(data > (*root)->data)
{
        insert(&(*root)->right,data);
}
else if(data==(*root)->data)
{
        (*root)->count=(*root)->count +1;
}
}
void traverse(NODE **root)
{
if((*root)!=NULL)
{
        if(((*root)->count)%2!=0)
        printf("Value %d comes odd times\n",(*root)->data);
        traverse(&(*root)->left);
        traverse(&(*root)->right);
}
}
int findOdd(int *input, int length)
{
int i;
NODE *root=NULL;
for(i=0;i<length;i++)
{
insert(&root,input[i]);
}
traverse(&root);
}

int main(void)
{
int a[]={1, 2, 3, 1, 2, 3, 4, 5, 9, 10};
findOdd(a,10);
return 0;
}

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

Below is output of above program
Value 4 comes odd times
Value 5 comes odd times
Value 9 comes odd times
Value 10 comes odd times

Please let me know if any better solution is available

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

In scala it is a one liner.

scala> val arr = Array(1,2,1,4,3,2,5,3)
arr: Array[Int] = Array(1, 2, 1, 4, 3, 2, 5, 3)

scala> arr.map(e => (e, arr.count(x => x == e))).distinct
res41: Array[(Int, Int)] = Array((1,2), (2,2), (4,1), (3,2), (5,1))

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

My answer was:
int findOdd(int[] input, int length) {
if(input.size() == 1)
{
return input[0];
}
sort(&input);
counter = 1;
for(int i = 1; i < input.size(); i++)
{
if(input[i - 1] < input[i])
{
if(counter % 2 == 1)
return input[i - 1];
else
counter = 0;
}

counter++;
}
return input[input.size() - 1];
}

- lilzh4ng November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

If we are not allowed to sort:

#include <iostream>

using namespace std;

int findOdd(int input[], int length)
{
    int count = 0;

    for(int i = 0; i < length; i++)
    {
        count = 1;

        for(int j = 0; j < length; j++)
        {
            if (i == j) continue;

            if (input[j] == input[i])
                count++;
        }

        if (count % 2 == 0)
            continue;
        else
            return input[i];
    }
}


int main()
{
    int input[] = {1,2,3,2,1,2,3,2,1};
    int odd = findOdd(input, 9);
    cout << odd;

    return 0;
}

- Anonymous November 21, 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