Amazon Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

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.

A 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.

- Prakash February 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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());	
}
	}

}

- Chandan August 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use HashMap<String,HashSet<Integer>> may be better

- facebook February 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Tester February 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does Intersection of all lists do the work ?

- Jay Parikh February 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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());	
}
	}

}

- chandan kumar August 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question looks strange.
Did you mean that you have an input list and a second prepared list?
So, select all emails that are in the given list which intersects with the prepared list.

- sergey.radov February 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Hardik Soni February 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Python,
I think Intersection operation of all lists will do.

I am not sure. Is it possible ?

- Jay Parikh February 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it asking to consider each list as a Set and then do a intersection of all the sets to get the distinct elements ? Not sure about the language implementation, but i guess java and python do have something as makeset and intersection ?

- Abhijeet February 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a TreeSet (sorted) , add all email ids (from all list) to it, iterate the treeset, check if an email id exists for list of list size.

- panks82 March 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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());	
}
	}

}

- chand268ck August 10, 2015 | 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