Microsoft Interview Question for Software Engineer in Tests






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

I believe, It is a BFS. You do a BFS using a queue. Every time, You take out a element from queue to examine...
If( leafNode )
Add it to Linklist
else
Add its children to Queue.

This way, you get the required ordering in the link list as well.

- Ravi December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Do a DFS of tree, start
1. Push root on stack.
2. while stack is not empty, repeat below steps
3. Pop an element from stack and add it to list, if it has no children.
4. Push right node first on stack.
5. Push left node on stack.

- anonymous July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

clearly specify what does the question wants

- pranav December 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do InorderTraversal.
During traversal once you get a leaf node, add in the Linked list( it's single linked list, that's what I ahve udnerstood from the question, doesn't have a previous pointer).
Time complexity = O(N).

- Gautam December 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For n element tree, there can be at maximum ceil(n/2) leaf nodes. From the problem, I see that the nodes are appended at the tail end of the list. So, during the traversal, if we want to add a node to the linked list, we have to traverse all the way to the end of the linked list and then insert it. This will be O(n^2) ( n for inorder traversal and n for linkedlist traversal )

If allowed, one way would be to keep track of tail node of the linked list after each insertion. Then it will be O(n). Any other method to do this problem?

- rkt December 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@rtk: It' not necessary to add the leaf nodes at the end of the link list, you can add them at the front of the list also :) . Adding every element to the front of the linked list is O(1). Thus in all the algorithm would take O(N).

- Guess Who?? December 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a Depth First Search and keep on adding the element.

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

Do a dfs traversal using stack and whenever a child does not have children, append to list.
1. push root node on stack. pop stack and push right child node on stack and then left child node on stack.
2. pop stack. It will give a child. If new node does not have child, then append to linked list else push right child and then left child on stack.

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

typedef struct tree{
             int data;
             struct tree *left;
             struct tree *right;
}*node;
typedef strict linklist{
                 int data;
                 struct linklist *next; 
}*list;

list * InorderTraversal( node *root )
{
   static list *head=NULL; // static head pointer
   if( root == NULL )
   {
         return;
   }
   else if( root->left == NULL && root->right == NULL )
   {
        if( head == NULL )
        {
         head = (list *) malloc(sizeof(list));
           head->next = NULL;
           head->data = root->data;
        }
        else
        {
          list *p = NULL;
          p = head;
          while( p->next != NULL )
          {
              p = p ->next;
          } 
      p->next = (list *) malloc(sizeof(list));
          p = p->next;
          p->next = NULL;
          p->data = root->data;
        } 
   }
   else
   {
        InorderTraversal( root->left );
        InorderTraversal( root->right );
   }
  return head;
}

- siva.sai.2020 December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is there any bug in my code ?

- siva.sai.2020 December 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class node {
    public node next;
    public tnode data;
}

public class tnode {
    public tnode left;
    public tnode right;
    public int data;
}

public class printLeaf {
    public static void printLeaf(tnode treenode,node head){
        if(treenode.left !=null){
            printLeaf(treenode.left,head);
        }
        if(isLeaf(treenode)){
            appendtoTail(head,treenode);
        }
        if(treenode.right !=null){
            printLeaf(treenode.right,head);
        }       
    }

    private static boolean isLeaf(tnode treenode) {
        if(treenode.left==null && treenode.right==null)return true;
        return false;
    }

    private static void appendtoTail(node head, tnode treenode) {
        if(head.data==null){
            head.data=treenode;
            return;
        }
        node cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        node newnode=new node();
        newnode.data=treenode;
        cur.next=newnode;
    }
}

- Test May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Modified Inorder traversal.
However, it changes the structure of the tree.

void LL(struct node* root,struct node** start)
{
        static struct node* tail;
        if(root)
        {
                LL(root->left,start);
 
                if(isLeaf(root))
                {
                        if(!*start) 
                        {
                                *start=root;
                                tail=root;
                        }
                        else 
                        {       tail->right=root;
                                tail=tail->right;
                        }
                }
                LL(root->right,start);
        }
}

start points to the head of the linked list.

Complete code: ideone.com/6WhB3

- Aashish July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An approach which doesn't modify the tree.

int isLeaf(struct node* r)
{
        return !(r->left || r->right);
}
 
void LL(struct node* root)
{
        if(root)
        {
                LL(root->left);
 
                if(isLeaf(root))
                        printf("%d ",root->data);
                        
                LL(root->right);
        }
}

ideone.com/6Aj2t

- Aashish July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct tree
{
int data;
struct tree *left;
struct tree *right;
}*root;

struct list
{
int data;
struct list *link;
}*node;

void create_List(struct tree *root)
{
if(!root->left && !root->right)
{
struct tree *temp=(list*)malloc(sizeof(struct list));
temp->data=root->data;
temp->link=root->left;
node=temp;
return;
}
create_List(root->right);
create_List(root->left);
}

void main()
{
root=NULL;
node=NULL;
create_List(root);
while(!node)
{
printf("%d->",node->data);
node=node->link;
}
}

- xyz July 14, 2012 | 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