Monday, April 28, 2014

Answering Interview Questions (Part -2) – Write a Program to swap two Integer variables.

This is the second post of the series ‘Answering Interview Questions’. The task is to write five different programs (or let’s say functions) to swap two integers using C-programming. Then you have to arrange those functions in ascending order of time taken in swapping those integer variables. I know 7+ ways of doing this. However, most students come up with only 3 ways. Very rarely a single student comes up with 4 ways of doing this. However, no student so far (to whom I have asked this question) across the country has been able to come up with all 5 functions all by himself. So usually I help them to get there.

However, the task is NOT over even if you have come up with 5 functions. You’re supposed to arrange them in ascending order w.r.t. execution too.

I have asked these questions to students in groups - to both CSE and ECE students.  I asked this question in MNNIT Allahabd, NIT Trichy, MANIT Bhopal and YMCA Faridabad - none of the students gave me the correct answer. Even in my own college (BIT Mesra) - 5 students gave me 5 different arrangements, all of them wrong!

You know, when you have 5 functions so you can have so many permutations and combinations.

NOTE: Some of the solutions mentioned below may need some tweaking for boundary cases. Most of my blog-articles related to Technical Interview Questions will be like that. This is to ensure that students do NOT mug up the solutions, rather, they discuss them and learn. Also, not everyone can learn everything at the same time. Depending on my mood, I may write a continuation of this post someday. But for now, you will have to think about the boundary cases where some of the methods mentioned below may fail. And I have knowingly, not mentioned, the best and the coolest solutions that I know!

So let me write down seven solutions for you:

   1.  temp = a;

2   2.   *temp = *a;

     3.  a= a+b;

4   4.   a= a*b; 

5. a= a^b;

     6. gcd = GCD(a,b); // also known as HCF
   lcm = LCM(a,b);
   a = gcd*lcm/b;

     7.  push(a);

I know 3 more solutions as on date, however, I will not write them for you :P  

This is to see if students really figure them out by themselves. I know many will read this blog-article before interviews, so I would like to see if they can come up with any other solution. Also, even if they do read this post, it will show that they really worked hard to learn to crack the interview, which will also count as a positive. However, if someone thinks that he can outsmart interviewers by mugging these things, then s/he will be trapped in a follow up question!

Now, the task is to arrange these seven in ascending order of time taken (during execution). I will just provide the order without giving the reason for it. This is to protect the thinkers from the muggers. I don’t want people to mug it up. I also don’t want to spoon feed you. You will have to learn.

So the order is:
2 1 5 3 4 7 6 (the position of 7 & 6 may change depending on their respective implementations).

No comments:

Post a Comment