The Frog in the Well and the Assignment Operator
One or two years back, I attended an interview for a very popular multinational company. I was happy with the job I had and attended the walk-in interview just to see what the industry is looking for. Those days, I was quite comfortable with C++ programming and considered myself really good at it.
I hadn't applied for the interview. Some HR firm had tracked me down via someone else's referral and had asked me to attend the walk-in. So, I walked in without even a resume. The guys at the interview were quite surprised but were kind enough to let me write down my resume summary on a sheet of paper! Anyway, I had two rounds of technical interviews, and I did pretty well on those.
The first interviewer asked me how I rated myself on C++ skills. I confidentially said "about 8". He was shocked and said "I guess even Stroustrup considers himself a 7 or so". All I could manage to say to that was "perhaps he knows a lot that he realises that he has much more left to learn". Anyway, the first interview went well, and I was taken to the next interviewer.
Here's an excerpt from the second one:
Interviewer: Can you write code to swap two numbers without using a temporary variable?
Me: What kind of numbers?
Interviewer: ??
Me: Integers or floating point?
Interviewer: Err.. integers.. (clearly wondering how that makes any difference)
Me: In that case..
a = a^b;
b = a^b;
a = a^b;
Interviewer: (surprised) How does this work?
Me: The XOR does this and that and... But this wont work for floats
Interviewer: Oh..kay.. so, what if I need floats too?
Me: Then there's no perfect method to do it. You could do something like:
a = a+b;
b = a-b;
a = a-b;
Me: But it fails on overflows and the like
Interviewer: (happy and relieved to see the solution he wanted) hmm.. that's okay. This is good enough
I had clearly bowled him over and then another guy glances over and tells him -"when you're through I'd like to interview him too".
It was this third interview that opened my eyes. The guy asked only 3 or 4 questions and I couldn't answer any of them satisfactorily. These were really interesting clever questions. For example: "how do you write a smart pointer class that'll call the derived class destructor even if the base class destructor is not virtual?"
i.e.
smart_ptr<base> p = new derived;
When 'p' goes out of scope,
~derived()
should be called even if ~base()
is not virtual
.I couldn't solve this one and the couple of the other simpler ones he asked. My pride was shattered, and I sat there defeated.
Anyway, they did like me and took me to the HR interview. I shocked him by saying that I expected a salary of Rs.8.5L p.a. I'm pretty sure that's why they didn't call me afterwards ;-)
Anyways, here's an interesting article that I had read (and downloaded) a long time back - The Anatomy of the Assignment Operator. It's a good read for all C++ enthusiasts. If you haven't read it yet, I strongly recommend you do.
The author has posted a follow-up article The Assignment Operator Revisited. In this article, he says:
"If you think you know it all in the C++ world, it must mean you’re not talking to your colleagues very much. If I had any pretensions to knowing it all when I wrote my assignment-operator article ("The Anatomy of the Assignment Operator," C++ Report, Nov/Dec 1997), they didn’t last long afterwards."
I couldn't agree more!
ithil frog aaru?
ReplyDeletea)syam
b)syam
c)syam
d)all the above.
You don't have to be a rocket scientist to figure out the frog in this story. Wait.. but you ARE a rocket scientist!
ReplyDelete