Microsoft Interview Question for Software Engineer in Tests






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

BST to Circular DLL
static BSTNode prev, head;

	public static BSTNode convetToCLL(BSTNode root) {
		if (root == null)
			return null;
		convetToCLL(root.getLeft());
		if (prev == null) { // first node in list
			head = root;
		} else {
			prev.setRight(root);
			root.setLeft(prev);
		}
		prev = root;
		convetToCLL(root.getRight());
		if (prev.getRight() == null) { // last node in list
			head.setLeft(prev);
			prev.setRight(head);
		}
		return head;
	}

- Vir Pratap Uttam May 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
10
of 10 vote

BST to Doubly Linked List
static BSTNode prevDLL, headDLL;
	public static BSTNode convetToDLL(BSTNode root) {
		if (root == null)
			return null;
		convetToDLL(root.getLeft());
		if (prevDLL == null) { 
			headDLL = root;
		} else {
			prevDLL.setRight(root);
			root.setLeft(prevDLL);
		}
		prevDLL = root;
		convetToDLL(root.getRight());
		return headDLL;
	}

- Vir Pratap Uttam May 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Basic idea: If we could get a doubly linked list + it's head + it's tail, we could use recursion.

Recursively create list of left subtree, of right subtree and insert the root in the middle, returning the head of left tree and tail of right tree.

Pseudo code...

DLL & TreeToDLL(Tree & t) {
    
     // do base cases, left out.

     DLL l = TreeToDLL(t.Left);
     DLL r = TreeToDLL(t.Right);

     LLNode n = new LLNode(t.value);

     return Concat(l, n, r);
}

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

Aagh. formatting all screwed up. Apologies.

- Anonymous August 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And 'do not use additional data structure' is violated... but the idea still works.

Use left as previous and right as next, and have the routine return the head and tail...

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

void join(Node a, Node b) {
a->large = b;
b->small = a;
}


/*
helper function -- given two circular doubly linked
lists, append them and return the new list.
*/
static Node append(Node a, Node b) {
Node aLast, bLast;

if (a==NULL) return(b);
if (b==NULL) return(a);

aLast = a->small;
bLast = b->small;

join(aLast, b);
join(bLast, a);

return(a);
}


/*
--Recursion--
Given an ordered binary tree, recursively change it into
a circular doubly linked list which is returned.
*/
static Node treeToList(Node root) {
Node aList, bList;

if (root==NULL) return(NULL);

/* recursively solve subtrees -- leap of faith! */
aList = treeToList(root->small);
bList = treeToList(root->large);

/* Make a length-1 list ouf of the root */
root->small = root;
root->large = root;

/* Append everything together in sorted order */
aList = append(aList, root);
aList = append(aList, bList);

return(aList);

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

Use logic of inorder traversal

void ConvertTreeToDLL(TreeNode *currentNode) {
	if(currentNode) {
		// Go all the way to left most
		ConvertTreeToDLL(currentNode -> left);
		
		// Create the list
		
		// If head is null, create it
		if(!head) {
			head = (DLL *) malloc(sizeof(DLL));
			head -> value = currentNode -> value;
			head -> left = 0;
			head -> right = 0;
			cachedPrevNode = head;
		}
		
		// else add it to current list
		else {
			newNode = (DLL *) malloc(sizeof(DLL));
			newNode -> value = currentNode -> value;
			newNode -> left = cachedPrevNode;
			cachedPrevNode -> right = newNode;
			cachedPrevNode = newNode;
		}
		
		// Go all the way to rigt most
		ConvertTreeToDLL(currentNode -> right);
		
	}
}

- KP August 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

{{
node* tree2list(node *tree)
{
node *head = NULL;
if (tree)
{
if (tree->left)
{
node *l = tree2list(tree->left);
head = l;
while (l->right)
l = l->right;
l->right = tree;
tree->left = l;
}
else
head = tree;

if (tree->right)
{
tree->right = tree2list(tree->right);
tree->right->left = tree->right;
}

}

return head;
}
}}

- SL August 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

btree* attachList(btree *a, btree* b)
{
        btree *temp;
        if(a == 0)
                return b;
        if(b == 0)
                return a;

        a->left->right = b;
        b->left->right = a;
        temp = a->left;
        a->left = b->left;
        b->left = temp;
        return a;
}

btree* convertToList(btree *p)
{
        btree *pLeft, *pRight;
        if(p == 0)
                return 0;

        pLeft = convertToList(p->left);
        pRight = convertToList(p->right);
        p->left = p->right = p;
        p = attachList(pLeft, p);
        p = attachList(p, pRight);
        return p;
}

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

TL(n, b)
	if(n == NULL)
		return NULL
	n->l = TL(n->l, 0)
	n->r = TL(n->r, 1)
	if(b == 0)
		return n->r
	else
		return n->l

- Prateek Caire September 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Improved one..

TL(n, b)
	if(n == NULL)
		return NULL
	a = TL(n->l, 0)
	n->l = a
	a->r = n
	b = TL(n->l, 0)
	n->r = b
	b->l = n
	if(b == 0)
		if(n->r)
			return n->r
		else
			return n
	else
		if(n->l)
			return n->l
		else
			return n

- Prateek Caire September 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

errata: b = TL(n->r, 1)

- Prateek Caire September 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

errata: b = TL(n->r, 1)

- Prateek Caire September 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

errata: b = TL(n->r, 1)

- Prateek Caire September 21, 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