473,287 Members | 3,240 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

interview question (easy or hard?)

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
Nov 22 '07 #1
12 4006
On 2007-11-22 21:50, kostas wrote:
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?
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.

--
Erik Wikström
Nov 22 '07 #2
kostas wrote:
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
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
Nov 23 '07 #3
On Thu, 22 Nov 2007 12:50:38 -0800, kostas wrote:
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.

--
Sohail Somani
http://uint32t.blogspot.com
Nov 23 '07 #4
kostas <sk******@gmail.comwrote in
news:e5**********************************@i12g2000 prf.googlegroups.com:
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); };
c) pi
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.
Nov 23 '07 #5


kostas wrote:
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.

Nov 23 '07 #6
On Thu, 22 Nov 2007 12:50:38 -0800 (PST), kostas <sk******@gmail.com>
wrote in comp.lang.c++:
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:
----------------------------------------------------------------------------------------
float fun(float value)
{
float f1 =0., f2 = value;
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
Nov 23 '07 #7
On Nov 22, 11:36 pm, "Alf P. Steinbach" <al...@start.nowrote:
* 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++.
But behind a lot of software projects there is plenty of C/C++ code
I am afraid :-)

Thank you for your comments
Kostas

Nov 23 '07 #8
On Nov 23, 6:13 am, Andre Kostur <nntps...@kostur.netwrote:
kostas <skola...@gmail.comwrote innews:e5**********************************@i12g20 00prf.googlegroups.com:
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); };
c) pi

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
Nov 23 '07 #9
On Nov 23, 3:21 am, Sohail Somani <soh...@taggedtype.netwrote:
On Thu, 22 Nov 2007 12:50:38 -0800, kostas wrote:
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".

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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 23 '07 #10
On Fri, 23 Nov 2007 06:10:44 -0800 (PST) in comp.lang.c++, James
Kanze <ja*********@gmail.comwrote,
>More "can you read poorly written uncommented code".
A prime requirement in most of the programming jobs in existence,
I think.
Nov 24 '07 #11
On Nov 23, 5:56 pm, "Alf P. Steinbach" <al...@start.nowrote:
....
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
Nov 25 '07 #12
On Sat, 24 Nov 2007 19:25:40 -0800, pauldepstein wrote:
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.

--
Sohail Somani
http://uint32t.blogspot.com
Nov 25 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
0
by: Tony Lavinio | last post by:
Dear Stylus Studio Friends, The new year is scarcely one month old, but we already have lots to report! For starters, there's Stylus Studio 6 Release 2. The latest release of Stylus Studio...
27
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
13
by: yuvalif | last post by:
Hi All, The company I work for is requiting a C++ developer to my team. I prepared the following "test" and I would like to hear some comments about it: is it too hard/easy, if its too specific,...
27
by: dis_is_eagle | last post by:
hi....i am preparing for interview...where can i get helpful c interview questions and tips...also,can anybody provide me with a soft copy of the c-faq book...thanx and regards.. eric
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
14
by: shamirza | last post by:
Question Do ActiveX DLLs made in VB still need the VB runtimes on the machine? ________________________________________ Answer In a word, Yes. Visual Basic does not support what is known...
21
by: Noah Roberts | last post by:
How would you answer this question? "Describe the structure of a class." I was stumped, quite frankly.
22
by: Nicholls.Mark | last post by:
I want to set some simple coding tests for an interview....Joel recommends (for C programmers).... 1. Reverse a string in place 2. Reverse a linked list 3. Count all the bits that are on in a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.