Facebook Interview Question for Software Engineer / Developers






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

by doing a bfs starting at given node we can visit all nodes, each time an new node is discovered (until the termination of bfs), create a new node copying its value, however leaving neightbors field null. create a hashtable mapping address of the old node to newly created node. now its just a matter of translation and returning the address (translated from old) of the new root.

- light April 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

aren't you counting on the old graph to still be alive?
It doesn't sound like a healthy assumption...

- ZeDuS September 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Node
{
public int Value { get; set; }
public List<Node> Neighbors { get; set; }

public Node(int value)
{
this.Value = value;
}
}

public static Node CloneGraph(Node srcGraph)
{
if (srcGraph == null)
{
return null;
}

Dictionary<Node, Node> srcToDestNodeMapping = new Dictionary<Node, Node>();

Node destGraph = DfsCreateDestNodes(srcGraph, srcToDestNodeMapping);

foreach (Node srcNode in srcToDestNodeMapping.Keys)
{
foreach (Node neighbor in srcNode.Neighbors)
{
srcToDestNodeMapping[srcNode].Neighbors.Add(srcToDestNodeMapping[neighbor]);
}
}

return destGraph;
}

private static Node DfsCreateDestNodes(Node srcNode, Dictionary<Node, Node> srcToDestNodeMapping)
{
if (srcToDestNodeMapping.ContainsKey(srcNode))
{
return null;
}

Node destNode = new Node(srcNode.Value);
destNode.Neighbors = new List<Node>();

srcToDestNodeMapping.Add(srcNode, destNode);

foreach (Node n in srcNode.Neighbors)
{
DfsCreateDestNodes(n, srcToDestNodeMapping);
}

return destNode;
}

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

public class Node
    {
        public int Value { get; set; }
        public List<Node> Neighbors { get; set; }

        public Node(int value)
        {
            this.Value = value;
        }
    }

    public static Node CloneGraph(Node srcGraph)
    {
        if (srcGraph == null)
        {
            return null;
        }

        Dictionary<Node, Node> srcToDestNodeMapping = new Dictionary<Node, Node>();

        Node destGraph = DfsCreateDestNodes(srcGraph, srcToDestNodeMapping);

        foreach (Node srcNode in srcToDestNodeMapping.Keys)
        {
            foreach (Node neighbor in srcNode.Neighbors)
            {
                srcToDestNodeMapping[srcNode].Neighbors.Add(srcToDestNodeMapping[neighbor]);
            }
        }

        return destGraph;
    }

    private static Node DfsCreateDestNodes(Node srcNode, Dictionary<Node, Node> srcToDestNodeMapping)
    {
        if (srcToDestNodeMapping.ContainsKey(srcNode))
        {
            return null;
        }

        Node destNode = new Node(srcNode.Value);
        destNode.Neighbors = new List<Node>();

        srcToDestNodeMapping.Add(srcNode, destNode);

        foreach (Node n in srcNode.Neighbors)
        {
            DfsCreateDestNodes(n, srcToDestNodeMapping);
        }

        return destNode;
    }

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

public void clone(Node root) {

       if(root == null) return null;
       Node r1 = createClone(root);
       for(Node n : root.adjacentNodes()) {
             Node n1 = clone(n);
             r1.addNode(n1);

       }
       return r1;

}

- Naresh Pote June 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void clone(struct node* root)
{
if(p == NULL)
retutn ;
struct node *p = (struct node *)malloc(sizeof(struct node));
p->left = clone(root->right);
p->right = clone(root->left);
}

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

Since it is not a binary tree, this solution is wrong. We need to clone all the neighbors.
But question also does not mention that it is a acyclic graph (or tree), so that is more difficult.

- ksh October 12, 2011 | Flag


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