Serving the Quantitative Finance Community

 
User avatar
stoneyl
Topic Author
Posts: 0
Joined: December 19th, 2007, 3:11 pm

a tricky interview c++ question

September 4th, 2009, 12:47 pm

got an very tricky c++ interview question here/******************************int i = 1;void f(){ int i = 2; { int i = 3; //How to access the second i here????????????? }}is there any special local scope resolution operators we can use here???
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

a tricky interview c++ question

September 4th, 2009, 1:32 pm

No, there isn't.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

a tricky interview c++ question

September 4th, 2009, 2:19 pm

QuoteOriginally posted by: bojanNo, there isn't.Change the name of variable to j? OK, I know, silly. Or maybe an enum?
Last edited by Cuchulainn on September 3rd, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
crmorcom
Posts: 0
Joined: April 9th, 2008, 4:09 pm

a tricky interview c++ question

September 4th, 2009, 3:00 pm

You could take a pointer or reference to it in its own scope and then dereference that within the block.Who thinks up these questions? I think I'd fire anyone who ever wrote code where this matters!
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

a tricky interview c++ question

September 4th, 2009, 3:18 pm

Actually, we had this or something very similar on our standard question sheet that we set to interviewees at one of the places I worked.... It is a common sort of question -- the point is not to test general programming skill.Rather, if somebody comes in at tells you they have 15 years C++ experience, and they fall down on something like this, you know you maybe shouldn't trust everything they say....
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

a tricky interview c++ question

September 4th, 2009, 3:45 pm

QuoteWho thinks up these questions? Some genius. I think someone who has never programmed.
 
User avatar
crmorcom
Posts: 0
Joined: April 9th, 2008, 4:09 pm

a tricky interview c++ question

September 4th, 2009, 4:01 pm

QuoteOriginally posted by: bojanRather, if somebody comes in at tells you they have 15 years C++ experience, and they fall down on something like this, you know you maybe shouldn't trust everything they say....Understood, but one has to be a little careful with that: sometimes people who know too much try to be too clever and write really hideous code (which they think is "elegant"). Never trust anyone, ever, until you have actually seen the code they write, and whether they bother to use comments
 
User avatar
rmax
Posts: 374
Joined: December 8th, 2005, 9:31 am

a tricky interview c++ question

September 4th, 2009, 4:07 pm

QuoteOriginally posted by: bojanActually, we had this or something very similar on our standard question sheet that we set to interviewees at one of the places I worked.... It is a common sort of question -- the point is not to test general programming skill.Rather, if somebody comes in at tells you they have 15 years C++ experience, and they fall down on something like this, you know you maybe shouldn't trust everything they say....I remember interviewing someone who said that they had a lot of experience in programming VBA. I asked what Option Explicit did and they didn't know.... The interview was quite short.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

a tricky interview c++ question

September 5th, 2009, 11:48 am

QuoteOriginally posted by: rmaxQuoteOriginally posted by: bojanActually, we had this or something very similar on our standard question sheet that we set to interviewees at one of the places I worked.... It is a common sort of question -- the point is not to test general programming skill.Rather, if somebody comes in at tells you they have 15 years C++ experience, and they fall down on something like this, you know you maybe shouldn't trust everything they say....I remember interviewing someone who said that they had a lot of experience in programming VBA. I asked what Option Explicit did and they didn't know.... The interview was quite short.Just take the default Less compiler errors interviewee; i have worked for 10 years in VC++interviewer; interesting, let's take a simple class Point with (public) member data x and y, and a few constructorsinterviewee: member data, class, what's that?*&^%#@ Interviewee had C experience but was using the VC++ product
Last edited by Cuchulainn on September 4th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

a tricky interview c++ question

September 5th, 2009, 11:57 am

QuoteYou could take a pointer or reference to it in its own scope and then dereference that within the block.It works btw is is better to use 'new'?#include <iostream>using namespace std;void f(){ int i = 2; int* I = &i; { int i = 3; cout << i << endl; //How to access the second i here????????????? cout << *I << endl; }}int main(){ f(); return 0;}
Last edited by Cuchulainn on September 4th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
Fermion
Posts: 2
Joined: November 14th, 2002, 8:50 pm

a tricky interview c++ question

September 5th, 2009, 8:54 pm

[void f(){ int i = 2; int & I = i; { int i = 3; cout << i << endl; //How to access the second i here????????????? cout << I << endl; }}is a little simpler don't you think?int main(){ f(); return 0;}
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

a tricky interview c++ question

September 7th, 2009, 1:12 pm

QuoteOriginally posted by: Fermion[void f(){ int i = 2; int & I = i; { int i = 3; cout << i << endl; //How to access the second i here????????????? cout << I << endl; }}is a little simpler don't you think?int main(){ f(); return 0;}Only difference might be performance.
 
User avatar
AlexesDad
Posts: 11
Joined: May 29th, 2009, 4:10 pm

a tricky interview c++ question

September 7th, 2009, 4:37 pm

QuoteOriginally posted by: Fermion[void f(){ int i = 2; int & I = i; { int i = 3; cout << i << endl; //How to access the second i here????????????? cout << I << endl; }}is a little simpler don't you think?int main(){ f(); return 0;}It's simpler.It's neater.It's more in the spirit of C++.And there's NO performance hit.PS DON'T use pointers in C++ except in those VERY RARE occasions when you ABSOLUTELY have to
Last edited by AlexesDad on September 6th, 2009, 10:00 pm, edited 1 time in total.
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

a tricky interview c++ question

September 7th, 2009, 6:26 pm

For C++ and programming beginners, I would propose this as the "solution".....#include <iostream>void g(int j){ int i=3; // This is the way to access the original i... std::cout<<j<<std::endl;}void f(){ int i = 2; g(i);}int main(void){ f();}
 
User avatar
AlexesDad
Posts: 11
Joined: May 29th, 2009, 4:10 pm

a tricky interview c++ question

September 7th, 2009, 6:44 pm

I believe that's a worse solution.I prefer Fermion's, with a minor tweak :void f(){ int i = 2;{ int & I = i; int i = 3; cout << i << endl; //How to access the second i here????????????? cout << I << endl;}}int main(){ f(); return 0;} There's much less pollution of outer scopes.
Last edited by AlexesDad on September 6th, 2009, 10:00 pm, edited 1 time in total.