Microsoft Interview Question for Software Engineer / Developers






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

How about creating a XML file from the given n-ary tree using level order traversal (similar breath first search in graphs)
So if "a" has two children "b" and "c" and this is the graph to be serialized then we can make like

<Node>
a //Name of that current node
b,c //Comma separated children of the current node from left to right.
</Node>
<Node>
b
</Node>
<Node>
c
</Node>

With the help <Node> tag in the file, we know that the next line will be holding the current node and the following line will hold either the children of the currently referred node from left to right comma separated or the closing tag </Node> If we encounter the closing tag after the current node value, it indicates that the currently referred node is a left node.

So this has complexity of O(n) in serializing and de-serializing. It works for n-ary case in general.

- UB_Green January 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

solution:(notice to remove the '/' in the link)
w/w/w.careertea.com/techforum/post.aspx?PostID=19

- codyboy January 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@codyboy
Dont do advt of ur site here... that site sux, first learn urself dude

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

//Serialize the tree.Writing the tree values to a file or array is serialization.
        //Storing the data from the tree in an array. or we can store the node itself in an object array.We have to
        //design accordingly.
        public void Serialize(Node root,ref int []treeArray,ref int index)
        {          
            
                if (root == null)
                {
                    treeArray[index++] = -999;
                }
                else
                {
                    treeArray[index++] = root.ID;
                    Serialize(root.Left, ref treeArray, ref index);
                    Serialize(root.Right, ref treeArray, ref index);
                }
            
        }
        //DESerialize the tree.Creating the tree from the data stored in the file or array.
        public Node DeSerialize(ref int[] treeArray, ref int index,int arrayCount)
        {
            if (treeArray[index] == -999 && index < arrayCount)
                {
                    index++;
                    return null;
                }
                else 
                {
                    Node node = new Node();
                    node.ID = treeArray[index++];
                    node.Left = DeSerialize(ref treeArray, ref index,arrayCount);
                    node.Right = DeSerialize(ref treeArray,ref index,arrayCount);
                    return node;
                }
         }

        //Complexity is O(n)
        Please comment...

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

I think this works.It's a neat solution.

- Jesukumar January 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well... event if this works that's not what original task states to do. Originally it's about "a n-ary tree" but not BST..

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

good solution

- Karthik January 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A solution can be to serialize the n-ary tree into a postfix notation and save to disk. A special token can be used to separate the children from the parent.
Then the tree can be deserialized using a stack.

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

Can't we use the Prufer number to encode the tree and write that sequence to file.
To deserialize it read the prufer sequence from file and convert back to the tree by decoding the prufer sequence.

OR another way could be store the edges themselves :)

Please comment on this!!!

- Anonymous February 19, 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