Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

guilhebl's answer is good, but you don't need to keep track of a count. This works without storing that extra data:

1. Create a HashSet.

2. Traverse array and for each element check to see if it is in the hashset. If it is in the set, remove it and continue. Otherwise, add it to the set.

3. Return the set.

- yawgnimmeh May 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class ArrayUtil {
	public static Set<Integer> getOddTimesRepeatedNumbers(int arr[]) {
		Set<Integer> result = new HashSet<>();

		if (Objects.isNull(arr))
			return result;

		for (int data : arr) {
			if (result.contains(data)) {
				result.remove(data);
				continue;
			}
			result.add(data);
		}

		return result;
	}
}

- harikrishna553 May 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Create a HashMap and store Key = element value, Value = Count occurrences of element in array.

2. Traverse array storing element in Map and counting each elements occurrence - O(n)

3. Traverse HashMap and for each Key (element) get it's count. If count % 2 == 1 (Odd) print number.

- guilhebl May 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;

public class CalculateOddFRequency {

static ArrayList<Integer> arrList = new ArrayList<>();

public static void main(String[] args) {
// TODO Auto-generated method stub
Random random = new Random();

int[] array = new int[200];

for (int i = 0; i < 200; i++) {
array[i] = random.nextInt(10);

}
ReturnOddTimesAppearingNumber(array);

if (arrList.size() > 0) {
for (int a : arrList) {
System.out.print(a + " ");
}
}

}

public static void ReturnOddTimesAppearingNumber(int[] array) {

HashMap<Integer, Integer> hm = new HashMap<>();

for (int num : array) {
if (hm.containsKey(num)) {
hm.put(num, hm.get(num) + 1);
}

else {

hm.put(num, 1);

}
}

for (Entry<Integer, Integer> entryset : hm.entrySet()) {
if (entryset.getValue() % 2 != 0) {
arrList.add(entryset.getKey());
}
}

}

}

- Bhanu May 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;

public class CalculateOddFRequency {

static ArrayList<Integer> arrList = new ArrayList<>();

public static void main(String[] args) {
// TODO Auto-generated method stub
Random random = new Random();

int[] array = new int[200];

for (int i = 0; i < 200; i++) {
array[i] = random.nextInt(10);
}
ReturnOddTimesAppearingNumber(array);
if (arrList.size() > 0) {
for (int a : arrList) {
System.out.print(a + " ");}}}

public static void ReturnOddTimesAppearingNumber(int[] array) {

HashMap<Integer, Integer> hm = new HashMap<>();

for (int num : array) {
if (hm.containsKey(num)) {
hm.put(num, hm.get(num) + 1);
}

else {
hm.put(num, 1);}}

for (Entry<Integer, Integer> entryset : hm.entrySet()) {
if (entryset.getValue() % 2 != 0) {
arrList.add(entryset.getKey());
}}}}

- Bhanu May 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
Here's a solution with Time Complexity: O(n) and Space Complexity: O(1) 1. Left shift 1 by each number in the array and maintain XOR result of these numbers. (XOR eliminates all the even occurring numbers as A^A=0) 2. Again left shift 1 by each number and logically AND it with the XOR result. If the same number is returned then that number is occurring odd times. Result for the above example: 1 2 10000 7 4 8 {{{ {code} public static void printOddOccurringNumbers(int[] a) { int xor = 0; for (int i = 0; i < a.length; i++) { int shifted = 1 << a[i]; xor ^= shifted; } for (int i = 0; i < a.length; i++) { int shifted = 1 << a[i]; if ((shifted & xor) == shifted) { System.out.print(a[i] + " "); xor ^= shifted; //remove the odd times occurring number } } } {code} - rj_nix May 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's a solution with Time Complexity: O(n) and Space Complexity: O(1)

1. Left shift 1 by each number in the array and maintain XOR result of these numbers.
(XOR eliminates all the even occurring numbers as A^A=0)
2. Again left shift 1 by each number and logically AND it with the XOR result. If the same number is returned then that number is occurring odd times.

Result for the above example: 1 2 10000 7 4 8

{code}

	public static void printOddOccurringNumbers(int[] a) {
		int xor = 0;

		for (int i = 0; i < a.length; i++) {
			int shifted = 1 << a[i];
			xor ^= shifted;
		}

		for (int i = 0; i < a.length; i++) {
			int shifted = 1 << a[i];
			if ((shifted & xor) == shifted) {
				System.out.print(a[i] + " ");
				xor ^= shifted; //remove the odd times occurring number 
			}
		}
	}

{code}

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

//Amazon Question: You have given an array of integers. The appearance of integers vary (ie some integers appears twice or once or more than once) How would you determine which integers appeared odd number of times ?   
//Using Dict C# (Hashmap alternate of Java)
//Time Complexity: O(N)

