Microsoft Interview Question


Country: -




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

whoever writes this kind of code should be fired on the spot! Why is this a good interview question?

- Anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this just shows you are not very familiar with C++ templates: believe me, I saw even more uglier constructs when dealing with STL style code..

Perhaps this concrete example is a bit "overengineered" but if you are a professional in C++, it should not pose any problem to you to solve it.

Maybe interviewer just wanted to see if what you skills really are
if you write "experienced C++ programmer" in your CV

The solution is as follows:

'Foo' is a class which takes a template parameter which is itself a template. Hence we can declare 'Param' type as follows:

template < template < class > class X, class Y >
class Param { };

main() {

    Foo< Param > tt;
    tt.foo();
}

and the code is running

- Anonymous October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

check this codepad.org/FHEj9jij
it runs well

.
On uncommenting the lines in main() : codepad.org/sUM5mkBS
it gives error.

How to do it correctly?

- monish.gupta1 October 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is one way of doing this ..

template < typename X1 >
struct Foo;


template < template < int, int > class X, int A, int B>
struct Foo< X<A, B> > {
void foo() {
printf("ok, %d, %d \n", A, B);
}};

template < int X, int Y >
class Param {
int x,y;
public:
Param(){x=X; y=Y;}
void printParam(){
cout<<x<<" "<<y<<"\n";
}
};

int main() {
Param<10, 20> p;
p.printParam();

Foo< Param<10, 20> > a;
a.foo();
return 0;
}

- P October 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@monish


template < template < int, int > class X>
struct Foo {
int foo() {
printf("ok\n");
return 0;
}};

template < int X, int Y >
class Param {
int x,y;
public:
Param(){x=X; y=Y;}
void printParam(){
cout<<x<<" "<<y<<"\n";
}
};

int main() {
Param<10, 20> p;
p.printParam();

Foo< Param> tt;
tt.foo();
return 0;
}

- MOZHI October 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the above code works out

- MOZHI October 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is more discussion somewhat related to this question.

stackoverflow.com/questions/7745405/accessing-x-and-y-in-template-class-a-like-in-templatetemplateint-x-int-y-cla

- Monish October 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- siva.sai.2020 May 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

template <template <class> class X, class Y>
class Param {
};

template <template <template<class> class, class> class Param>
class Bogus {
public:
int foo() {
printf("ok\n");
}
};

int main()
{
Bogus<Param> tt;
tt.foo();
}

- Anonymous January 01, 2012 | Flag Reply


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