# Microsoft Interview | Set 6

I am a guy from CSE dept, IIT-BHU and currently in 4th year.  I am placed in microsoft now and all the credit goes to geeksforgeeks for sharpening and improving my coding skills. Thanks a lot guys for such a wonderful site.  Below is my interview experience, if its of any help to my fellow job seekers.

Written Round 1 :
50 multiple choice questions in 60 mins time. Had 2 sections – Aptitude and Programming Ability.

Written Round 2 : 3 coding questions in 1 hour.

1. Swap every consecutive odd and even positioned bit in a number.
Ex:- 10101011010101 = 01010111101010

2. Given a binary search tree . Convert it into a doubly linked list in place (no extra space) such that prev points to left child and next points to right child.

3. Given a linked list that contains 0,1 and 2 . Sort this linked such that it contains 0s first, then 1s and then 2s in O(n) time. Ex:- 2->2->1->0->0->2->1->1->0 = 0->0->0->1->1->1-2->2->2

PI round 1 (Technical):

1. About urself, most challenging project of all you have done and about ur internship.

2. Remove from string s1, all the characters that are present in string s2.
S1=”abcde” s2=”xyazbx” Ans. S1= ”cde”

3. Reverse alternate k nodes in an linked list.
Ex:- 1->2->3->4->5->6->7->8 if k=2; then return 2->1->3->4->6->5->7->8

4. Always ask some questions to the interviewer at the end. That’s a good gesture.

5. Write test cases for the programs above and see if ur program covers all the test cases or not.

PI round 2 (Program manager)

1. Design a tic-tac-toe game for multiplayer on a smartphone.

2. Design a vending machine for blind and deaf.

PI round 3 (Technical)

1. Add numbers from 1 to n^2 into a n X n matrix spirally.

2. Remove duplicates from a string in place. Ex-microsoft -> microsft

3. Some question on recursive functions using memoization technique for optimization.

PI round 4 (Testing)

1. Given a func :: int *strcmp(char *s1,char *s2);
Write test cases to check functionality and security issues and even automate the
generation of test cases.

2. Given a func :: int typeOfTriangle(int side1,int side2,int side3); (func gives a number for the type of triangle)
Write test cases to check functionality and security issues and even automate the generation of test cases.

3. WAP to reverse words in a sentence.
I am a good boy -> boy good a am i

4. WAP to get the next higher palindrome of a given number.
123 -> 131 1232 -> 1331
PS – Functional issues are test cases that check the functionality of the program and Security issues are the ones where the program may crash.

PI round 5 (Technical)
1. WAP to check if a binary tree is a BST or not.

2. WAP to find Least Common Ancestor of two nodes in a BST.

3. Reverse every two nodes in a linked iteratively and recursively.

PS- Whenever u write a program be sure to cover all the corner cases and write it in a proper indented way. As interviewers ask to write testcases for the program u have written and then check if ur program covers all the test cases.

All the best fellas !!!

Many Many congratulations to Pritam Kumar Das. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# Company Wise Coding Practice    Topic Wise Coding Practice

• Brijesh

Swap odd and even bits on 32-bit integer

swap (int num)
{
return ( (num & 0xAAAAAAAA) >> 1 | (num & 0x55555555) << 1));
}

• Gaurav Ramesh

thanks for this. nice logic.

• Saket

P1 round 3 ques 2
Remove duplicate from String.Let me know if it fails in any scenario.

static String removeDup(String str){
StringBuffer sbuff = new StringBuffer(str);
int len = str.length();
int n = 0 ; int i=0;
while (i<len ) {
if((n & (1 << sbuff.charAt(i)-‘A’)) >= 1){
sbuff.deleteCharAt(i);
len=len-1;
}
else {
n = n |(1 << sbuff.charAt(i)-‘A’);
++i;
}
}
return sbuff.toString();
}

• Saket

Swap odd and even bits. Please let me know if it fails in any scenario.

static int swap_odd_even_bits(int n){
int odd =1; int even = 1 << 1; int n1=0;
while(odd <= n || even <= n){
if ((n & odd) == 0 )
n1= n & ~even;
else
n1 = n | even;
if((n & even) == 0)
n = n1 & ~odd;
else
n = n1 | odd;

odd = (odd << 1)<<1;
even= (even << 1)<<1;
}
return n;
}

• Saket

Written Round 2 Ques no. 3

public void sort_0_1_2(){
sort_0_1_2(root);
}

