Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

Node reverseAlterante ( Node head) {
if ( head == null || head.next == null )
        return head;
Node r = reverseAlternate(head.next.next);
Node t = head.next ;
t.next = head;
head.next = r;
return t ;
}

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

good solution

- practioner January 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

error: 'Node' does not name a type
compilation terminated due to -Wfatal-errors.

when i execute the code this particular error
comes can u solve this
thanx

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

dude this is not the complete code; just a function. I am not sure how you coded the whole program. The function is in Java. You will have to create a class Node and then write the code for linked-list creation.

- learner January 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
7
of 7 vote

public LinkedListNode reverseAlternate(LinkedListNode fHead)
        {
            if (fHead.Next == null)
            {
                return fHead;
            }
            else
            {
                LinkedListNode temp1 = fHead;
                LinkedListNode temp2 = fHead.Next;

                temp1.Next = reverseAlternate(temp2.Next);
                temp2.Next = temp1;

                return temp2;
            }
        }

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

yes

- omer asim oztop January 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct

- silveranbead January 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

while(p && p->next )
{
    temp = p->data;
    p->data = p->next->data;
    p->next->data = temp;
    p = p->next->next;
}

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

This will be expensive if data is some large structure or class. So the best way should be switching pointers.

- ljiatu@umich.edu January 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse(Node n)
{
Node before = null;
Node after = null;
Node curr = null;

if(n->next == null || n == null)
return n;

curr = n;
Node n = n->next;

while(curr->next!= null)
{
after = curr->next->next;
curr->next->next = curr;
if(before!= null)
before->next = curr->next;
curr->next = after;
before=curr;
curr = curr->next;
}

return n;
}

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

#include <iostream>
#include <cassert>

template <typename T>
struct ListNode
{
  ListNode(T val):data_(val),next_(NULL),prev_(NULL)
  {
  }
  std::shared_ptr <ListNode> next_;
  std::shared_ptr <ListNode> prev_;
  T                          data_;
};

template <typename T>
class LinkedList 
{
public:
  void Print();
  void SwapNeighbours();
  void Insert(T val);
private:
  std::shared_ptr< ListNode<T> > head_;
  std::shared_ptr< ListNode<T> > tail_;

};

template <typename T>  
void LinkedList<T>::Insert(T val)
{
  if(head_ == NULL)
    {
      head_ = std::make_shared< ListNode<T> >(val);
      tail_ = head_;
    }
  else
    {
      assert(tail_!=NULL);
      tail_->next_ = std::make_shared< ListNode<T> >(val);
      tail_ = tail_->next_;
    }
}
template <typename T>
void swap(std::shared_ptr< ListNode<T> > l1, std::shared_ptr< ListNode<T> > l2)
{
  if(!l1 || !l2)
    {
      return;
    }
  auto val  = l1->data_;
  l1->data_ = l2->data_;
  l2->data_ = val; 
}
template <typename T>
void LinkedList<T>::SwapNeighbours()
{
  auto temp = head_;
  if(!temp)
    {
      return;
    }
  while(temp && temp->next_)
    {
      swap(temp,temp->next_);
      temp = temp->next_->next_;
    }
}

template <typename T>
void LinkedList<T>::Print()
{
  if(!head_)
    {
      return;
    }
  auto temp = head_;
  while(temp)
    {
      std::cout<<"["<<temp->data_<<"]->";
      temp = temp->next_;
    }
  std::cout<<"[NULL]"<<std::endl;
}

#include "LList.hpp"


int main ()
{
  LinkedList<int> llist;
  for(int i=0; i<10; i++)  
    {
      llist.Insert(i);
    }
  llist.SwapNeighbours();
  llist.Print();
}

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

void LinkList::swap()
{
	Node *node, *after, *before;
	int i=1;
	node = before = root;

	while(node && node->next)
	{		
		after = node->next;		

		node->next = after->next;

		after->next = node;

		if (i==1)
		{
			root = after;
		}
		else
		{
			before->next = after;
		}
		
		i++;

		before = node;
		node = node->next;		
	}
}

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

public static Node swapAlternateNodes(Node head)
	{
		if(head == null || head.next == null)
			return head;

		
		Node temp = head;
		head = head.next;

		Node current = temp;	
		Node prev = null;
		

		while(current != null && current.next != null)
		{
			Node nextCurrent = current.next.next;
			temp = current.next;	
			current.next = nextCurrent;
			temp.next = current;
			
			if(prev != null)
				prev.next = temp;
			prev = current;
			current = nextCurrent;
			
		}
		
		return head;
	}

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

I think the question has not been phrased properly. It should be "swap" adjacent elements of a linked list. That's probably what you meant.

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

#include <stdio.h>

struct link
{
	int data;
	struct link *next;
};
	
