Amazon Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Phone Interview




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

Test case:
1) by value, to test the correction of the function
1. normal cases: like some integers in some fixed range
2. very large cases: give some integers which are out of integer range
3. blank cases: give {}
2) by element quantity, to test the speed of the function
1. small set
2. large set
3) give some special test cases, want have the results expected or speed demand

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

public Integer[] unique(Integer[] input) {
        Integer[] output = new Integer[0];
        HashSet<Integer> a = new HashSet<Integer>();
        HashSet<Integer> b = new HashSet<Integer>();
        for (Integer i : input) {
            if (!a.add(i)) {
                b.add(i);
            }
        }
        return b.toArray(output);
    }

- svgbdha April 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why you are using variable 'a'? what is the purpose?

- Anonymus June 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use Sorting using any nlogn method like heap sort
and then compare if u find more then 1 element , put it in another array...
void findunique(int arr[],int size)
{
int j=0,arr2[size];
heapsort(arr,size);
	for(int i =0;i<size;i++)
	{
		if(i<size-1 && arr[i]==arr[i+1])
		{
			while(i<size-1 && arr[i]==arr[i+1])
			i++;
		arr2[j++]=arr[i];
		}
	}
}

- swati4agrawal April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assuming they don't want the answer array in the same order as the original array, sorting first works fine.

- curious_george April 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why sort at O(nlgn) when u can loop once at O(n) and maintain a hash table where u can check for the element at O(1)

- Sandy April 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anything mentioned about the desired time and space complexities?

- Nascent April 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope.

- JSDUDE April 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReturnDuplicate {
	public static void main(String[] args) {
		int array[] = new int[]{2,1,2,4,3,1,5,1};
		int[] duplicates = getDuplicates(array);
		for(int i=0; i<duplicates.length;i++)
			System.out.println(duplicates[i]);
	}

	private static int[] getDuplicates(int[] array) {
		HashSet<Integer> uniqueSet = new HashSet<Integer>();
		ArrayList<Integer> duplicates = new ArrayList<Integer>();
		
		for(int i=0 ; i< array.length; i++){
			Integer item = array[i];
			if(uniqueSet.remove(item))
				duplicates.add(item);
			else
				uniqueSet.add(item);
		}
		
		int[] dupArray = new int[duplicates.size()];
		for(int i=0; i< duplicates.size(); i++)
			dupArray[i] = duplicates.get(i);
		
		return dupArray;
	}
}

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

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

class NumUtils {
	public Integer[] nonSingular(Integer[] numbers) {
		Set<Integer> allNums = new HashSet<>();
		Set<Integer> nonSingularNums = new HashSet<>();
		for (Integer number : numbers) {
			if (!allNums.add(number)) {
				nonSingularNums.add(number);
			}
		}
		return (Integer[])nonSingularNums.toArray();
	}

}

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

Create a BST with node of following type. store the frequency of number in the count.

struct Node {
    int data;
    int count;
    struct Node *left;
    struct Node *left;
};

Create a new array by traversing the BST, containing only those values whose count is > 1

- Sangeet Kumar April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

you could even make a bst of unique keys and if a duplicate is found append to the array
that way you could save looping through bst after creation
so you would get running time of order O(nlgn)

or making use of hashing will make it even better O(n)

since we need to go through all the elems in the array we cant do better than O(n) hence using hash tables would be best

- Sandy April 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[]){
int[] sat = {2,1,2,4,3,1,5,1};
// int[] sat = {1,1,1,1,1,1,1,1,1,1};
HashSet sat1 = new HashSet();
for(int i=0;i<sat.length;i++){
sat1.add(sat[i]);
}
Object[] sat2 = sat1.toArray();
int len1 = sat.length - sat2.length;
for(int j=0;j<sat.length-len1;j++){
System.out.print(sat2[j] + " ");
}
System.out.println();
for(int i=0;i<sat2.length;i++){
int count = 0;
for(int j=0;j<sat.length;j++){
if(sat2[i].equals(sat[j])){
count++;
}
}
if(count>=2){
System.out.print(sat2[i] + " ");
}
}
}

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

If array size is N and all elements are in the range 1 to N, then we can design an algorithm with below complexities :
Time complexity - O(N)
Space complexity - O(1)

Are the elements within the range 1 to N?

- Sudhindra.A April 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def get_arr(arr)
arr1_1 = []
for i in 0..arr.length-1
for j in i+1..arr.length
if arr[i] == arr[j]
unless arr1_1.include? arr[i]
arr1_1 << arr[i]
end
end
end
end
return arr1_1
end
arr3 = get_arr(arr1)
arr4 = get_arr(arr2)
arr = []
arr = arr.concat(arr3).concat(arr4)
p arr

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

def get_arr(arr)
  arr1_1 = []
  for i in 0..arr.length-1
    for j in i+1..arr.length
      if arr[i] == arr[j]
        unless arr1_1.include? arr[i]
          arr1_1 << arr[i]
        end
      end
    end
  end
  return arr1_1
end
arr3 = get_arr(arr1)
arr4 = get_arr(arr2)
arr = []
arr = arr.concat(arr3).concat(arr4)
p arr

This code is in ruby

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

Working C Code, messy though

/* Program to print instances of elements which have duplicates in a given 
array */

#include <stdio.h>
#include <stdlib.h>

int* findDupEle(int arr[], int size);

int main(void)
{
	int arr[]={2,1,2,4,3,1,5,1};
	int size=sizeof(arr)/sizeof(arr[0]);
	
	int *dup=findDupEle(arr, size);//Pointer to the array containing 
								   //duplicates is returned
	
	int i=1;
	int k=*dup;
	
	//Printing the elements which have repeats 
	printf("\nThe Array of Duplicates contains : "); 
	while(i<=k)
	{
		printf("%d\t",*(dup+i));
		i++;
	}
	printf("\n");
	
	return 0;
}

