Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

import java.util.LinkedHashSet;
import java.util.Set;


public class IterativeSubset {

	public static void main(String[] args) {
		
		int array[]=new int[]{5,6,3,1,4,7,8,9,2,11,12,2,4,6,9,4,1};
		
		int arrayLngth=array.length;
		int i=0;
		Set<Integer> sets=new LinkedHashSet<Integer>();
		
		for(int j=0;j<arrayLngth;j++)
		{
			i=j;
			while(!sets.contains(array[i]))
			{
				i=array[i];
				sets.add(i);
			}
			System.out.println(sets.toString());
			sets.clear();
		}

	}

}

- Ankit garg January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

instead of using hash you can directly use array.It will save you lot of space.
for eg. as the number ranges from 0 to n-1
you can write this as
for(int j=0;j<arrayLngth;j++)
{
i=j;
len=0;
init(array) // this will make all entries again positive
while(array[i]>0)
{
i=array[i];
array[i]=array[i]*-1;
len++
}
System.out.println(len)

}

- blackfever January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you provide an example?

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

public static Set<Integer>[] createSetOfElements(int a[]) {
		Set<Integer>[] allSets = new Set[a.length];
		int largestSetSize = 0;
		for (int i = 0; i < a.length; i++) {
			allSets[i] = new HashSet<Integer>();
			int element = a[i];
			while (!allSets[i].contains(element)) {
				allSets[i].add(element);
				if (element < a.length) {
					element = a[element];
				}
			}
			if (allSets[i].size() > largestSetSize) {
				largestSetSize = allSets[i].size();

			}
		}
		System.out.println("Size of Largest set is " + largestSetSize);
		System.out.println("Printing elements in all sets");
		int i = 1;
		for (Set<Integer> set : allSets) {
			System.out.print("Set# " + i++ + " =>  ");
			for (Integer integer : set) {
				System.out.print(integer + " ");
			}
			System.out.println();
		}
		return allSets;
	}

	public static void main(String[] args) {
		int a[] = { 1, 2, 3, 4, 4, 5 };
		createSetOfElements(a);
	}

Sample Input: { 1, 2, 3, 4, 4, 5 }
Output:
Printing elements in all sets
Set# 1 => 1 2 3 4
Set# 2 => 2 3 4
Set# 3 => 3 4
Set# 4 => 4
Set# 5 => 4
Set# 6 => 5

- Ganni January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Set is an interface. How did you create a new instance on this line -

Set<Integer>[] allSets = new Set[a.length];

- buckeyekarun January 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Set<Integer>[] createSetOfElements(int a[]) {
		Set<Integer>[] allSets = new Set[a.length];
		int largestSetSize = 0;
		for (int i = 0; i < a.length; i++) {
			allSets[i] = new HashSet<Integer>();
			int element = a[i];
			while (!allSets[i].contains(element)) {
				allSets[i].add(element);
				if (element < a.length) {
					element = a[element];
				}
			}
			if (allSets[i].size() > largestSetSize) {
				largestSetSize = allSets[i].size();

			}
		}
		System.out.println("Size of Largest set is " + largestSetSize);
		System.out.println("Printing elements in all sets");
		int i = 1;
		for (Set<Integer> set : allSets) {
			System.out.print("Set# " + i++ + " =>  ");
			for (Integer integer : set) {
				System.out.print(integer + " ");
			}
			System.out.println();
		}
		return allSets;
	}

	public static void main(String[] args) {
		int a[] = { 1, 2, 3, 4, 4, 5 };
		createSetOfElements(a);
	}

Sample Input: a[] = { 1, 2, 3, 4, 4, 5 }
Output:
Size of Largest set is 4
Printing elements in all sets
Set# 1 => 1 2 3 4
Set# 2 => 2 3 4
Set# 3 => 3 4
Set# 4 => 4
Set# 5 => 4
Set# 6 => 5

- ganni January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this works only for sorted array......

- mqamri January 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class maxSet {
public static void main(String[] args) {
int a[] = {5,6,3,1,4,7,8,9,2,11,12,2,4,6,9,4,1};
int b[][] = new int[a.length][a.length];
for(int i=0;i<a.length;i++){
int j=i,n=0;
do{
boolean flag = false;
for(int k=0;k<a.length;k++){
if(b[i][k] == a[j])
flag = true;
}
if(!flag)
b[i][n]=a[j];
j=a[j];
n++;
}while(j<a.length && n<a.length);
}

System.out.println("\n\nSets generated: ");
int count[] = new int[a.length];
int max=0;
for(int i = 0; i<a.length;i++){
for(int j=0 ; j<a.length;j++)
if(b[i][j] != 0){
System.out.print(b[i][j]+ " ");
count[i]++;
}
if(count[i]>max)
max = count[i];
System.out.println();
}

System.out.println("\nSize of largest Set is :"+max);
}
}