//struct link *global_ptr = NULL;	
	
void insert_node(struct link **ptr,int value)
{
	//global_ptr = *ptr;
	struct link *temp = (struct link*)malloc(sizeof(struct link));
	struct link *temp_ptr = NULL;
	temp->data = value;
	temp->next = NULL;
	if(*ptr == NULL)
	{
		*ptr = temp;
	}
	else
	{
		temp_ptr = (*ptr);
		while(temp_ptr->next != NULL)
		{
			temp_ptr = temp_ptr->next;
		}
		temp_ptr->next = temp;
	}
}

void display_list(struct link *disp_list)
{
	while(disp_list != NULL)
	{
		printf("The value in the list %d\n",disp_list->data);
		disp_list = disp_list->next;
	}
}

void display_swap_nodes(struct link **root_node)
{
	struct link *test_ptr=NULL;
	test_ptr = *root_node;
	struct link *temp_node1=NULL;
	struct link *temp_node2=NULL;
	while(test_ptr->next != NULL)
	{
		
		temp_node1 = test_ptr->next;
		if(temp_node1->next != NULL)
		{
			temp_node2 = temp_node1->next;
		}
		else
		{
			temp_node2 = temp_node1->next;
			temp_node2->next = NULL;
			break;
		}
		test_ptr = temp_node1->next;
		if(temp_node2->next != NULL)
		{
			test_ptr->next = temp_node2->next;
		}
		else
		{
			test_ptr->next = temp_node2;
		}
		test_ptr = temp_node2;
	}
}

int main()
{
	struct link *link_ptr = NULL;
	int count;
	int value;
	
	for(count =0 ; count<8; count++)
	{
		scanf("%d",&value);
		insert_node(&link_ptr,value);
	}
	display_swap_nodes(&link_ptr);
	display_list(link_ptr);
	return 0;
}

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

Quickest and easiest way to swap the alternate nodes will be to swap the data of alternate nodes, you can also swap nodes by using pointers, The only overhead swapping nodes using pointer will be to actually have one additional pointer. Below is the function which will swap data for alternate nodes.

static void swap_nodes( struct node **head )
{
struct node *even, *odd;

odd = *head;
if( odd->next != NULL )
	even = odd->next;

while( odd->next != NULL )
	{
	odd->data = odd->data ^ even->data;
	even->data = odd->data ^ even->data;
	odd->data = odd->data ^ even->data;

	odd = even->next;
	if( odd == NULL )
		break;

	even = odd->next;
	}
}

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

odd->data=odd->data^even->data
wht does ^ mean, how it works

- nvn January 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void rev2(node *start)
{
while(start->next && start )
{
start->data=start->data + (start->next)->data;
(start->next)->data=start->data -  (start->next)->data;
start->data=start->data - (start->next)->data;
start=start->next;
}
}

- chetan kumar b v January 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i 0 to n, i+2){
   a[i] = a[i]+a[i+1]
   delete(a[i+1])
}

Am I making sense here

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

deleting from specific location 0(1) I guess but every thing should not take more than 0(n)

- Xeb January 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can we do something like, take first 2 elements put in stack, then pop and link them. Then take next two elements put in stack and then pop them ! That should do

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

void swap (struct list **list1)
{
    struct list *cur, *tmp, *next;
    cur = *list1;
    if (cur && cur->next)
        *list1 = cur->next;

    //To make sure that we have at least two more elements to be swapped.
    while (cur && cur->next)
    {
        next = cur->next;
        tmp = next->next;
        next->next = cur;
        //We have to make 1->next as 4 in above example (figure).

  if(tmp)
    if (tmp->next)
        cur->next = tmp->next;
    else
        cur->next = tmp;    // take care of an add number of nodes
else
    cur->next = NULL; 
    }
    return;
}

Source : StackOverFlow

- shiva.kurapati January 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* swapAlt(Node* head)
{
	Node* temp = head;
	while (temp != NULL)
	{
		int n = temp->val;
		if( temp->next == NULL )
			break;
		temp->val = temp->next->val;
		temp->next->val = n;
		if( temp->next->next != NULL )
			temp = temp->next->next;	
	}
	return head;
}

- Anonymous February 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ansewr in C# - This answer does not use recursion which makes it better for large lists.
The first prev instance is created so we will not have to check for null every iteration.

private static Node Reverse2(Node head) {
      Node prev = new Node();
      Node curr = head;
      Node reverseHead = head.Next;

      while (curr != null && curr.Next != null) {
        Node nn = curr.Next.Next;
        Node newHead = curr.Next;

        newHead.Next = curr;
        curr.Next = nn;
        prev.Next = newHead;

        prev = curr;
        curr = nn;
      }

      return reverseHead;
    }

- ido.ran December 08, 2013 | 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