Interesting C++ interview question

ArmchairAthlete

Diamond Member
Dec 3, 2002
3,763
0
0
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.
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
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?
 

ArmchairAthlete

Diamond Member
Dec 3, 2002
3,763
0
0
I suppose if when he said "any number" he did actually mean an int you could do this:

(num << 2) + num

If I'd heard "integer" I may have come up with it, too late now.


Not valid C++....
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Do casts count as steps? (what the heck is a step anyway?)

(((int) num) << 2) + ((int) num)

 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

Kyteland

Diamond Member
Dec 30, 2002
5,747
1
81
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.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
For that matter, why not use multiple threads? If you get two steps per thread, things could get quite interesting indeed.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: drebo
for(int i = 0; i < 5; i++)
a = a + a;

The unrolling of the loop excess two source code steps

 

Gibson486

Lifer
Aug 9, 2000
18,378
1
0
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.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
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
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
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"
 

Gibson486

Lifer
Aug 9, 2000
18,378
1
0
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




 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
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.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
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.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
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);
 

jonmullen

Platinum Member
Jun 17, 2002
2,517
0
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.
 

sao123

Lifer
May 27, 2002
12,650
203
106
couldnt you do a binary shift 2bits to the number (multiplication x4), then add itself again?



00010 = 2
01000 + 00010 = 01010 = 10?
 

DanceMan

Senior member
Jan 26, 2001
474
0
0
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....

 

BustaBust

Golden Member
Dec 21, 2001
1,425
2
81
(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)
 

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
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.




 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |