Microsoft Interview Question for Software Engineer / Developers


Team: Bing
Country: India
Interview Type: In-Person




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

1. clone the list without considering random pointers. For each node, add an entry
to a hashmap: old_addr -> clone_addr
2. use the hashmap to set up the random pointers

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

Question not clear, if linked list is singly linked nothing can be done. If it is doubly linked ... cannot think beyond please provide a sample.

- Doctor.Sai December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its something like

singly linked list
1->2->3->4
and random ptrs like
1->3->2->4->1

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

copying the next node is fine. For random pointer I guess pointer arithmetic should work fine.

dist = (ptr->next_random) - (ptr);
ptr_new->next_random = ptr_new + dist;

I think this should work both for positive and negative dist.

- fred December 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ anonymous : can u give a code for the same...

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

I guess the approach of anonymous is fine. Last weekend same question has been asked from me and I gave the same approach. Interviewer was looking satisfied with the answer.

- Deepak December 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

plzzz provide code

- anonymous December 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is a pseudo code

List* cloneList(List *p)
{
	List cloneHead();
	List* clonePtr = &cloneHead;
	HashTable hTable();
	char *curp = NULL;
	
	//copy the next pointers and add the original list node address as key in hash table and clone node address as value in list
	for(curp = p; curp != NULL; curp = curp->next)
	{
		clonePtr->next = copyNode(curp);
		hTable.insert((long)curp, (long)clonePtr->next);
		clonePtr = clonePtr->next;
	}
	clonePtr->next = NULL;
	
	// set the random pointers for the clone list
	for(clonePtr = cloneHead.next, curp = p; curp != NULL; curp = curp = curp->next)
	{
		List *node = hTable.search((long)curp->rand_ptr);
		clonePtr->rand_ptr = node;
		clonePtr = clonePtr->next;
	}
	
	return cloneHead.next;
}

List* copyNode(List *p)
{
	List *node = new List();
	node->data = p->data;
	node->next = p->next;
	node->rand_ptr= NULL;
	return node;
}

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

There is a way by which it can be done even with randomised pointer locations, but the original list will be destroyed...
First we make a new list where next node points correctly and the random pointer points to the corresponding rand node of the original list.
That is:-
ptr_new->rand=ptr->rand
In the next step we traverse both lists simultaneously and realign the random pointers in the original list to their new list nodes.
That is:-
ptr->rand=ptr_new
In the third step we do a similar simultaneous traversal and assign the random pointer as
new_ptr->rand = new_ptr->rand->rand
The problem with this method as i said before is that the original list is destroyed...

- anoopananth December 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

class Node {
    public:
    int key;
    Node* next;
    Node* rand;
};
class List {
    public:
    Node* head;

    List() {
        head = NULL;
    }

    Node* newNode(Node* node) {
        Node* newNode = new Node();
        newNode->key = node->key;
        newNode->rand = node->rand;
        node->next = newNode;
        return newNode;
    }

    List clone() {
        Node *iter = head, *curr = NULL, *tail = NULL, *temp;
        List clone;
        while (iter != NULL) {
            curr = iter;
            iter = iter->next;
            temp = newNode(curr);
            if (list.head == NULL) {
                tail = clone.head = temp;
            } else {
                tail->next = temp;
                tail = temp;
            }

- kartikaditya December 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The best resource:
geeksforgeeks.org/archives/1155

- Srikant Aggarwal January 12, 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