473,598 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)<=t ol)
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 4022
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)<=t ol)
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)<=t ol)
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******** *************** ***********@i12 g2000prf.google groups.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)<=t ol)
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)<=t ol)
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)<=t ol)
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.l earn.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.no wrote:
* 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...@kostu r.netwrote:
kostas <skola...@gmail .comwrote innews:e5****** *************** *************@i 12g2000prf.goog legroups.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...@taggedt ype.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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 23 '07 #10

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

Similar topics

54
17378
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 give interviewees seems to trip them up all the time, and I wonder if I'm being too much of a hardass... it seems easy enough to ME, but these guys, when I get them up to the whiteboard, seem to get really confused. The exercise is this:
0
2167
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 boasts literally hundreds of product enhancements, and is available now for free trial download. We're also proud to present our interview with Dr. Daniela Florescu, editor of the XQuery specification. Read this month's Stylus Scoop and learn about...
27
3024
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 internals? Q1 . What is the use of pointer to an array? Q2 . What is the use of array of pointers? Q3 . What is the use of pointer to function ? Q4 . How to print through serial port? What is Flow Control(Xon,Xoff) ?
13
2089
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, is it really testing ones knowledge in C++, etc. It is as following: (1) Explain the following code, what it does? What will be the output of "main"? (2) Write a copy constructor for class "Values".
27
2293
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
6943
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
14
4093
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 as "Static
21
438
by: Noah Roberts | last post by:
How would you answer this question? "Describe the structure of a class." I was stumped, quite frankly.
22
4283
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 byte 4. Binary search 5. Find the longest run in a string 6. atoi 7. itoa
0
7981
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6711
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3938
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.