interview question (easy or hard?)

K

kostas

Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;

if(fabs(tmp)<=tol)
break;

if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}

1. What is the result of the above approximate algorithm ?
a) value modulo 1000.
b) square root of value.
c) pi

2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415

3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.

4. Correct the previous bug.
----------------------------------------------------------------------------------------
What do you think is the difficulty level of the exercise
Any other comments?

regards
Kostas
 
A

Alf P. Steinbach

* kostas:
Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)

There is no language C/C++, just as there is no language Java/C++.

I came up with the following
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;

if(fabs(tmp)<=tol)
break;

if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}

1. What is the result of the above approximate algorithm ?
a) value modulo 1000.
b) square root of value.
c) pi

2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415

3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.

4. Correct the previous bug.

Any programmer should be able to answer. The test is of general
programming background, not of C++ proficiency or CAD/CAM knowledge.

Any other comments?

Why don't you use "double"?

Also, "while(1)" may emit a warning with some compilers; for the purpose
of clean compiles, I prefer "for(;;)".

Also, what's the point of the "result" variable: is the company's coding
guideline to never return except at the end of a function (that might
indicate an unholy mix of C and C++, as does the "C/C++")?


Cheers,

- Alf
 
E

Erik Wikström

Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;

if(fabs(tmp)<=tol)
break;

if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}

1. What is the result of the above approximate algorithm ?
a) value modulo 1000.
b) square root of value.
c) pi

2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415

3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.

4. Correct the previous bug.

I guess it depends on what you want to test. The above will test how
good the applicant is at floating point math and performing calculations
in his head, if that is what you want then it is an OK question.
 
K

Kai-Uwe Bux

kostas said:
Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;

if(fabs(tmp)<=tol)
break;

if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}

1. What is the result of the above approximate algorithm ?
a) value modulo 1000.
b) square root of value.
c) pi

2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415

3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.

4. Correct the previous bug.

It's not hard.

Note that it is not a language question.

Any other comments?

1) I would write that loop a little different:

float fun(float value)
{
float lower = 0;
float upper = value;
float relative_error = value/1000.;
while ( true ) {
float mean = (lower + upper )/2.;
float tmp = mean * mean - value;

if( std::abs(tmp) <= relative_error ) {
return ( mean );
}

if(tmp>0.) {
upper = mean;
}else {
lower = mean;
}
}
}


2) You could also ask about the remaining bug of the function not
terminating for negative values. It should assert something, throw and
exception or return something usefull; but it should not go into an
infinite loop.


Best

Kai-Uwe Bux
 
S

Sohail Somani

Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
[snip]

Hi,

The points the others are making are good. It isn't really a language
question. More a "can you read uncommented code" question. Which would
scare me the hell away :)

I didn't read the code too closely (only first two lines of the loop),
but it seems to be a find-the-square-root question. I would recommend
that the question look something like this:

This is an algorithm to find the square root. It uses bisection(?).

For which cases does the loop not terminate? (no multiple choice)
How would you make this algorithm reusable to more than just finding
square roots?

I've used a variant of this question with a requirement on code I can
execute and it has done wonders for weeding people out. The second
question with its requirements on running code usually does it because
either the code is really really bad, or they give up.

Good question to ask, but it can show you so much more about the
candidate than what you will learn with your original set of questions.
 
A

Andre Kostur

Hi
I was asked to propose an interview question for C/C++ programmers

There is no language named "C/C++". Let's assume C++, since that's the
newsgroup you're in.
(CAD/CAE software)
I came up with the following
---------------------------------------------------------------------- -
----------------- float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;

if(fabs(tmp)<=tol)
break;

if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}

1. What is the result of the above approximate algorithm ?
a) value modulo 1000.

If that's what this is supposed to do, why isn't it (or why even bother
with the function, but just call the standard library function
directly):

float fun(float value) { return std::fmod(value, 1000.); };
b) square root of value.

Or:

float fun(float value) { return std::sqrt(value); };

Or:

float fun(float value) { return M_PI; };
2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415

B.
3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.
B.


4. Correct the previous bug.

See answers to #1.
---------------------------------------------------------------------- -
----------------- What do you think is the difficulty level of the
exercise Any other comments?

What does this prove about C++ knowledge? Other than the person wishes
to reinvent the wheel. This only seems to test if you recognize a
certain numerical analysis algorithm.
 
C

Carla Fong