namespace Noofoccurenceofint
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[] { 1, 1, 1, 2, 2, 5, 8, 9, 5, 9, 10 };

            List<int> oddocc =  OddTimes(a);

            foreach(int n in oddocc)
            {
                Console.WriteLine(n);


            }
        }


        public static List<int> OddTimes(int[] a)
        {
            Dictionary<int, int> D = new Dictionary<int, int>();
            List<int> oddocc = new List<int>();

            for (int i = 0; i < a.Length; i++)
            {
                if (D.ContainsKey(a[i]))
                    D[a[i]]++;
                else
                    D[a[i]] = 1;

            }

            foreach(KeyValuePair<int,int> kvp in D)
            {
                if (kvp.Value % 2 != 0)
                    oddocc.Add(kvp.Key);
                
            }


            return oddocc;

        }
    }

- dany08 May 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the array - O(n logn)
additional space O(2)
traverse array O(n)

public static void printOddOccurNum(int[] nums) {
		Arrays.sort(nums);
		int prev=nums[0];
		int c=1;
		for (int i = 1; i < nums.length; i++) {
			if(nums[i]==prev) {
				c++;
			}else {
				if(c%2!=0) {
					System.out.println(prev+":"+c);
				}
				prev = nums[i];
				c=1;
			}
		}

}

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

#include <stdio.h>

int main(void) {
int a[]={1,1,1,2,5,1000,5,7,4,8};
int n,i,j,count=0,temp=0;
n=sizeof(a)/sizeof(int);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The numbers which appeared odd number of times and their count is\n{\n");
for(i=0;i<n;i++)
{
count=0;
for(j=0;j<n;j++)
{
if(a[i]==a[j])
{
count++;
}
}
if(count%2!=0)
printf("%d\t %d\n",a[i],count);
i=i+(count-1);
}
printf("}") ;


return 0;
}

output:
The numbers which appeared odd number of times and their count is
{
1 3
2 1
4 1
7 1
8 1
1000 1
}

- Kamalashree May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main(void) {
	int a[]={1,1,1,2,5,1000,5,7,4,8};
	int n,i,j,count=0,temp=0;
	n=sizeof(a)/sizeof(int);
	for(i=0;i<n;i++)
	{
	    for(j=0;j<n-1;j++)
	    {
	        if(a[i]<a[j])
	        {
	            temp=a[i];
	            a[i]=a[j];
	            a[j]=temp;
	        }
	    }
	}
    printf("The numbers which appeared odd number of times  and their count is\n{\n");
	for(i=0;i<n;i++)
	{
	    count=0;
	for(j=0;j<n;j++)
	{
	   if(a[i]==a[j])
	   {
	       count++;
	   }
	}
	if(count%2!=0)
	printf("%d\t %d\n",a[i],count);
	i=i+(count-1);
}
printf("}")	;

	
	return 0;
}

output:
The numbers which appeared odd number of times  and their count is
{
1	 3
2	 1
4	 1
7	 1
8	 1
1000	 1
}

- Kamalashree May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main(void) {
	int a[]={1,1,1,2,5,1000,5,7,4,8};
	int n,i,j,count=0,temp=0;
	n=sizeof(a)/sizeof(int);
	for(i=0;i<n;i++)
	{
	    for(j=0;j<n-1;j++)
	    {
	        if(a[i]<a[j])
	        {
	            temp=a[i];
	            a[i]=a[j];
	            a[j]=temp;
	        }
	    }
	}
    printf("The numbers which appeared odd number of times  and their count is\n{\n");
	for(i=0;i<n;i++)
	{
	    count=0;
	for(j=0;j<n;j++)
	{
	   if(a[i]==a[j])
	   {
	       count++;
	   }
	}
	if(count%2!=0)
	printf("%d\t %d\n",a[i],count);
	i=i+(count-1);
}
printf("}")	;

	
	return 0;
}

output:
The numbers which appeared odd number of times  and their count is
{
1	 3
2	 1
4	 1
7	 1
8	 1
1000	 1
}

- Kamalashree May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main(void) {
	int a[]={1,1,1,2,5,1000,5,7,4,8};
	int n,i,j,count=0,temp=0;
	n=sizeof(a)/sizeof(int);
	for(i=0;i<n;i++)
	{
	    for(j=0;j<n-1;j++)
	    {
	        if(a[i]<a[j])
	        {
	            temp=a[i];
	            a[i]=a[j];
	            a[j]=temp;
	        }
	    }
	}
    printf("The numbers which appeared odd number of times  and their count is\n{\n");
	for(i=0;i<n;i++)
	{
	    count=0;
	for(j=0;j<n;j++)
	{
	   if(a[i]==a[j])
	   {
	       count++;
	   }
	}
	if(count%2!=0)
	printf("%d\t %d\n",a[i],count);
	i=i+(count-1);
}
printf("}")	;

	
	return 0;
}

output:
The numbers which appeared odd number of times  and their count is
{
1	 3
2	 1
4	 1
7	 1
8	 1
1000	 1
}

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

#include <stdio.h>

int main(void) {
	int a[]={1,1,1,2,5,1000,5,7,4,8};
	int n,i,j,count=0,temp=0;
	n=sizeof(a)/sizeof(int);
	for(i=0;i<n;i++)
	{
	    for(j=0;j<n-1;j++)
	    {
	        if(a[i]<a[j])
	        {
	            temp=a[i];
	            a[i]=a[j];
	            a[j]=temp;
	        }
	    }
	}
    printf("The numbers which appeared odd number of times  and their count is\n{\n");
	for(i=0;i<n;i++)
	{
	    count=0;
	for(j=0;j<n;j++)
	{
	   if(a[i]==a[j])
	   {
	       count++;
	   }
	}
	if(count%2!=0)
	printf("%d\t %d\n",a[i],count);
	i=i+(count-1);
}
printf("}")	;

	
	return 0;
}

output:
The numbers which appeared odd number of times  and their count is
{
1	 3
2	 1
4	 1
7	 1
8	 1
1000	 1
}

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

Python code

a = [1,1,1,2,5,10000,5,7,4,8]

d = {}
for i in a:
    if d.get(i):
        d[i] = d[i]+1
    else:
        d[i] = 1

print d

- jayparikh111 May 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void findOddlyRepeatedNumbers(int a[]) {
		if (a == null || a.length == 0) {
			return;
		}

		HashMap<Integer, Integer> map = new HashMap<>(a.length);

		int count = 0;
		for (int i = 0; i < a.length; i++) {
			if (!map.containsKey(a[i])) {
				map.put(a[i], 1);
			} else {
				count = map.get(a[i]);
				map.put(a[i], ++count);
			}
		}

		Iterator<Entry<Integer, Integer>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<Integer, Integer> entry = (Entry<Integer, Integer>) iterator.next();
			int key = entry.getKey();
			int value = entry.getValue();
			if (value % 2 != 0) {
				System.out.println(key + " repeated for " + value + " times");
			}
		}
	}

