Facebook Interview Question for Software Engineer / Developers






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

Simple.
Find minimal spanning tree for the graph. Then check the expected time. This should do the trick.

- Lord Darth Plagueis June 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL. Simple huh?

Minimum spanning tree is obviously wrong. (Try thinking about what you write, once in a while).

Consider the following input:

f_d 0
toilet 1.0
bedroom 0.0
kitchen 0.0


f_d toilet 2
f_d kitchen 2
f_d bedroom 1
bedroom toilet 1.5
kitchen toilet 1

If we take minimum spanning tree, expected time is 2.5, but the optimal is 2.


Also to original poster,

there could be more than n! possibilities. What prevents multiple trips to the front door? You can visit the same location twice, if that gives easy access to other locations, no?

Interesting problem, seems like dynamic programming might work here, but have to think about it...

- LOLer June 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

.

- LOLer October 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

He He my mistake. Agreed LOLer. Loved the statement **(Try thinking about what you write, once in a while).** :D . Actually I thought - but wrongly.

Min span tree still works - come to think about it - in case any vertex with p=0 is removed, unless there is a connectivity issue in the graph.
[Also I said *should*. I never said - "It did". :(
But yes - u do have a point. I should have said "MAY".]

Can you please add the constraint - and give me another example?

- Lord Darth Plaguies June 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ah uh oh...2nd time same mistake. Wrong again.
---Ignore my response.

- Lord Darth Plaguies June 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This time me, Sidious,Padme and Yoda all sat together and thought about it. No kidding :(
Looks like the issue is:
1. "All the vertex with non zero probability we need to traverse".
2. "Also minimize the path weight"
Hence -
"Find a path with minimal weight so that it goes through the vertex which are having non zero probability"

@LOLer -->Is the above a proper formulation?
Sorry for being a dumb :( for the 1st time.

- Lord Darth Plaguies June 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In case that is the formulation - then
"Find shortest path connecting all vertices reachable from a single point" is the same formulation.
The above one - implies
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

- Lord Darth Plaguies June 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In case those kind of node exists - we would 1. Either remove them to create a pass-through path.
2. In case a path through them is more economical than a direct connection we would keep them.

Note: Original Problem did not tell anything about 0 probability.

- Anonymous June 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

--Even that wont do. :(
--Ignore.

- Anonymous June 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This should do the trick.
http://mathworld.wolfram.com/ChinesePostmanProblem.html
**A problem asking for the shortest tour of a graph which visits each edge at least once (Kwan 1962; Skiena 1990, p. 194).**

http://www.uclic.ucl.ac.uk/harold/cpp/CPP.java

[Whops - a days end]
[Was not simple to find even.]

- Lord Darth Plaguies June 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And why would this work? Why do you even have to visit each edge? The example I gave in counterexample to your MST still disproves that this won't work.

- LOLer June 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That should read ...also proves that this won't work...
instead of "still disproves...".

- LOLer June 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nops.
LOLer - did you read the comment?

Note: Original Problem did not tell anything about 0 probability.

In case those kind of node exists - we would 1. Either remove them to create a pass-through path.
2. In case a path through them is more economical than a direct connection we would keep them.
That does not change the problem.
In case you still disagree - I need to find the guy who gave the puzzle and get a communication out of him.
Thanks.

- Lord Darth Plaguies June 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even if you do that and it is correct, so what? What are you arguing for? The all edges version? The all edges solution is incorrect, even if we disallow 0 probability. I will leave it to you to figure that out.

The puzzle is on the facebook website, you can email them and ask them for clarification...

- LOLer June 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think LoLer is right. You are not visiting every edge, you are trying to visiting every edge. A couple of edges needn't be visited.

- Anonymous July 04, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is what I feels
1. Starts from first node
2. Its a complete graph

lets say we have solution for 2 nodes
We can find out quickly which node to start in case of 2 nodes

Now adding a third node means only including it in any of the 3 positions
3-2-1 or 2-3-1 or 2-1-3.
Earlier order need not be affected because it is already optimal and new addition wont change the order as the tree is complete

I guess we can keep on adding new nodes
n*n*n maximum ........

- Syam July 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

uh...
I don't think it work. If you add a new node, your previous optimal sub may be changed.

- Camoranesi July 04, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would rephrase the problem this way:

Given a graph (V,E) where V = the set of all vertices and E = the set of all edges between them.

To each edge E(v1,v2) assign weight = P(v2) * t(v1->v2).

Now, given a starting vertex V, produce a traversal order of the graph such that the vertices with non-zero probability are visited at least once and the weight of the traversal is minimized.

- bokay July 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I've solved this one and DP+DFS will be enough to solve this puzzle. Good luck.

- Anonymous September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am not sure how to solve the problem with DP + DFS as the walk could consists of loop. I cannot justify the subproblem of optimal solution is also optimal.
I guess using DFS is able to find a solution, but might not be the optimal one

- Anonymous January 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't it a general case of TSP? (If assign same probability for every node). NP-Complete therefore? Did I miss anything here?

Two tricks I will do:
1. Use some heuristic function to get near minimal at the beginning.
2. Run "All pairs short path" at the beginning and store the result. So calculating cost for every possible permutation might be improved.

- Anonymous September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem can be simplify as finding a walk of the Graph, that traverse all the nodes while minimize the sum of di*pi, where pi is the possibility and di is the distance of the walk from the source.
I do not think it is a P complete problem as it is even more complicate than the mail man problem, which, as far as I can remember, is a NP-complete issue.

- Anonymous January 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

apply greedy algo with probbality/distance as a key at each vertex

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

The probem of finding the minimum cost k intermediate nodes between source s and destination d can be solved with following
DP formulation

V(k) = Min( V(k-1) + P(k)*DkDk-1)

This simply means if there is path with k-1 length then the path of k length for any given node exists provided there is path
from that node to end node of k-1 path. Also taking the min cost for k intermediate node to get optimal path.


Now the problem remains is what to choose for source s and destination d. We dont know !

so apply above algorithm for each possible s and d

for(i = n0; i < nn; i++)
for(j = i+1; j < nn; j++ ) //to avoid duplicates like ij, ji start from j=i+1
V(k) = Min( V(k-1) + P(k)*DkDk-1)

- Naresh October 10, 2010 | 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