kostas said:
Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
float tol = value/1000.;
float result,tmp;
while(1) {
result = (f2+f1)/2.;
tmp = result*result-value;

if(fabs(tmp)<=tol)
break;

if(tmp>0.) {
f2 = result;
}else {
f1 = result;
}
}
return result;
}

1. What is the result of the above approximate algorithm ?
a) value modulo 1000.
b) square root of value.
c) pi

2. Which of the values below is closer to the return value of
fun(9.) ?
a) 3.
b) 3.0015
c) 3.1415

3. For which of the values below the while loop does not end ?
a) 0.
b) 0.5
c) 1.

4. Correct the previous bug.
----------------------------------------------------------------------------------------
What do you think is the difficulty level of the exercise
Any other comments?

regards
Kostas


I think the question is irrelevant if someone is hiring me as a
software/firmware engineer.

If I'm supposed to port code from Platform A to Platform B and make it
work on the new hardware then this quiz might be useful, but if the
legacy code is completely uncommented like this then I'll go looking for
a shop that knows what they're doing.

I've spun my wheels waaaay too many times trying to figure out some
hotshot's spaghetti code to ever want to do it again without novocain.
IMHO it's often easier and faster to just re-write the WDT than cobble
together existing buggy code and try to get inside the mind of the
original programmer.

Give your applicant a 'real world' problem related to your project and
see what the code looks like. And remember, it rarely works perfectly
the first time.

Carla
The fact that no one understands you does NOT make you an artist.
 
J

Jack Klein

Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following

In the other comments category:

Superfluous '.' on initializer.
float tol = value/1000.;

Superfluous and possible dangerous '.' on "1000.". If you had left
off the decimal point, the constant would be of type int and would be
directly promoted to float. With the decimal point, you have a
constant of type double, causing value to be promoted to double, the
division done to produce a double result, that double result then
being converted back to float by the assignment to "tol".

A division by a power of ten, which does not have an exact
representation in most binary floating point formats, can produce
different results when performed in double versus when performed in
float. The final float could actually have a different value when the
division is performed on doubles and converted to a float.
float result,tmp;
while(1) {
result = (f2+f1)/2.;

Ditto again on the "2.", although since 1/2 is exactly representable
in most binary floating point formats the difference is likely to be
negligible. Again, though, you are performing a float addition of f1
and f2, converting the result to double to divide by (double)2.0, then
converting the double result back to float for the assignment.
tmp = result*result-value;

if(fabs(tmp)<=tol)
break;

if(tmp>0.) {

One more time, "0." forces the float value "tmp" to be converted to
double, although a compiler might optimize this one away, converting
it to: "if (!tmp)"
f2 = result;
}else {
f1 = result;
}
}
return result;
}

CAD and CAE programs should probably never use float, as the lowered
precision compared to double is often intolerable in complex
calculations. A good experienced engineering programmer should
actually complain about the extra execution time and possible accuracy
implications of the forced conversions caused by mixing double
literals with float values.

Using "1000." is not a good idea when mixing with floating point types
other than double. You should have used 1000F, or omitted the decimal
point to produce an integer literal, allowing the compiler to handle
it automatically and properly.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
K

kostas

* kostas:


There is no language C/C++, just as there is no language Java/C++.

But behind a lot of software projects there is plenty of C/C++ code
I am afraid :)

Thank you for your comments
Kostas
 
K

kostas

If that's what this is supposed to do, why isn't it (or why even bother
with the function, but just call the standard library function
directly):

float fun(float value) { return std::fmod(value, 1000.); };


Or:

float fun(float value) { return std::sqrt(value); };


Or:

float fun(float value) { return M_PI; };

I admit that the first question is a little problematic. Initially i
hadn't included it. My colleagues noted that it is much more difficult
without it and i had to "drive" the interviewee someway.

What does this prove about C++ knowledge? Other than the person wishes
to reinvent the wheel. This only seems to test if you recognize a
certain numerical analysis algorithm.

The target is not only experienced programmers.

Why did you give the answers :)

Thank you for your comments.
Kostas
 
A

Alf P. Steinbach

* kostas:
But behind a lot of software projects there is plenty of C/C++ code
I am afraid :)

There's also a lot of Java/C++ code, C++ used in Java-idiomatic ways.

The point is that it's generally an ungood idea to blindly transfer
conventions and idioms and guidelines that make sense in language X, to
language Y.