- coder145 June 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var a = [1,1,1,2,5,10000,5,7,4,8];
function findOdds(arr) {
  var result = [];
  arr.forEach(function(num) {
    if (result.indexOf(num) == -1) {
      result.push(num);
    } else {
      result.splice(result.indexOf(num), 1);
    }
  });
  return result;
}

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

var a = [1,1,1,2,5,10000,5,7,4,8];
function findOdds(arr) {
  var result = [];
  arr.forEach(function(num) {
    if (result.indexOf(num) == -1) {
      result.push(num);
    } else {
      result.splice(result.indexOf(num), 1);
    }
  });
  return result;
}

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

var a = [1,1,1,2,5,10000,5,7,4,8];
function findOdds(arr) {
var result = [];
arr.forEach(function(num) {
if (result.indexOf(num) == -1) {
result.push(num);
} else {
result.splice(result.indexOf(num), 1);
}
});
return result;
}

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

var a = [1,1,1,2,5,10000,5,7,4,8];
function findOdds(arr) {
var result = [];
arr.forEach(function(num) {
if (result.indexOf(num) == -1) {
result.push(num);
} else {
result.splice(result.indexOf(num), 1);
}
});
return result;
}

- Andrew Henderson September 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var a = [1,1,1,2,5,10000,5,7,4,8];
function findOdds(arr) {
  var result = [];
  arr.forEach(function(num) {
    if (result.indexOf(num) == -1) {
      result.push(num);
    } else {
      result.splice(result.indexOf(num), 1);
    }
  });
  return result;
}

- andrew.m.henderson September 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here's a solution with Time Complexity: O(n) Space Complexity: O(1)
Maintain an XOR result of all the numbers.
XOR can be used to eliminate the numbers occurring even number of times.
Use logical AND with each number and this XOR result, if the same number comes back it is occurring odd number of times.
The following code gives the result: 1, 2, 10000, 7, 4, 8

public static void printOddOccurringNumbers(int[] a) {
		int xor = 0;

		for (int i = 0; i < a.length; i++) {
			int shifted = 1 << a[i];
			xor ^= shifted;
		}

		for (int i = 0; i < a.length; i++) {
			int shifted = 1 << a[i];
			if ((shifted & xor) == shifted) {
				System.out.print(a[i] + " ");
				xor ^= shifted; //remove the odd times occurring number 
			}
		}
	}

- rj_nix May 14, 2016 | 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