Microsoft Interview Question


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




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

public static void Insert(Node first, int n)
{
    Node temp=new Node(n);
    if(first==null || first.value>n)
    {
        temp.next=first;
        first=temp;
        return;
    }
    while(first.next!=null && first.next.value<n)
        first=first.next;
    temp.next=first.next;
    first.next=temp;
    return;
}

- Anonymous January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You should always return the head.

first=temp;

This statement does nothing useful, Java passes by value.

- Anonymous February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class SortedLinkedList {
	Node head;
	
	class Node{
		int value;
		Node next;
		public Node(int value) {
			super();
			this.value = value;
			this.next = null;
		}
		
	}
	
	public void add(int value){
		Node newnode = new Node(value);
		if(head == null){
			head = newnode;
			return;
		}
		Node ptr = head;
		Node prev = null;
		while(ptr != null && value > ptr.value){
			prev = ptr;
			ptr = ptr.next;
		}
		if(ptr == head){
			newnode.next = head;
			head = newnode;
			return;
		}
		if(ptr == null){
			prev.next = newnode;
			return;
		}
		newnode.next = ptr;
		prev.next = newnode;
	}
	
	public void print(){
		Node ptr = head;
		if(ptr == null){
			return;
		}
		while(ptr != null){
			System.out.print(ptr.value);
			ptr = ptr.next;
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SortedLinkedList ll = new SortedLinkedList();
		ll.add(3);
		ll.add(9);
		ll.add(6);
		ll.add(7);
		ll.add(3);
		
		ll.print();
	}

}

- Kevin February 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

void insert(node* &root, int x)
{
    node *start = root;
    node *n = new node(x);
    if (!root)
    {
        root = n;
        return;
    }
    if (root->info > x)
    {
        n->next = root;
        root = n;
        return;
    }
    while ((start->next) && (start->next->info < x))
    {
        start = start->next;
    }
    n->next = start->next;
    start->next = n;
}

- Mihail Burduja January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you are missing the case when the node you want to insert is lower than the head

- francisco.gutierrez.91 January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fixed it!

- Mihail Burduja January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Javascript, assuming we have LinkedList class with head property:

LinkedList.prototype.insertNode = function(value){
  var prev = {
    data: null,
    next: this.head
  };
  var current = this.head;
  while(current!==null && current.data < value){
    prev = current;
    current = current.next;
  }
  prev.next={
    data:value,
    next:current
  };
  if(prev.data === null)
    this.head = prev.next;
};

- S.Abakumoff January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Whenever we have to insert a node into a sorted single linked list we have to take care of these cases:
1. the node we want to insert is null?
2. the list is empty (==null)?
3. the node to be inserted will fit before the head of the linked list?
4. and ask the interviewer what to do if the linked list contains values == node.value


after checking the 3 things :

Node temp = root;
while(temp.next!= null){
   if(temp.next.value < node.value) temp = temp.next;
   else break;
}
node.next = temp.next;
temp.next = node;

- Kamy January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not the best code ever, but I think it works!

Public void AddtoSortedLinkList(Node head, Node addTo)
{
	if (head == null) {head = addTo; return;}
	if (head.next == null) 
	{
		if (head.item < addTo.item) head.next = addTo;
		else head = addTo;
		return;
	}
	Node prev = new Node();
	Node cur = new Node();
	prev = head;
	cur = prev.next;
	while(cur.next != null)
	{
		if (cur.item < addTo.item)
		{
			prev = cur;
			cur = cur.next;
			continue;
		}else
			break;
	}
	if (cur.item < addTo.Item ) cur.next = addTo;
	else
	{
		addTo.next = cur;
		prev.next = addTo;
	}
}

- Golnaz May 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void InsertToSortedList(stIntList* pSorted, int data)
{
stIntList* pTemp = pSorted;
stIntList* pPrev = NULL;

while(pTemp != NULL)
{
pPrev = pTemp;

if(pTemp->data >= data)
{
stIntList* pNew = new stIntList;
pNew->data = data;
pNew->pNList = pTemp->pNList;
pPrev->pNList = pNew;
std::cout<<"\nInseted Succesfully "<<data<<" at "<<pNew;
break;
}
pTemp = pTemp->pNList;
}
}

- rishikantku June 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void InsertToSortedList(stIntList* pSorted, int data)
{
stIntList* pTemp = pSorted;
stIntList* pPrev = NULL;

while(pTemp != NULL)
{
pPrev = pTemp;

if(pTemp->data >= data)
{
stIntList* pNew = new stIntList;
pNew->data = data;
pNew->pNList = pTemp->pNList;
pPrev->pNList = pNew;
std::cout<<"\nInseted Succesfully "<<data<<" at "<<pNew;
break;
}
pTemp = pTemp->pNList;
}
}

- rishikantku June 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void insert(node **head,int data)
{

if(!head)
return;

struct node *temp=head;
struct node * newnode=(struct node *)(malloc(sizeof(struct node)));
newnode->next=NULL;

while(temp)
{
if(temp->data<data)
{temp=temp->next;
}
else
{
newnode->next=temp->next;
temp->next=newnode;
break;
}

}

}

code in C

- akie July 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tested for all cases. Worst case complexity will be O(N) if the elements are already sorted before insertion.

// Add in sorted order
	public void addSorted(MyLinkedList<Integer> list, Integer data)
	{
		MyNode<Integer> newNode = new MyNode<Integer>(data);
		
		if(list.head == null)
		{
			list.head = newNode;
			return;
		}
		
		MyNode<Integer> temp = list.head;
		
		while(temp!=null)
		{
			if(temp.data > data)
			{
				MyNode<Integer> temp1 = temp.next;
				temp.next = new MyNode<Integer>(temp.data);
				temp.next.next = temp1;
				temp.data = data;
				break;
			}
			else{
				if(temp.next == null){
					temp.next = newNode;
					break;
				}
				temp = temp.next;
			}
		}

}

- obaidsalikeen June 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

temp = head
Node newNode = new Node();
while(temp.key < newNode.value)
{
     temp = temp.next
}

newNode.next = temp.next;
temp.next = newNode;

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

you are missing some special cases, what if you have to insert at the head or at the end, and you don't update head pointer

- francisco.gutierrez.91 January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code doesn't work.
1. newNode doesn't have a value
2. after the while loop temp holds the first node bigger than the node you're inserting, so you're inserting the node in the wrong position

- Mihail Burduja January 24, 2013 | 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