Amazon Interview Question for Software Engineer / Developers


Country: India




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

Breadth first search on a graph?

- Rayden November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Raydena:
Thanks for your comment.
But the graph of facebook contains trillions of users...it would be not efficient to run BFS on whole graph.
Also, how a simple BFS can assure that no other shorter path exist after it ends.
See, every node has its connection and one of which is shortest one(either from person A/Afriends end or Person B/Bfriends end). Also, u have to prevent to go yourself in loops

- shwetank2003819 November 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did not say to execute on the whole facebook universe graph. Just start from one person than construct your graph.

Why do you think Breadth first search won't find the shortest path? I am not talking about depth first search. This is a weightless non-directional graph.

Keeping track of visited nodes will prevent loops.

Of course after 2nd or 3rd level things will get out of control assuming everyone has avg of 200 friends.

- Rayden November 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

How about implementing Least Common Ancestor (LCA) ?

- Amit December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LCA in an N-ary tree will solve it. Nice thought Amit !!

- Sachin Jain January 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now if only Facebook was anything remotely resembling a tree structure.

- eugene.yarovoi March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

This is so simple, that you shouldn't even need to ask the question.

No need for any ridiculous algorithms, or complicated ideas.

If you want to find the path between A and B, then start by getting the friends of A and B.

Check if any of those friends are the same.

Then get the friends of the friends. Some will have the same friends, so always check for names already in the list, and skip them.

Then check if any of the friends of the friends of A, are the same as the friends of the friends of B.

Repeat this until you get a common friend.

This WILL be the shortest path.

- Pete May 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's pointless to be discussing specific named algorithms. This is so simple and straight forward, and there is no faster or better way than the intuitive way I just mentioned.

And it's very important to start from both A and B at the same time. Just getting friends of friends of.... from A until you find B, takes many factors more friend checking.

Even with the way I described, the number of people to check quickly grows to millions, but there is no faster way.

You could try to be smart, and first only check people in a specific area/country, based on where the people are located.

- Peter May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And remember the premise.

"You are provided with an API which returns the list of friends of a particular persons"

So it's irrelevant how data is structures inside facebook.

This API is all we have to work with.

- Peter May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Considering each node as a person, and distance between person and friend as 1, we can apply Iterative deepening Depth First Search.

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

There are various similarity measures like cosine or co-relation which can be used ... even a modified apriori algo can be used ... but this can be answered only by ppl with knowledge in data mining ... I cant think of any way this can be solved other than IDA ie, iterative deepening search.

What abt IDA* ?

- gowtham.n.mail November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very bad idea, to just keep going until you find the other person.

It will be MUCH more efficient to work your way out from both people, until you find a common friend.

That reduces the number of friends you have to check, by many many factors.

- Pete May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes it's a basic question, take a hashmap, keep value as distance of the friend from the starting friend and key as the friend id, and keep on iterating over the hashmap and adding the current person's friends in the hashmap, until you find the required friend.

- ishant agarwal November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ishan , pls explain in detail , m not geting it ?

- Anni November 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are you kidding me?
Suppose we are finding a connection bw A and B

You will first add all the friends of A in the Map (say they are 200)
Entries in Map = 200

Now you will process the first entry, which might also have 200 friends
Size after processing first entry = 400

Similarly after processing second entry 400 + 200 = 600
After processing 200th entry
200*200 = 40000

Till, then you will process, 201th to 40000th entry

I hope I have made my point.

- Sushant November 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sushant: Yes, it requires a lot of storage and time. But how else would you do it? Unless there's a way you can cache results (discuss with interviewer), there's not really a way to know the shortest path between two nodes without looking at all the paths of at least that length.

- eugene.yarovoi November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sushant: Hmmm...actually, I take that back. There are better ways of doing this, though not in a way that takes less than exponential time in the distance of the connection.

How about this: there is a path that is of length at most n between two vertices of a graph iff the intersection of the set of all nodes within distance n/2 of the first endpoint and the set of all nodes within distance n/2 of the second endpoint is nonempty. We can thus get one of these sets as a hashset and then do a set lookup on each of the elements in the other set. This would give us d^(n/2) time, where n is the distance and d is the expansion factor (200 in your example). Better than d^n time!

- eugene.yarovoi November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So something small like a friend of a friend or a friend of a friend of a friend of a friend can be verified quite reasonably with my algo.

- eugene.yarovoi November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hmm... is that fact about all graphs, can you please point me to its proof.

And yes, in my opinion the only way to go about it is by using some sort of parallel processing or clustering technique such as the Map Reduce framework.

- Sushant December 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sushant: Suppose there is a vertex "v" that is distance n/2 from both endpoints, e1 and e2. So, there is a path from e1 to e2, namely, e1 -> v -> e2 that has length bounded by n/2 + n/2 = n.

