# Microsoft Technical Interview Questions with Answers

**Microsoft Corporation** is a multinational American based software company which develops world class computing products. As measured on the basis of revenue, Microsoft Corporation is the world’s largest software making company. Microsoft is the market dominating company in the field of OS development and Office Suite. The career at Microsoft is the dream of millions of people and to help them, here is the Microsoft technical interview questions with answers that has been asked in the Microsoft Recruitment. But before this, lets see this introductory video of Microsoft Corporation.

**Interview Questions with Answers-**

Following are the Microsoft Technical Interview Questions with Answers-

**1. Given two strings find if they are anagrams or not.**

**eg.”tom marvolo riddle” and “i am lord voldemort”.**

**(The example added the other constraint that the white spaces do not matter.)**

**ANS ::**

int isAnagram(char *s1,char *s2)

{

char characterCounter[256]={0};

int i;

for(;*s1;s1++)

{

if(*s1==’ ‘) continue;

characterCounter[*s1]+=1;

}

for(;*s2;s2++)

{

if(*s2==’ ‘) continue;

characterCounter[*s2]-=1;

if( characterCounter[*s2] <0)

return 0; //false

}

for(i=0;i<256;i++)

if(characterCounter[0]!=0)

return 0; //false

return 1; // True, as both are anagram

}

**2. Write a program that reverses alternate elements in a given linked list input: a->b->c->d->e, output should be b->a->d->c->e.**

**ANS ::**

#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();

**3.You are given a binary search tree, and a value(data item), you need to tell the left most right cousin in as minimum time and as minimum space ?(need to minimize actual time complexity, he need minimum order of complexity as well as number of node access should be minimum)**

**ANS ::**

Just traverse till left most node,when you hit null,go to the root node of the current node and then go for its right child.

**4. You have given a linked list in which each node have three items, 1) data, 2) a next pointer and 3) a random pointer which is pointing to its successor in sorted order. Replicate it ? (Need to generate a new linked list in O(n) + O(n) complexity)**

**ANS ::**

loop1:

temp = smallNode = head;

while(temp != null)

{

if(temp->data < smallNode.data)

{smallNode = temp;}

newNode = getNewNode();

newNode->data = temp->data;

nextNode = temp->next;

temp->next = newNode;

newNode->next = nextNode;

temp = nextNode;

}

loop2:

temp = smallNode;

while(temp->random != null)

{

temp->next->random = temp->random->next;

temp = temp->random;

}

temp->next->random = null;

loop3:

replicateHead = head->next;

temp = head;

replicate = replicateHead;

while(temp != null)

{

temp->next = replicate->next;

if(temp->next != null)

{replicate->next = temp->next->next;}

else

{replicate->next = null;}

temp = temp->next;

replicate = replicate->next;

}

Complexity: O(3N), O(1) space.

**5.Given a 2D array of size m X n, containing either 1 or 0. As we traverse through, where ever we encounter 0, we need to convert the whole corresponding row and column to 0, where the original value may or may not be 0. Now devise an algorithm to solve the problem minimizing the time and space complexity.**

**ANS ::**

First record the number of 0’s in rows and columns by initializing rows[array.length] and columns[array[0].length]
//Now,

for(r=0; r<array.length; r++)

for(c=0; c<array[0].length; c++)

if(array[r][c] == 0)

columns[c] = 1;

rows[r] = 1;

//Next step;

for(int r=0; r<rows.length; r++)

for(int c=0; c<columns.length; c++)

if(rows[r] == 1 || columns[c] == 1)

array[r][c] = 0;

Time-complexity: Linear O(mn); two times sweep of the entire 2d array

Space-Complexity: Num of column + Num of rows – O(m+n).

**6. How would you reverse a doubly-linked list?**

**ANS ::**

Node * pCurrent = pHead, *pTemp;

while (pCurrent)

{

pTemp = pCurrent->pNext;

pCurrent->pNext = pCurrent->pPrev;

pCurrent->pPrev = temp;

pHead = pCurrent;

pCurrent = temp;

}

**7. Given the following prototype:**

**int compact(int * p, int size);**

**Write a function that will take a sorted array, possibly with duplicates and compact the array, returning the new length of the array. That is, if p points to an array containing: 1, 3, 7, 7, 8, 9, 9, 9, and 10 when the function returns, the contents of p should be: 1, 3, 7, 8, 9, and 10, with a length of 5 returned.**

**ANS ::**

int compact(int * p, int size)

{

int current, insert = 1;

for (current=1; current < size; current++)

if (p[current] != p[insert-1])

{

p[insert] = p[current];

current++;

insert++;

} else

current++;

}

**8. When is a switch statement better than multiple if statements?**

**ANS ::**

A switch statement is best to use in the case when one have more than two conditional expressions which are based on a single numeric type variable.

These are some of the Microsoft Technical Interview Questions with Answers. If you want some more questions with answers, you can download the pdf here Microsoft Placement Paper PDF

If you want to add some more in this post, then feel free to write for us in the comment section of the post below.

**Related Posts-**

Interview Questions for Siemens and Nokia Siemens

Reference-

The video in the post is taken from the Youtube.

### Tell us Your Queries, Suggestions and Feedback

« India bulls technical interview questions with answers: Lupin Placement Criteria »

## One Response to Microsoft Technical Interview Questions with Answers