private void sort_0_1_2(Node node){
Node one_start = null;
Node one_last = null; Node zero_start = null;
Node zero_last = null; Node two_start = null;
Node two_last = null;

while(node != null) {

if(node.data == 2 && two_start == null)
{ two_start = node; two_last = node; node =node.next; two_last.next=null;}
else if(node.data == 2 && two_last != null) {
Node temp = node;
node = node.next;
two_last.next = temp;
two_last = two_last.next;
two_last.next = null;
}
else if(node.data == 0 && zero_start == null)
{ zero_start = node; zero_last = node; node =node.next; zero_last.next=null;}
else if(node.data == 0 && zero_last != null) {
Node temp = node;
node = node.next;
zero_last.next = temp;
zero_last = zero_last.next;
zero_last.next = null;
}
else if(node.data == 1 && one_start == null)
{ one_start = node; one_last = node; node =node.next; one_last.next =null;}
else if(node.data == 1 && one_last != null) {
Node temp = node;
node = node.next;
one_last.next = temp;
one_last = one_last.next;
one_last.next = null;
}

}

if(zero_last != null)
root=zero_start;
else if(one_last != null)
root = one_start;
else
root = two_start;
if(zero_last != null && one_start != null)
zero_last.next = one_start;
else
zero_last.next = two_start;
if(one_last != null)
one_last.next = two_start;
}

• Saket

public void sort_0_1_2(){
sort_0_1_2(root);
}

private void sort_0_1_2(Node node){
Node one_start = null;
Node one_last = null; Node zero_start = null;
Node zero_last = null; Node two_start = null;
Node two_last = null;

while(node != null) {

if(node.data == 2 && two_start == null)
{ two_start = node; two_last = node; node =node.next;
two_last.next=null;}
else if(node.data == 2 && two_last != null) {
Node temp = node;
node = node.next;
two_last.next = temp;
two_last = two_last.next;
two_last.next = null;
}
else if(node.data == 0 && zero_start == null)
{ zero_start = node; zero_last = node; node =node.next;
zero_last.next=null;}
else if(node.data == 0 && zero_last != null) {
Node temp = node;
node = node.next;
zero_last.next = temp;
zero_last = zero_last.next;
zero_last.next = null;
}
else if(node.data == 1 && one_start == null)
{ one_start = node; one_last = node; node =node.next;
one_last.next =null;}
else if(node.data == 1 && one_last != null) {
Node temp = node;
node = node.next;
one_last.next = temp;
one_last = one_last.next;
one_last.next = null;
}

}

if(zero_last != null)
root=zero_start;
else if(one_last != null)
root = one_start;
else
root = two_start;
if(zero_last != null && one_start != null)
zero_last.next = one_start;
else
zero_last.next = two_start;
if(one_last != null)
one_last.next = two_start;
}

• VikasG

Can someone clearly explain this problem that was asked in Written Round 2 Problem 2:

Given a binary search tree . Convert it into a doubly linked list in place (no extra space) such that prev points to left child and next points to right child.

• Manish Kumar
``` ```
wriiten round-2 Ques-3

struct node p0;
struct node* h0 = &p0;
p0.next = NULL;
struct node p1;
struct node* h1 = &p1;
p1.next = NULL;
struct node p2;
struct node* h2 = &p2;
p2.next = NULL;

return NULL;
}
printf("\nrun");
while(cur!=NULL){
if(cur->data==0){
h0->next=cur;
h0=h0->next;
}
if(cur->data==1){
h1->next=cur;
h1=h1->next;
}
if(cur->data==2){
h2->next=cur;
h2=h2->next;
}
cur=cur->next;
}
if(h0!=NULL){
h0->next=NULL;
}//else {
// h0=p1.next;
//}
if(h1!=NULL){
h1->next=NULL;
h0->next=p1.next;
}//else {
// h1=p2.next;
//}
if(h2!=NULL){
h2->next=NULL;
h1->next=p2.next;
}
if((p0.next)!=NULL && (p1.next!=NULL) && (p2.next!=NULL)){
h0->next=p1.next;
h1->next=p2.next;
}
return (p0.next);

}
``` ```
• Amit

round 3-ques1
//2) Add numbers from 1 to n^2 into a n X n matrix spirally.
#include
#include
#define N 10
void myfunc(int a[N][N],int n)
{
int y,k=1,i=0,j=n-1,x;
while(k<=n*n)
{
x=i;
while(x<=j)
a[i][x++]=k++;
y=i+1;
while(y=i)
a[j][x–]=k++;
y=j-1;
while(y>i)
a[y–][i]=k++;
i++;
j–;
}
}
int main()
{
int a[N][N],i,j;
myfunc(a,N);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
getch();
return 0;
}

• Yang Wei

the correct Myfunc should be

``` ```
void myfunc(int a[N][N],int n)
{
int y,k=1,i=0,j=n-1,x;
while(k<=n*n)
{
y=i;
while(y<=j)
a[i][y++]=k++;
x=i+1;
while(x<=j)
a[x++][j]=k++;
y=j-1;
while(y>=i) {
a[j][y--]=k++;
}
x=j-1;
while(x>i)
a[x--][i]=k++;
i++;
j--;
}
}
``` ```
• Sachin

• Andy

Can you write these question in 1 hour Personel interview?

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```
• ANURAG

@pritam kumar das.. can u please tell how to prepare for the interview?should i focus on OS & DBMS,or any other subject,or just coding?

• dreamer

Thanks a ton for the questions.
Can you tell us how to prepare for the aptittude and programming baility questins?
Also, how did you write the codes in the written round?
Any comments/explanations? or just the indented code ?

``` ```
/* Paste your code here (You may delete these lines if not writing code) */
``` ```