- eugene.yarovoi December 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very bad idea, to just keep going until you find the other person.

It will be MUCH more efficient to work your way out from both people at the same time, until you find a common friend.

That reduces the number of friends you have to check, by many many factors.

- Peter May 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

...and working from two people at the same time is precisely what I proposed in the comments here.

- eugene.yarovoi May 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't see how hashmap help here? bfs should work it out...

- Anonymous November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hashmap is a good solution to the problem. BFS can be very expensive if we are dealing here with millions of nodes. Also keeping track can also be impractical if multiple users are searching.

- Fahad November 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anon

I am also using BFS, but just using hashmap so that you don't add to the list the already processed friend or person.

- ishant agarwal November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why dont we use dijkstra algo..in this case?

- Aravind November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the weight on the edges?

- Rayden November 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dijkstra algo must work. weight of my friends always 1 from me. so we have weights which are always positive

- Rids November 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We could use Dijkstra's, but simpler algorithms will suffice. There's also a more efficient algo based on doing a BFS from both endpoints and looking at the set intersection of the nodes that result.

- eugene.yarovoi December 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

I would try something different then already mentioned in the answers.

Start from both the person.First find direct friend of both the persons( depth 1) and find a intersection, it not there then find all there friends of friends for each person and then find a intersection and continue in that way.

- gaurav.in November 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

This can be solved using Dijkstra's algorithm.
Explanation: Assume Facebook has a small set of people- A,B,C,D,E,F,G.
A is friends with B,C,E
B is friends with C,D (and A)
D is friends with E & G (and B)
Now construct a connected graph.
Suppose we now want to find shortest hop between A & G, we can use Dijkstra's algorithm. Cant we then extrapolate this to real facebook situation?

- Ashish Agarwal November 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Now construct a connected graph." You want to construct the Whole Facebook connection Graph? I do not think this is practical.

- alberto.cid November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Assume Facebook has a small set of people" - wrong assumption.

- amustea November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why is this a wrong assumption? and why can't someone create a connected graph for all facebook connections ? the problem does not ask you to architect the complete solution which may involve things from neural networks & AI & much more.. i will be happy to hear the arguments on why we cant extrapolate?

- toashishagarwal November 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question explicitly mentions Facebook, And it has More than 800 million active users. This algorithm requires 2 traversals. 1 to construct the graph (and duplicate the Facebook data), and the second pass to find the connections. This is definitely a solution that works, but why waste so many CPU cycles if you can do it in 1 traversal only.
The way a see it, the connected graph is already on the database (Facebook) and you are provided with an API to solve the problem, so duplicating the Database seems to me as a waste of resources.

- alberto.cid December 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem can take huge memory and processor time if BFS is used.

My Initial assumption is to create groups according to some criteria(based on 'related' status).
Then find a path between groups of the person A and person B.
Then establish a path between the two groups, then between the two persons.

Now we have a path of some length between A and B.
Now we may try BFS between them , just to reduce the length between A and B.

I think its confusing, but my point is to establish a path between A and B and then reduce it to the minimum possible length.

- SantiagoYMG December 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So, BFS right... solves the matter.

BFS is a linear time (O(V + E)) algo that effectively computes shortest distance of any node, from a given node of interest, in a weightless graph.

Facebook might be a monster network, but connections or paths from a user or node fade away after a certain distance, so one cannot conclude BFS is ineffective.

Infact algo's like Dijkstra's, etc will be less effective compared to BFS in *this* case.

- kartikaditya December 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What do you mean they fade away? and on an average after what distance approximately? (in your opinion, I think its an interesting take on the problem)

Also, HashMap is much better in terms of space complexity compared to using a Queue and a set of visited nodes.

- Sushant December 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about implementing Least Common Ancestor (LCA) ?

- Amit December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It all depends on how the facebook organizes its users. If you look at one question in career book, it explains different approaches like Facebook maintaining different servers placing users from one country in one server, etc.
Never the less the facebook has to use the graph datastructure to connect different people. In my view the interviewer is interested in different possible ways where we can easily deduce the shortest distance like based on the groups both share we can go through the common friends in those groups or something like this.

Although i am not giving any solution but this is my approach towards the problem. Basically we have either DFS or BFS to travel graph so we should use either of them

- swathi December 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-Create a hash table for visited node
-Start two thread and one start from A and other one from B, both share above visited hash table
- Perform dfs from A(using first thread) and B(using second thread) and put new node in visited table; -There wont be any cycle
If we find that a node already exists in visited table then we found the min distance...

We can have some upper limit after which we can return none

- shsf July 08, 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