Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

Here is an implementation in C#. head of linked list is passed by ref because head may be deleted and so it may change.

public static void Delete(ref Node head, int data)
        {
            if (head != null)
            {
                Node curr = head;
                if (curr.data == data)
                    head = head.next;
                else
                {
                    while (curr.next != null)
                    {
                        if (curr.next.data == data)
                        {
                            curr.next = curr.next.next;
                            return;
                        }
                        curr = curr.next;
                    }
                }
            }
        }

- berkaypamir November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int deleteNode(LinkedListNode head, LinkedListNode n)
{
	      if( n == null) return -1;  
	      else
	      {
	    	   LinkedListNode current = head;
	    	   LinkedListNode previous = null;
                   while(current != null)
                   {
                       if(current.value == n.value)
                      {
                  //deletes first or middle elements, copy the node values from next node and deletes the //next node
                     if(current.next != null) 
                        {
                            current.value = current.next.value;
                            current.next = current.next.next;
                             return 0;
                         }
                         else
                         {
                            previous.next = null;
                            return 0;
                          }
                   }
                   //store the previous here just in case if the element to be deleted is the last node
                   previous = current; 
                   current = current.next; //move current to the next element
               }
	      }
	      return 1; 
}

- hello world January 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int deleteNode(LinkedListNode head, LinkedListNode n)
{
if( n == null) return -1;
else
{
LinkedListNode current = head;
LinkedListNode previous = null;
while(current != null)
{
if(current.value == n.value)
{
//deletes first or middle elements, copy the node values from next node and deletes the //next node
if(current.next != null)
{
current.value = current.next.value;
current.next = current.next.next;
return 0;
}
else
{
previous.next = null;
return 0;
}
}
//store the previous here just in case if the element to be deleted is the last node
previous = current;
current = current.next; //move current to the next element
}
}
return 1;
}

- raj March 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

when there is only 1 element
previous is null
you cannot access previous

previous.next = null is an error

else
{
previous.next = null;
return 0;
}

- haile August 26, 2017 | 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