Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Find common elements in their adjacency list

- sonesh February 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
5
of 5 vote

find the nodes which are directly connected to both the nodes.
The common vertex or node will be the mutual friends

- DashDash February 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Simple set intersection between the two people's friend lists will work here.

A while back I answered a similar but more general question: given two people in a social network, what is the shortest path of friend connections from one to the other? Discussed here: careercup DOT com/question?id=14087784

- eugene.yarovoi February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, its just intersection which takes O(n) where n is the number of friends each one has.
All nodes are white colour.
Mark each friend of A as black and while visiting the friends of B, if any one the friends is black, then he/she is a mutual friend of both. ---> O(n) where n is avg. number of friends one has.

- bharat February 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Put the first user's list of friends in a hash table (with their user-id as key).
Iterate on the second user's list of friends, and lookup each of them in the hash table.
Any match is a common friend.
Time is O(M+N) where M and N are the number of friends of each user respecitively.
Space is O(min(M,N)) - the space for the hash table if we choose the user with less friends as the first user.

- gen-y-s February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why to use space.. as it can be done in O(m+n) without any space... already this solution is suggested above.

- bharat February 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Class Graph
{
int data;
Graph List<Graph> Vertices;
}


List<Graph> CommonFriends(Graph P1, Graph P2)
{
List<Graph> friends = new List<Graph>();

if(P1==null || P2==null)
return friends;

foreach(Graph g in P1.Vertices)
{
if(P2.Vertices.Contains(g))
friends.Add(g);
}
return friends;
}

- GM February 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Graphs have adjacency matrix to define connections
If there are 3 nodes
Let m=1 and the code will find with whom does it share common links
for(i=0;i<3;i++)
{
if(a[m][i]==1)
{
for(j=0;j<3;j++)
{
if(a[i][j]==1)
{
if(j!=m)
return j;
}
}
}
}

- Swet February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a BFS to get a path to second friend. Depending on whether you want a common friend who is directly connected to source and target friend, check the path length ( number of steps from Friend 1 to Friend 2). Any friend lying on the path would be a common friend if path length of more than 2 steps is allowed.

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

Use BFS ..

- Vaibhav March 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Innfriend is online platform to search your old school/college/company friends. Innfriend makes your search easy. Once you find your, innfriend helps you to get connected with your friend, you would keep getting update for any changes i.e. contact number, company, city, social networking profile etc. from your friends.

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

Make a directed Graph. where every person is a node. if person A is friend with B. Then graph will be--
A----->B
Then Find " Strongly Connected Component".

- shamimice03 September 11, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

do either dfs or bfs to search the node which is common to given node.

- go4gold February 23, 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