Input:
5 6 3 1 4 7 8 9 2 11 12 2 4 6 9 4 1

Sets generated:
5 7 9 11 2 3 1 6 8
6 8 2 3 1
3 1 6 8 2
1 6 8 2 3
4
7 9 11 2 3 1 6 8
8 2 3 1 6
9 11 2 3 1 6 8
2 3 1 6 8
11 2 3 1 6 8
12 4
2 3 1 6 8
4
6 8 2 3 1
9 11 2 3 1 6 8
4
1 6 8 2 3

Size of largest Set is :9

- vasanth January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does the interviewer wants us to modify the given array so that we can find the longest such sequence? Or just print sequences (sets) to the given array?

- IntwPrep.MS January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

exactly my thoughts. for gettign longest sequence, each element has to be unique, hence its an increasing order of integers from 0 through n-1. but finding the set of permutations of those n integers to yield maximum sets is interesting.

- jay swaminathan January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

an interesting variation of the question is, what is the maximum possible size of any set S[i]. the answer is n. all n elements have to be unique from 0 to n-1. another followup - if the n elements can be shuffled around ,create a set of sets of all possible maximum sets .

for example for number 0 1 2 3,
{1,2,3,0},{1,3,0,2} are 2 orderings for S[0] to be of maximum size.

- jay swaminathan January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

0,1,2,2,3,4

0,1,2
2,3,4
so no the answer is not n, the problem is pretty interesting in itself forget variation
0,1,6,5,2,4,3,7
the answer to above one is 7 not 8

- kkr.ashish January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		int[] input = new int[] { 1, 2, 3, 4, 4, 5};
		int[] setSize = new int[input.length];

		int max = -1;
		for (int index = 0; index < input.length; index++) {
			Set<Integer> set = new HashSet<>();
			int curr = input[index];
			boolean alreadySet = false;
			while (!set.contains(curr)) {
				set.add(curr);
				curr = input[curr];
				if (setSize[curr] > 0) {
					// -1 because a[curr] will already be there
					setSize[index] = set.size() + setSize[curr] - 1;
					alreadySet = true;
				}
			}
			if (!alreadySet) {
				setSize[index] = set.size();
			}

			if (max < setSize[index]) {
				max = setSize[index];
			}

			System.out.println("for index " + index + " set size " + setSize[index]);
		}

		System.out.println(max);
	}

- LK January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

forgot to add break

updated code

public static void main(String[] args) {
		int[] input = new int[] { 1, 2, 3, 4, 4, 5};
		int[] setSize = new int[input.length];

		int max = -1;
		for (int index = 0; index < input.length; index++) {
			Set<Integer> set = new HashSet<>();
			int curr = input[index];
			boolean alreadySet = false;
			while (!set.contains(curr)) {
				set.add(curr);
				curr = input[curr];
				if (setSize[curr] > 0) {
					// -1 because a[curr] will already be there
					setSize[index] = set.size() + setSize[curr] - 1;
					alreadySet = true;
					break;
				}
			}
			if (!alreadySet) {
				setSize[index] = set.size();
			}

			if (max < setSize[index]) {
				max = setSize[index];
			}

			System.out.println("for index " + index + " set size " + setSize[index]);
		}

		System.out.println(max);
	}

- LK January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[]){
			
			int arr[]=new int[]{5,6,3,1,4,7,8,9,2,11,12,2,4,6,9,4,1};
			int len = arr.length;
			int maxcount = 0;
			int count = 0;
			Set<Integer> finalSet=null;
			
			for(int i=0;i<len;i++){
				Set<Integer> set = new LinkedHashSet<Integer>();
				int num = arr[i];
				count = 0;
				while(!set.contains(num)){
					set.add(num);
					num = arr[num];
					count++;
				}
				
				if(count>maxcount){
					maxcount = count;
					finalSet = set;
				}
			}
		System.out.println(finalSet);	
		System.out.println("Maxsize is :: "+maxcount);
			
		}

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

public static void main(String args[]){
			
			int arr[]=new int[]{5,6,3,1,4,7,8,9,2,11,12,2,4,6,9,4,1};
			int len = arr.length;
			int maxcount = 0;
			int count = 0;
			Set<Integer> finalSet=null;
			
			for(int i=0;i<len;i++){
				Set<Integer> set = new LinkedHashSet<Integer>();
				int num = arr[i];
				count = 0;
				while(!set.contains(num)){
					set.add(num);
					num = arr[num];
					count++;
				}
				
				if(count>maxcount){
					maxcount = count;
					finalSet = set;
				}
			}
		System.out.println(finalSet);	
		System.out.println("Maxsize is :: "+maxcount);
			
		}

- Anonymous January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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