440,581 Members | 2,023 Online Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

# interview question (easy or hard?)

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a kostas 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

 P: n/a 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

 P: n/a On Thu, 22 Nov 2007 12:50:38 -0800 (PST), kostas 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

 P: n/a On Nov 22, 11:36 pm, "Alf P. Steinbach"

 P: n/a On Nov 23, 6:13 am, Andre Kostur

 P: n/a On Nov 23, 3:21 am, Sohail Somani

 P: n/a On Fri, 23 Nov 2007 06:10:44 -0800 (PST) in comp.lang.c++, James Kanze 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

 P: n/a On Nov 23, 5:56 pm, "Alf P. Steinbach"

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion. 