//Function that returns the array containing instances of 
//elements with duplicates
int* findDupEle(int arr[], int size)
{	
	if(size==0 || size==1)
	{
		printf("\nErroneous Input!\n");
		abort();
	}
		
	int hasha[101]={0};//Sort of hash which accepts values from 1 to 100
	
	int count=0;
	
	int* dup=(int*)malloc(sizeof(int)*size+1);//Dynamically declare and allocate 
											  //memory to the array which is 
											  //going to store duplicates
	
	int i=0,k=0;
	
	
	while(i<size)
	{
	
			if(hasha[arr[i]]==0)//If the value is being seen for 
				hasha[arr[i]]=1;//the first time this is executed
				
	
			else if(hasha[arr[i]]==1)
			{
				dup=dup+1;//dup pointer is incremented
				*dup=arr[i];
				k++;
				hasha[arr[i]]++;//hash is also incremented and hence 
								//elements which are seen for the 2nd 
								//time only are put into the dup array
			
			}	
			i++;
	}
	
	dup=dup-k;
	*dup=k;
	
	return dup;
}

- mahesh_ostwal@yahoo.in April 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algorithm:
foreach element in the array
look if element exist in hashmap
if it doesn't exisit
add to hasmap with count as 1
else
if value of that element from hasmap returns 1
update hashmap value to -1 // -1 values will be skipped from adding it to the
//array
increment counter
output array[counter] = array element // ad to the output array

int[] findUniqueDup(int[ ] input] {
// check for null
//check if array.length = 1; 
// In the above cases return -1;
Hashmap hm = new Hashmap();
int value,j -1;
int[] output;
foreach ( int i in input){
     Object o = hm.get(i);
      if( o == null) // i is not in hashmap so add it in hashmap
           hm.put(i,1);
      else{
               val = hm.get(i);
               if(value ! = -1)
                     hm.put(i,-1);
                     j++;
                     output[j] = i;
       }
  }
return output;
}

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

import java.util.HashSet;

public class Sample3 {

public static void main(String[] args) {

Integer[] numbers = { 1, 2, 1, 1, 2, 4, 5, 6, 6};
Integer[] result = new Integer[numbers.length];

HashSet<Integer> hs = new HashSet<Integer>();
HashSet<Integer> dups = new HashSet<Integer>();

for (int i = 0; i < numbers.length; i++) {

if (!hs.add(numbers[i])) {

dups.add(numbers[i]);
}
}

System.out.println(dups);
}

}

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

List<int> st = new List<int>() { 2, 1, 2, 4, 3, 1, 5, 1 };
var r = st.GroupBy(c => c).Where(x => x.Count() > 1);

- sparta.prague July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What language is this?
Do you happen to know what's the time complexity of the 2nd line?

- JSDUDE July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] arr1 = {2,1,2,4,3,1,5,1,5,4};
int[] arr2 = new int[6];
int data = 0;
for(int i : arr1){
arr2[i] = arr2[i] +1;
if(arr2[i] == 2){
++data;
}
}
int[] arr3 = new int[data]; //finalArray
data = 0;
for(int i = 0; i< arr2.length; i++){
if(arr2[i] >= 2 ){
arr3[data] = i;
data++;
}
}

- nikbat1@gmail.com October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DuplicateInArray {

public static void main(String args[]) {
int[] arr = {2,1,2,4,3,1,5,1};
int[] res = findDuplicate(arr);
System.out.print("[");
for (int i = 0; i < res.length; i++) {
if (i != res.length - 1) {
System.out.print(res[i] + ",");
} else {
System.out.print(res[i]);
}
}
System.out.println("]");
}

public static int[] findDuplicate(int[] arr) {

int[] result;
int size = 0;

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

for (int i = 0; i < arr.length; i++) {
if (valueToCount.containsKey(arr[i])) {
valueToCount.put(arr[i], valueToCount.get(arr[i]) + 1);
} else {
valueToCount.put(arr[i], 1);
}
}
size = valueToCount.entrySet().stream().filter((pair) ->
((int)pair.getValue()!=1)).map((_item) -> 1).reduce(size, Integer::sum);
result = new int[size];
int index = 0;

for (Map.Entry pair : valueToCount.entrySet()) {
if ((int)pair.getValue()!=1) {
result[index] = (int) pair.getKey();
index++;
}
}
return result;
}
}

- Anonymous October 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DuplicateInArray {

public static void main(String args[]) {
int[] arr = {2,1,2,4,3,1,5,1};
int[] res = findDuplicate(arr);
System.out.print("[");
for (int i = 0; i < res.length; i++) {
if (i != res.length - 1) {
System.out.print(res[i] + ",");
} else {
System.out.print(res[i]);
}
}
System.out.println("]");
}

public static int[] findDuplicate(int[] arr) {

int[] result;
int size = 0;

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

for (int i = 0; i < arr.length; i++) {
if (valueToCount.containsKey(arr[i])) {
valueToCount.put(arr[i], valueToCount.get(arr[i]) + 1);
} else {
valueToCount.put(arr[i], 1);
}
}
size = valueToCount.entrySet().stream().filter((pair) ->
((int)pair.getValue()!=1)).map((_item) -> 1).reduce(size, Integer::sum);
result = new int[size];
int index = 0;

for (Map.Entry pair : valueToCount.entrySet()) {
if ((int)pair.getValue()!=1) {
result[index] = (int) pair.getKey();
index++;
}
}
return result;
}
}

- farukmorali October 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It seems that you'd better give 20 or more test cases, that will give a good impression.

- Gabriel April 16, 2013 | 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