Microsoft Interview Question for Software Engineer / Developers


Team: Bing
Country: United States
Interview Type: In-Person




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

A O(n) solution:
1st pass: Copy the DLL like this: every copied node is placed as the next of original node.
Example: 1->2->3->4 =======> 1->1'->2->2'->3->3'->4->4'
2st pass: Copy the random pointer, making the random pointer of copied node point to the next node of random pointer of original node.
Example:
originalNode->right->left=orignalNode->left->right;
originallNode=originalNode->right->right;
3rd pass: Divide the DLL into two DLL.
Time: O(3n)=O(n)

- g233007 February 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you! Really good solution in O(n) time and O(1) space.

- numa February 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess its
originalNode->right->left=orignalNode->left;
instead of
originalNode->right->left=orignalNode->left->right;

- anonymous March 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Duplicate it the way you'd duplicate any (not necessarily acyclic) directed graph: Perform a depth-first traversal, keeping a memo of partially completed copies to handle cycles.

def deep_copy(root):
    memo = { None : None }
    def copy(node):
        if node not in memo:
            clone = Node()
            memo[node] = clone
            clone.left = copy(node.left)
            clone.right = copy(node.right)
        return memo[node]
    return copy(root)

- psykotic February 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are ways to solve the less general problem that was given with less extra space.

- eugene.yarovoi February 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Eugene: He says the left pointers are arbitrary, so you cannot assume the linked structure is, say, acyclic. But like most questions on this site, the question is vague and poorly specified, so answering it requires guesswork, and it's possible the question as originally asked was more specific.

- psykotic February 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even if the structure has cycles, there are ways to copy it that only involves O(1) memory in addition to the memory for the new structure. The algorithm has been discussed in earlier duplicates of this question. The basic approach is that you interleave the new linked list with the old: oldNode0 -> newNode0 -> oldNode1 -> newNode1 -> ... -> oldNodeN -> newNodeN, and then you perform something like:

Node* current = list->head;
while (current != null)
{
    // current->next is the copy of current
    // current->random->next is the copy of current's randm node
    // the copy's random will be the random's copy
    current->next->random = current->random->next;
    current = current->next->next;
}

Then de-interleave, and voila!

- eugene.yarovoi February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, if you're allowed to temporarily modify the existing data structure (which I assumed was illegal), then all kinds of things are possible. The interleaving is definitely a nice and simple way of doing it!

- psykotic February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the response

- Anonymous February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@psykotic: Ah, I see what you were thinking.

- eugene.yarovoi February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please see this website nobrainer .co .cc
There is an awesome example with explanation
nobrainer .co .cc/2012/02/clone-linked-list-with-next-and-random.html

- Anony February 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Do a shallow copy, and maintain the new node address and data value pair in a hash table.
2. Now traverse the new list, using the hash table as reference and update the random pointer.
Complexity O(n+n)

- MaxBrenner February 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if there is repetition in Values. hash table will point to a wrong node for arbitirary

- V February 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

saurabhhack123.blogspot.in

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

My solution to the problem is similar to MaxBrenner, but I made the new node and the corresponding source node address in the map, and use it as a reference and update the previous pointer.



typedef Node<int> IntNode;
void duplicate_linkList(IntNode *pSourceList,IntNode* &pTargeList)
{
if(pSourceList == NULL)
return ;
map<IntNode*,IntNode*> Map;
pTargeList = new IntNode(pSourceList->data);

Map.insert(std::map<IntNode*,IntNode*>::value_type(NULL,NULL));
Map.insert(std::map<IntNode*,IntNode*>::value_type(pSourceList,pTargeList));

IntNode *soureItr = pSourceList->next;
IntNode *tarItr = pTargeList;

while(soureItr != NULL)
{
IntNode *next = new IntNode(soureItr->data);
tarItr->next= next;
Map.insert(std::map<IntNode*,IntNode*>::value_type(soureItr,tarItr));
tarItr = next;
}
tarItr->next = NULL;
soureItr = pSourceList;
tarItr = pTargeList;
while(soureItr != NULL)
{
IntNode *t = (Map.find(soureItr->pre))->second;
tarItr->pre = t;
tarItr = tarItr->next;
soureItr = soureItr->next;
}

}

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

we can use 2 hash
hash1 stores each node of list1 as <node, its position in list>
hash2 stores each node on new list created as <position in new list, node of new list>
in first for loop we create all new nodes with next pointers(right pointers)
in second for loop we assign the prev pointers(random pointer)

Space complexity: O(2n) = O(n)
time complexity: O(2n) = O(n)
let me know if this is correct?

public LinkedList<Integer> createCopy(LinkedList<Integer> list1)
	{
		HashMap<Node<Integer>, Integer> hash1 = new HashMap<Node<Integer>, Integer>();
		HashMap<Integer, Node<Integer>> hash2 = new HashMap<Integer, Node<Integer>>();
		
		LinkedList<Integer> list2 = new LinkedList<Integer>();
		int position = 0;
		
		for(Node<Integer> current1 = list1.head; current1!=null; current1 = current1.next, position++)
		{
			Node<Integer> newNode =  list2.insert(current1.value);
			hash1.put(current1, position);			
			hash2.put(position, newNode);
		}
		
		for(Node<Integer> current1 = list1.head, current2 = list2.head; 
				current1!=null; 
				current1 = current1.next, current2 = current2.next)
		{
			int pos = current1.prev == null? -1 : hash1.get(current1.prev);
			current2.prev = pos == -1? null : hash2.get(pos);
		}
		
		return list2;
	}

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

n->l : node->left
r1 = root of original node
r2 : root of duplicated node

LLD()
	n = r1
	r2 = CN(n->d)
	r2->l = r1
	n2 = r2
	while(n)
		n2->r = CN(n->r->d)
		n2-l = n
		t = n->r
		n->r = n2
		n = t
		n2 = n2->r
	n = r2
	while(n)
		t = n->l
		l = t->l->r
		n->l = l
		n = n->r

- Prateek Caire February 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.


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