Amazon Interview Question
InternsCountry: United States
Interview Type: Phone Interview
package javacollection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class EmailSearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer,String> hm=new HashMap<>();
hm.put(1,"a@gmail.com");
hm.put(2, "b@gmail.com");
hm.put(3,"c@gmail.com");
hm.put(4,"d@gmail.com");
Set s=hm.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Can you please explain, why you need Hashset as value?
I think we know, how many emails are there [even if its not given we can keep a counter]. We can add all email address present in first mail into HashMap<String,Integer> and set all values to 1.
Then from second mail onwards we will first check if that mail address is present or not. If its present then increment the value by one. else we wont add that into hashmap. Then at end we can just iterate the hashmap
and whose value is equal to count we display only those.
package javacollection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class EmailSearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer,String> hm=new HashMap<>();
hm.put(1,"a@gmail.com");
hm.put(2, "b@gmail.com");
hm.put(3,"c@gmail.com");
hm.put(4,"d@gmail.com");
Set s=hm.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
static List<String> findCommonEmailAddresses(List<List<String>> emailList) {
List<String> smallestList = null;
for(List<String> list : emailList) {
if (smallestList == null) {
smallestList = list;
} else if(smallestList.size() > list.size()) {
smallestList = list;
}
}
List<String> result = new ArrayList<String>();
for (String emailAddress : smallestList) {
result.add(emailAddress);
for (List<String> list : emailList) {
if (!list.contains(emailAddress)) {
result.remove(emailAddress);
break;
}
}
}
return result;
}
This may be one possible solution
package javacollection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class EmailSearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Integer,String> hm=new HashMap<>();
hm.put(1,"a@gmail.com");
hm.put(2, "b@gmail.com");
hm.put(3,"c@gmail.com");
hm.put(4,"d@gmail.com");
Set s=hm.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
The question is not clear enough but I think you mean to say that we have a number of contact lists. Each contact list has a number of email IDs and you wish to find the email IDs that are common in all contact lists.
- Prakash February 07, 2015A HashMap<String, List<Integer>> may be used where key could be an email ID and the value could be a list of all contact lists that it appeared in.
We may iterate over all contact lists and add/update a map from email ID to list of all contact list IDs. Later, the email IDs that are mapped to a list of size equal to the number of contact lists are common among all contact lists.