## Amazon Interview Question

Quality Assurance Engineers**Country:**India

**Interview Type:**Written Test

```
import java.util.HashMap;
import java.util.Map;
public class SubSetQuestion {
public static void main(String[] args) {
int[] S1 = {1,5,4,6,8,2};
int[] S2 = {5,8,2};
int[] S3 = {5,8,2,7};
Map<Integer, Integer> map = new HashMap();
for(int i=0;i<S1.length;i++)
{
map.put(S1[i], 1);
}
for(int i=0;i<S2.length;i++)
{
if(!map.containsKey(S2[i]))
System.out.println("S2 is not subset");
}
for(int i=0;i<S3.length;i++)
{
if(!map.containsKey(S3[i]))
System.out.println("S3 is not subset");
}
}
}
```

```
public static void findSubset(){
int s1[]={1,5,4,6,8,2};
int s2[]={5,8,2};
//int s2[]={5,8,2,7};
if(s2.length > s1.length || s2.length == 0)
System.out.println(s2+" is not a subset of "+s1);
int k=0;
boolean isSubset=false;
for(int i=0;i<s1.length;i++){
if(s1[i] == s2[k]){
if(k+1 == s2.length){
System.out.println(s2+" is a subset of "+s1);
isSubset=true;
}
k++;
}
}
if(!isSubset)
System.out.println(s2+" is not a subset of "+s1);
}
```

```
public static void findSubset(){
int s1[]={1,5,4,6,8,2};
int s2[]={5,8,2};
//int s2[]={5,8,2,7};
if(s2.length > s1.length || s2.length == 0)
System.out.println(s2+" is not a subset of "+s1);
int k=0;
boolean isSubset=false;
for(int i=0;i<s1.length;i++){
if(s1[i] == s2[k]){
if(k+1 == s2.length){
System.out.println(s2+" is a subset of "+s1);
isSubset=true;
}
k++;
}
}
if(!isSubset)
System.out.println(s2+" is not a subset of "+s1);
}
```

```
public static void findSubset(){
int s1[]={1,5,4,6,8,2};
int s2[]={5,8,2};
//int s2[]={5,8,2,7};
if(s2.length > s1.length || s2.length == 0)
System.out.println(s2+" is not a subset of "+s1);
int k=0;
boolean isSubset=false;
for(int i=0;i<s1.length;i++){
if(s1[i] == s2[k]){
if(k+1 == s2.length){
System.out.println(s2+" is a subset of "+s1);
isSubset=true;
}
k++;
}
}
if(!isSubset)
System.out.println(s2+" is not a subset of "+s1);
```

```
public static void findSubset(){
int s1[]={1,5,4,6,8,2};
int s2[]={5,8,2};
//int s2[]={5,8,2,7};
if(s2.length > s1.length || s2.length == 0)
System.out.println(s2+" is not a subset of "+s1);
int k=0;
boolean isSubset=false;
for(int i=0;i<s1.length;i++){
if(s1[i] == s2[k]){
if(k+1 == s2.length){
System.out.println(s2+" is a subset of "+s1);
isSubset=true;
}
k++;
}
}
if(!isSubset)
System.out.println(s2+" is not a subset of "+s1);
}
```

Make hash table of S1. Iterate S1 if all elements are present and iterate S3 if any one element is missing by checking with hash table of S1.

- Anonymous May 10, 2019