Tranferring C and/or Java coding guidelines to C++ is a recipe for
disaster. For example, adopting a C guideline "return only at the end
of a function" (not that it necessarily makes sense even in C) in C++,
may tend to hide exception safety problems, and certainly puts the focus
on precisely the opposite of what's needed to achieve exception safety.
For another example, adopting the Java guideline of all uppercase for
constants (ironically originating with early C) to C++, may result in
macro name collisions, and anyway in less readable and maintainable
source code. But then, in shops where such is established practice, it
may be difficult to spot the alleged "disasters", or argue that such
should be avoided. Because it's all a continuous disaster, it's normal.

Thank you for your comments

You're welcome.

Cheers, & hth.,

- Alf
 
J

James Kanze

[snip]
The points the others are making are good. It isn't really a language
question. More a "can you read uncommented code" question. Which would
scare me the hell away :)

More "can you read poorly written uncommented code".

Of course, I'm sceptical about "what does this code do" type
questions to begin with. I tend to use the opposite approach:
first define what the code should do, and then implement it.
 
K

kostas

Hi
I was asked to propose an interview question for C/C++ programmers
(CAD/CAE software)
I came up with the following
[snip]
The points the others are making are good. It isn't really a language
question. More a "can you read uncommented code" question. Which would
scare me the hell away :)

More "can you read poorly written uncommented code".

Unfortunately, in the company I work for, we hadn't yet the time to
convert all of our code to "perfectly written documented code". So we
are in the need of such poor programmers that can cope at least with
the code of this exercise.
 
S

Sohail Somani

Unfortunately, in the company I work for, we hadn't yet the time to
convert all of our code to "perfectly written documented code". So we
are in the need of such poor programmers that can cope at least with the
code of this exercise.

You should be prepared that people who can do so will be pretty damn good
and pay them appropriately.
 
D

David Harmon

On Fri, 23 Nov 2007 06:10:44 -0800 (PST) in comp.lang.c++, James
Kanze said:
More "can you read poorly written uncommented code".

A prime requirement in most of the programming jobs in existence,
I think.
 
D

Duane Hebert

David Harmon said:
On Fri, 23 Nov 2007 06:10:44 -0800 (PST) in comp.lang.c++, James


A prime requirement in most of the programming jobs in existence,
I think.

Unfortunate when writing clear, self documenting code should
be the prime requirement.

I can't disagree with you though.
 
P

pauldepstein

The point is that it's generally an ungood idea to blindly transfer
conventions and idioms and guidelines that make sense in language X, to
language Y.
....

Very much a tangential point but I'd be interested to see the reason
you use the word "ungood" instead of the standard "bad". From your
posting history, I suspect the reason might be interesting and
enlightening.

Paul Epstein
 
A

Alf P. Steinbach

* (e-mail address removed):
...

Very much a tangential point but I'd be interested to see the reason
you use the word "ungood" instead of the standard "bad". From your
posting history, I suspect the reason might be interesting and
enlightening.

You're giving me too much credit.

In one sense "ungood" is a weaker form than "bad".

"ungood", interpreted literally, just says "not good".

But mainly the reason is old habit, a half technical and political
tongue-in-cheek comment. The word seems to have originated with George
Orwell's "1984" novel, in which the dictators had imposed some degree of
mind control and limitation of free expression by requiring the use of
Newspeak, an artifically limited language based on English. The "++" in
"C++" derives in part from the "double-plus" qualifier of Newspeak. And
since "ungood" is another Newspeak term, it fits well in a C++ context.

Using a term introduced by (fictional) dictators in order to limit free
expression and thinking, says in essence that I choose thinking and
speaking out over blindly following rules and keeping quiet, and that I
encourage others to also do so, a basic belief in communication.

Cheers,

- Alf


PS: In Norwegian, the translation of George Orwell's "All animals are
equal, but some animals are more equal than others", when discarding the
"animal" bit, becomes "Alle er like, men noen er likere enn andre",
where it directly states what Orwell just had to (let his fat pigs)
imply: "likere" means "better" in Norwegian! Food for thought... DS.
 
S

Sohail Somani

Very much a tangential point but I'd be interested to see the reason you
use the word "ungood" instead of the standard "bad". From your posting
history, I suspect the reason might be interesting and enlightening.

Doubleplusungood! Sorry, I *love* that term.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top