• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Interesting C++ interview question

ArmchairAthlete

Diamond Member
Actually, this probably isn't even C++ specific.

Take any input (float, int, etc) and multiply it by 5, without using the * operator in two steps.

My first thought before he added the "in two steps" bit was just add the input number together five times =). Not the right answer though.
 
I hate those kinds of interview questions. What exactly is that supposed to show the interviewer that you know? That you can write 5 in binary?
 
They think they're Google, and they're trying to find that elusive sort of coding genius who is so smart he is no longer paid to write actual code, but rather answers questions like that in blog posts all day, thus demonstrating how brilliant his employer was at interviewing.

I probably would have come up with two shift-lefts and an add as well.
 
How about recursion? How many "steps" does that count as?

template <typename Type>
multiply(Type number, uint step)
{
return (step>0?number+multiply(number, step-1):0);
}

Did they specifically say that it had to be any numeric type, or did you add that assumption yourself? That question usually only refers to using integer types.
 
For that matter, why not use multiple threads? If you get two steps per thread, things could get quite interesting indeed.
 
Originally posted by: Gibson486
Originally posted by: Common Courtesy
Originally posted by: drebo
for(int i = 0; i < 5; i++)
a = a + a;

The unrolling of the loop excess two source code steps

I do not think that will even work anyways. For starters, you will always get an even number.

a = 3
i = 0; a = 6
i = 1; a = 9
i = 2; a = 12
i = 3; a = 15
i = 4; a = 18

You'd have to start the loop at 1 because you've already multiplied your number by 1 when it's by itself (a = 3 for example):

a = 3
i = 1; a = 6
i = 2; a = 9
i = 3; a = 12
i = 4; a = 15
 
I probably would have come up with two shift-lefts and an add as well.
Me too.

Silly trick questions like this are annoying enough to make me reconsider whether I'd want to work for them.

How about this? ( 😉 and :roll: )

function fivebyfive (const char* vname )
{
cout << "5" << vname ;
}

Then

fivebyfive( "x" ) ; // output = "5x"
 
Originally posted by: tfinch2
Originally posted by: Gibson486
Originally posted by: Common Courtesy
Originally posted by: drebo
for(int i = 0; i < 5; i++)
a = a + a;

The unrolling of the loop excess two source code steps

I do not think that will even work anyways. For starters, you will always get an even number.

a = 3
i = 0; a = 6
i = 1; a = 9
i = 2; a = 12
i = 3; a = 15
i = 4; a = 18

You'd have to start the loop at 1 because you've already multiplied your number by 1 when it's by itself (a = 3 for example):

a = 3
i = 1; a = 6
i = 2; a = 9
i = 3; a = 12
i = 4; a = 15

but it is a + a.....

a = 3
i = 1; a = 6...yes...
i = 2; a = 9.... a + a = 6 + 6 = 12

a= a+a = the same as 2*a




 
No, you guys are right.

My a = a + a was wrong.

To use a loop, it'd need to be:

for(int i = 1; i < 5; i++)
a = a + (a / i);

That way, assuming a = 3, you get:

i = 1; a = 6 (3 + (3 / 1) = 6)
i = 2; a = 9 (6 + (6 / 2) = 9)
i = 3; a = 12 (9 + (9 / 3) = 12)
i = 4; a = 15 (12 + (12 / 4) = 15)

loop ends.

Either way, 2 lines of code is two lines of code. Since you didn't specify whether it's two lines of assembly or compiled code, the unfurling of the loop doesn't factor in.

It's two lines of code which at the end yields 5a.
 
Originally posted by: Gibson486
Originally posted by: tfinch2
Originally posted by: Gibson486
Originally posted by: Common Courtesy
Originally posted by: drebo
for(int i = 0; i < 5; i++)
a = a + a;

The unrolling of the loop excess two source code steps

I do not think that will even work anyways. For starters, you will always get an even number.

a = 3
i = 0; a = 6
i = 1; a = 9
i = 2; a = 12
i = 3; a = 15
i = 4; a = 18

You'd have to start the loop at 1 because you've already multiplied your number by 1 when it's by itself (a = 3 for example):

a = 3
i = 1; a = 6
i = 2; a = 9
i = 3; a = 12
i = 4; a = 15

but it is a + a.....

a = 3
i = 1; a = 6...yes...
i = 2; a = 9.... a + a = 6 + 6 = 12

a= a+a = the same as 2*a

You're right, I overlooked that. Still a bad solution though. 😛
 
Originally posted by: smack Down
a = a / (1/5)

Done in two steps.

ooo, I like this one. Just realize that 1/5 will return 1 (or 0?) instead of 0.20. You might need to say a /= (1.0/5.0);
 

Originally posted by: smack Down
a = a / (1/5)

In C++ semantics this will/should throw a Floating point exception in that the parens () will cause the 1 and the 5 to be divided as integers which will yield .20 in decimal math but 0 in integer division so you will get a good old divide by zero exception.

Originally posted by: Cogman
a /= (1.0/5.0);

this should work in all numerical cases or really for all types which have the operator /= (double f) properly defined.

Great answer though...of course if it is a C++ job interview and they are asking questions like that...my first clarification needs to be if operator overloading is allowed and considered a step.
 
couldnt you do a binary shift 2bits to the number (multiplication x4), then add itself again?



00010 = 2
01000 + 00010 = 01010 = 10?
 
Originally posted by: sao123
couldnt you do a binary shift 2bits to the number (multiplication x4), then add itself again?



00010 = 2
01000 + 00010 = 01010 = 10?

Yea, but does any bitshift work properly with float or double? Don't remember it working like that....

 
(X^x) / X

Not quite in C, but you know what I'm saying.


Also, does it have to work for every number?

Example: 5^2 - 0 = 25 (5*5)
This would not work for 3^2 - 0 = 27 (9x)
This would though: 3^2 - 12=15 (5x)
 
I think the correct answer is "blow me".

I wouldn't, obviously, walk right out of an interview if they asked me a question like that, but I wouldn't be interested in the job anymore.

Any HR person or manager that thinks that cute isn't someone I'd want to work for.




 
Bitwise shifting will only work for integers. Most systems would complain if you even tried logical ops on floats, though it would be possible to get at the float bits themselves (NOT with a cast, with a union). Casting everything to int and shifting would 'work', modulo loss of precision.
 
Back
Top