Microsoft Interview Question for Software Development Managers


Country: India
Interview Type: In-Person




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

This is because you are doing a using namespace std, which I believe makes the code use std::swap, rather than the swap you defined.

Try this:

#include<iostream>
#include <stdlib.h>
  
using std::cout;
using std::endl;
  
struct node
{
    int a;
    int b;
};
  
typedef struct node Node;
  
void swap(void *a,void *b)
{
    void *temp;
    temp=a;  
    a=b; 
    b=temp;
}
   
int main()
{
    Node *a1,*b1;
    a1=(Node*)malloc(sizeof(Node));
    b1=(Node*)malloc(sizeof(Node));
    a1->a=10;
    a1->b=20;
    b1->a=30;
    b1->b=40;
    cout<<a1->a<<" "<<a1->b<<endl;
    cout<<b1->a<<" "<<b1->b<<endl;
    cout << a1 << " " << b1 << endl;
    swap(a1,b1);
    cout << a1 << " " << b1 << endl;
    cout<<a1->a<<" "<<a1->b<<endl;
    cout<<b1->a<<" "<<b1->b;
}

- Loler March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with your ans.

- T000ny March 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

std::swap is defined as
template<typename _Tp>
inline void
swap(_Tp& __a, _Tp& __b);

The function matching the type (Node *) is called instead of generic type.
Try swap((void*)a1, (void*)b1);

- anonymous March 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How is it that it did not ask for #include<algorithm>

- Anonymous April 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

This code is not working you forgot to add header file for malloc. You will have to add
#include<malloc.h>
in the starting of code.
after that code is working fine. Complete running code at my terminal.

#include<iostream>
#include<malloc.h>
using namespace std;
struct node{
int a,b;
};
typedef struct node Node;
void swap(void *a,void *b){
void *temp;
temp=a;
a=b;
b=temp;
}
int main() {
Node *a1,*b1;
a1=(Node*)malloc(sizeof(Node));
b1=(Node*)malloc(sizeof(Node));
a1->a=10;
a1->b=20;
b1->a=30;
b1->b=40;
cout<<a1->a<<" "<<a1->b<<endl;
cout<<b1->a<<" "<<b1->b<<endl;
swap(a1,b1);
cout<<a1->a<<" "<<a1->b<<endl;
cout<<b1->a<<" "<<b1->b;
return 0;
}

- shravan40 March 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Your swap code doesn't work. Actually, the parameter passed through the function is passed by value, instead of refence, even if it's a pointer. As a result, what your code does is just to swap the two temporary pointers, which are destroyed when they're out of function scope.

- Blues Zhang April 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

flag == 1 is the right code
As seen in this output... the object P now holds the values 30 40
Before Pointer p1: 1020
------>Before Object P : 1020
-In written function
-Switch Values physically
After Pointer p1: 3040
------>After Object P : 3040

flag ==2 does nothing. As you said its just passed by value and thrown out
i left it in so i could see the outputs and compare
Before Pointer p1: 1020
------------>Before Object P : 1020
-In written function
-Does nothing in code
After Pointer p1: 1020
------------->After Object P : 1020

- spiffinme April 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think because of void pointer. No Type casting done.

- Prashanth April 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What worked for me
1.Explicitly calling swap using swap((void *)a,(void *)b);
2.Using memcpy to physically change contents of the objects.
Code below. Tested on c++ in codepad.com

//Swapping objects using void pointers
#include <iostream>
#include <stdlib.h>
using namespace std;

struct node
{
 int a,b;
};
typedef struct node Node;

void swap(void *a, void *b, int flag=1)
{
 cout<<"\n-In written function";
//CORRECT CODE
 if (flag==1)
 {
  //Physically replaces data while pointers point to same location
   cout<<"\n-Switch Values physically";
   void *temp = new Node;
   memcpy(temp,a,sizeof(Node));
   memcpy(a,b,sizeof(Node));
   memcpy(b,temp,sizeof(Node));
 }
//INCORRECT CODE
 else if (flag==2)
 {
   //Exchanges what pointers are pointing to
   cout<<"\n-Does nothing in code";
   void *temp;
   temp =a;
   a=b;
   b=temp;
 }
 else 
 {
   cout<<"\n-Wrong choice nothing done";
 }
}

int main()
{
  
  Node P,Q;
  Node *p1 = &P;
  Node *p2 = &Q;

  p1->a = 10; p1->b = 20;
  p2->a = 30; p2->b = 40;

  cout<<"\n Before Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
  cout<<"\n Before Objects P : "<<P.a<<P.b;
  cout<<"\n In built swap called. Not the one we wrote. Changes what the pointers point to. Uses template to accomplish. Wise.";
  swap(p1,p2); //Calls build in function from std. No String in output.
  cout<<"\n After Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
  cout<<"\n After Object P  : "<<P.a<<P.b;

  swap(p1,p2); //swaps back
  //Correct Run
  cout<<"\n\n Before Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
  cout<<  "\n Before Object P  : "<<P.a<<P.b;
  swap((void *)p1,(void *)p2,1); //Explicit call to our function
  cout<<"\n After Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
  cout<<"\n After Object P  : "<<P.a<<P.b;
  
  swap((void *)p1,(void *)p2,1); //swaps back
 //Incorrect Run
  cout<<"\n\n Before Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
  cout  <<"\n Before Object P  : "<<P.a<<P.b;
  swap((void *)p1,(void *)p2,2); //Explicit call to our function
  cout<<"\n After Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
  cout<<"\n After Object P  : "<<P.a<<P.b;

  return 0;
}

Output
//IN BUILT SWAP FUNCTION CALLED INSTEAD OF THE ONE ABOVE
Before Pointer p1: 1020
Before Objects P : 1020
In built swap called. Not the one we wrote. Changes what the pointers point to. Uses template to accomplish.Wise.
After Pointer p1: 3040
After Object P : 1020
//CORRECT RESULT
Before Pointer p1: 1020
Before Object P : 1020
-In written function
-Switch Values physically
After Pointer p1: 3040
After Object P : 3040

//SWAP BACK IGNORE
-In written function
-Switch Values physically

//INCORRECT RESULT
Before Pointer p1: 1020
Before Object P : 1020
-In written function
-Does nothing in code
After Pointer p1: 1020
After Object P : 1020

Do let me know if you have any comments.

- spiffinme April 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perfect. Interesting problem, it used std's swap and gives proper output. But actual intension of writing swap function is not being used.

Thanks for posting this problem.

- Manohar April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ std::swap function gets called, so values are swapped because std::swap function actually swaps these pointers.

- unknown August 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this is just the issue of right using pointers. Just change the relative code to:
void swap(void **a,void **b){
void *temp;
temp=*a;
*a=*b;
*b=temp;
}

And then use.
swap(&a1,&b1);

- jiefenghaspower October 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
1
of 1 vote

Did you even try running the code?

- Anonymous March 29, 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