473,405 Members | 2,287 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,405 software developers and data experts.

What type of questions can be asked in an technical interview of C

Frinds,

Hope everyone is doing fine.i feel pointers to be the most toughest
part in C. i have just completed learning pointers & arrays related
portions. I need to attend technical interview on C. wat type of
questions should be expected? Which part of C language do the staff
give more concern?
The interviewers have just mentioned that .. i will have interview on
C.
Also can anyone can help me with sites where i can go thru sample
programs in C?

Thanking you all...
Cherry
Nov 14 '05 #1
56 4251
Cherrish Vaidiyan <un***************@yahoo.com> scribbled the following:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest
part in C. i have just completed learning pointers & arrays related
portions. I need to attend technical interview on C. wat type of
questions should be expected? Which part of C language do the staff
give more concern?
The interviewers have just mentioned that .. i will have interview on
C.
Also can anyone can help me with sites where i can go thru sample
programs in C?


This is impossible to answer specifically, as it would require reading
the company's mind. But I think you're off to a good start with pointers
and arrays. Other topics that can come up are functions (calling and
parameter passing mechanisms), the type system, and preprocessor macros.
The most important rule I can think of is to learn the core language
first, and any possible OS-specific APIs second, if at all. Knowing
which flag to set to make the X widget turn purple is of no use if you
don't know how function calls work.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Nov 14 '05 #2
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in

I need to attend technical interview on C. wat type of
questions should be expected?

See if you can answer these.

What is wrong with

1)
#define min(x, y) x < y ? x : y

2)
char str[5] = "Hello";

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;

4)
if( ch != '.' || ch != ',')

5)
char *foo(int x)
{
char buff[32];
sprintf(buff, "%d", x);
return buff;
}


Nov 14 '05 #3
Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
I need to attend technical interview on C. wat type of
questions should be expected?
See if you can answer these.

What is wrong with 3) for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #4

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
What is wrong with

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.

It shows a rather different problem to the others. Hint, what happens if str
is a very long string?
Nov 14 '05 #5
Joona I Palaste wrote:
3)


for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;

Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.


It is not wrong as such. Problem is that it is in Omega(n^2).
It is a Nilgesqian problem in other words.

--
Thomas.

Nov 14 '05 #6

"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c4**********@news6.svr.pol.co.uk...
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in

I need to attend technical interview on C. wat type of
questions should be expected?
See if you can answer these.

What is wrong with

1)
#define min(x, y) x < y ? x : y


if x == y it returns y anyway?
2)
char str[5] = "Hello";
no space for string terminating null character
3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;
pass
4)
if( ch != '.' || ch != ',')
always evaluates to false
5)
char *foo(int x)
{
char buff[32];
sprintf(buff, "%d", x);
return buff;
}


returns pointer to local variable which will be destroyed
when the function has finished executing and will not be.

Where can I find more of these?
solutions will be useful.
cheers
cw
Nov 14 '05 #7
code_wrong wrote:
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c4**********@news6.svr.pol.co.uk...
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
I need to attend technical interview on C. wat type of
questions should be expected?

See if you can answer these.

What is wrong with

1)
#define min(x, y) x < y ? x : y

if x == y it returns y anyway?


x == y returns 1 or 0. Need some brackets.

#define min(x, y) ((x) < (y) ? (x) : (y))

2)
char str[5] = "Hello";

no space for string terminating null character


Correct. But that is not always an error. But if it is not a mistake
I would imagine a comment being a very very good idea.
3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;
pass


On every iteration strlen is called. (Yoda speak to avoid starting
a sentence with a function name is always fun). The loop is in
Omega(n^2) instead of Omega(n) where it should be. But it is perfectly
valid C.
4)
if( ch != '.' || ch != ',')

always evaluates to false


Umm, I think you got that the wrong way around?
Just think that ch can never be equal to two things at the
same time.
5)
char *foo(int x)
{
char buff[32];
sprintf(buff, "%d", x);
return buff;
}

returns pointer to local variable which will be destroyed
when the function has finished executing and will not be.


It will not be... A nice turn of phrase, but otherwise correct :)

--
Thomas.

Nov 14 '05 #8
Malcolm wrote:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
What is wrong with

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Am I blind or something? Other than doing more work than
necessary (and such slowing the program down) I can find
nothing wrong with this.

It shows a rather different problem to the others. Hint, what
happens if str is a very long string?


It keeps the CPU out of mischief. Still nothing _wrong_ with it.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #9

"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c4**********@news6.svr.pol.co.uk...
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in

I need to attend technical interview on C. wat type of
questions should be expected?
See if you can answer these.

What is wrong with

1)
#define min(x, y) x < y ? x : y


Does not protect against unwanted precedence evaluations. Should be:

#define min(x, y) ( ((x) < (y)) ? (x) : (y) )

I have an extra pair of parens around the logical expression - just part of
my preferred style.

2)
char str[5] = "Hello";
To store "Hello" as a string requires a 6th byte for the terminating null.
Don't know off the top of my head if the compiler will automatically expand
the definition - since it has all the information it needs - or not.

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;
Calling strlen() each pass is highly inefficient, but not "wrong". I would
also recommend reversing the operands in the if() statment:

if('a' == str[i])

that way if you goof and make the common mistake of typing '=' instead of
'==' you have just turned a logic error that might be a royal pain to track
down into a syntax error that the compiler will catch immediately.

4)
if( ch != '.' || ch != ',')
Almost certainly a logic error as it will always evaluate as true. Probably
meant to be && operator.

5)
char *foo(int x)
{
char buff[32];
sprintf(buff, "%d", x);
return buff;
}
buff is an automatic variable that is deallocated after foo() finishes
execution.


Nov 14 '05 #10
code_wrong wrote:
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message

See if you can answer these.

What is wrong with

1)
#define min(x, y) x < y ? x : y


if x == y it returns y anyway?


Nope. First, it evaluates arguments more than once, so it cannot
handle arguments with side effects. See what happens with:

min(a++, b++)

Second, the use of arguments in not guarded with parentheses, so
it can't handle expressions properly. See what happens with:

min(2 * a + b, d + 3 * c)
or
5 * min(a, b)

which last is fixed with:

#define min(x, y) ((x) < (y) ? (x) : (y))

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #11
Thomas Stegen wrote:
.... snip ...
It is not wrong as such. Problem is that it is in Omega(n^2).
It is a Nilgesqian problem in other words.


Shhh. Do not mention that name. It might appear.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #12
CBFalconer wrote:
Thomas Stegen wrote:

... snip ...

It is not wrong as such. Problem is that it is in Omega(n^2).
It is a [elided name] problem in other words.


Shhh. Do not mention that name. It might appear.


Yes, and although clc would eat him for breakfast, he wouldn't actually
realise he was wrong, no matter how often it was pointed out. He never
does.

Let sleeping idiots lie.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #13
In article <40********@nntphost.cis.strath.ac.uk>, Thomas Stegen wrote:
code_wrong wrote:
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c4**********@news6.svr.pol.co.uk...
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in

I need to attend technical interview on C. wat type of
questions should be expected?
See if you can answer these.

What is wrong with

1)
#define min(x, y) x < y ? x : y

if x == y it returns y anyway?


x == y returns 1 or 0. Need some brackets.

#define min(x, y) ((x) < (y) ? (x) : (y))


It's still note very good though, since one of it's arguments
will be evaluated twice.
2)
char str[5] = "Hello";


no space for string terminating null character


Correct. But that is not always an error. But if it is not a
mistake I would imagine a comment being a very very good idea.


These types of questions are complicated by lack of context. It's
impossible to tell if this is a bug or not, since the behavior is
defined. It does look like bad code.

To cover all the bases, the answer must somehow clue in the
interviewer that you know that the construct means, e.g.: "It
creates an array of 5 char, not a null-terminated string. That
probably isn't what the programmer wanted."
3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


pass


On every iteration strlen is called. (Yoda speak to avoid
starting a sentence with a function name is always fun). The
loop is in Omega(n^2) instead of Omega(n) where it should be.
But it is perfectly valid C.


strlen is a perfectly good way to start a sentence. Painful it
is. Yes! Mmmmmmm! ;-)
--
Neil Cerutti
"We have to show some respect for the dead!"
"Why? The dead are losers."
-- _Children Shouldn't Play with Death Things_
Nov 14 '05 #14
"code_wrong" <ta*@tac.ouch.co.uk> wrote in message

Where can I find more of these?
solutions will be useful.

The problem is that these tests put the emphasis on C trivia rather than
looking at what makes a good programmer.

For an analogy, let's say that we were askes to find the best Shakespeare
scholar at Oxford University. We could devise a test along the lines of
"write down all the characters who appear in Macbeth", and we would have a
good chance of picking out the person who is regarded as the best scholar.
However the test doesn't really examine what makes a good scholar, only what
correlates with it. If a prestigous chair rests on our decision, and people
know what sort of test we will set, then all we will achieve is wasting
everyone's time learning lists of characters.

To be a good C programmer you need to understand the principles of
structured programming. A program should be a function tree which is roughly
balanced, with functions becoming simpler and more general as you go down
the tree. There should also be a separation between portable and
platform-specific code, and between functions (which calculate values) and
procedures (which perform IO).

However the rules cannot be applied in a blind way. There's often a
trade-off between good design and efficency, and sometime the libraries you
have to use are themselves badly designed, and so force a compromise. There
is also the vexed question of error and exception processing.

So really looking for a good C programmer is more than an exercise in
setting quizzes with definite answers.
Nov 14 '05 #15
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
I need to attend technical interview on C. wat type of
questions should be expected?

See if you can answer these.

What is wrong with

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.


If strlen (str) == 1000000, then this would be wrong enough to count as
a serious bug.
Nov 14 '05 #16
In article <10*************@corp.supernews.com>,
"William L. Bahn" <wi*****@toomuchspam.net> wrote:
3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Calling strlen() each pass is highly inefficient, but not "wrong". I would
also recommend reversing the operands in the if() statment:

if('a' == str[i])

that way if you goof and make the common mistake of typing '=' instead of
'==' you have just turned a logic error that might be a royal pain to track
down into a syntax error that the compiler will catch immediately.


This one is ***absolutely wrong***. Evaluating strlen () only once might
save you from the royal pain to track down why an unimportant job on
your server that shouldn't take more than five seconds doesn't finish
overnight. On the other hand, if your compiler doesn't give you a
warning for

if (str[i] = 'a')

then you should get another compiler. Making your code unreadable is not
the solution.
4)
if( ch != '.' || ch != ',')


Almost certainly a logic error as it will always evaluate as true. Probably
meant to be && operator.


There is a completely different, equally likely explanation.
Nov 14 '05 #17
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
I need to attend technical interview on C. wat type of
questions should be expected?

See if you can answer these.

What is wrong with

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.


If strlen (str) == 1000000, then this would be wrong enough to count as
a serious bug.
Nov 14 '05 #18

"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message
de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
> I need to attend technical interview on C. wat type of
> questions should be expected?
>
See if you can answer these.

What is wrong with

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.


If strlen (str) == 1000000, then this would be wrong enough to count as
a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type
of i is unknown.
If i is declared as size_t, it should work.

Regis
Nov 14 '05 #19
In article <10*************@corp.supernews.com>,
"William L. Bahn" <wi*****@toomuchspam.net> wrote:
3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Calling strlen() each pass is highly inefficient, but not "wrong". I would
also recommend reversing the operands in the if() statment:

if('a' == str[i])

that way if you goof and make the common mistake of typing '=' instead of
'==' you have just turned a logic error that might be a royal pain to track
down into a syntax error that the compiler will catch immediately.


This one is ***absolutely wrong***. Evaluating strlen () only once might
save you from the royal pain to track down why an unimportant job on
your server that shouldn't take more than five seconds doesn't finish
overnight. On the other hand, if your compiler doesn't give you a
warning for

if (str[i] = 'a')

then you should get another compiler. Making your code unreadable is not
the solution.
4)
if( ch != '.' || ch != ',')


Almost certainly a logic error as it will always evaluate as true. Probably
meant to be && operator.


There is a completely different, equally likely explanation.
Nov 14 '05 #20

"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message
de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
"Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
> I need to attend technical interview on C. wat type of
> questions should be expected?
>
See if you can answer these.

What is wrong with

3)

for(i=0;i<strlen(str);i++)
if(str[i] == 'a')
answer++;


Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.


If strlen (str) == 1000000, then this would be wrong enough to count as
a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type
of i is unknown.
If i is declared as size_t, it should work.

Regis
Nov 14 '05 #21
Christian Bau wrote:
In article <10*************@corp.supernews.com>,
"William L. Bahn" <wi*****@toomuchspam.net> wrote:
I
would also recommend reversing the operands in the if() statment:

if('a' == str[i])
I'm not daft enough to recommend it here, but I agree with William that it's
a good idea.
that way if you goof and make the common mistake of typing '=' instead of
'==' you have just turned a logic error that might be a royal pain to
track down into a syntax error that the compiler will catch immediately.
This one is ***absolutely wrong***.


Sorry, Christian, but I disagree. It's not absolutely wrong. In fact, there
is much to be said for it.
Evaluating strlen () only once might
save you from the royal pain to track down why an unimportant job on
your server that shouldn't take more than five seconds doesn't finish
overnight. On the other hand, if your compiler doesn't give you a
warning for

if (str[i] = 'a')

then you should get another compiler.
If you have the choice, sure, why not? But what if you don't? I sometimes
think that many people in this newsgroup have never actually had a job in
the Real World! (Or, at least, talk as if they had not.) In an ideal world,
sure, we'd have a nice compiler, but sometimes we're stuck with the
compiler we've got and we have to make do.
Making your code unreadable is not the solution.


I agree that making code unreadable is not the solution, but I do not agree
that ('a' == str[i]) is unreadable.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #22
"code_wrong" <ta*@tac.ouch.co.uk> writes:
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c4**********@news6.svr.pol.co.uk...
5)
char *foo(int x)
{
char buff[32];
sprintf(buff, "%d", x);
return buff;
}


returns pointer to local variable which will be destroyed
when the function has finished executing and will not be.


A text representation of the value of `x' better fit in 31 characters,
otherwise undefined behavior is invoked before the return statement is
reached.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #23
Christian Bau wrote:
In article <10*************@corp.supernews.com>,
"William L. Bahn" <wi*****@toomuchspam.net> wrote:
I
would also recommend reversing the operands in the if() statment:

if('a' == str[i])
I'm not daft enough to recommend it here, but I agree with William that it's
a good idea.
that way if you goof and make the common mistake of typing '=' instead of
'==' you have just turned a logic error that might be a royal pain to
track down into a syntax error that the compiler will catch immediately.
This one is ***absolutely wrong***.


Sorry, Christian, but I disagree. It's not absolutely wrong. In fact, there
is much to be said for it.
Evaluating strlen () only once might
save you from the royal pain to track down why an unimportant job on
your server that shouldn't take more than five seconds doesn't finish
overnight. On the other hand, if your compiler doesn't give you a
warning for

if (str[i] = 'a')

then you should get another compiler.
If you have the choice, sure, why not? But what if you don't? I sometimes
think that many people in this newsgroup have never actually had a job in
the Real World! (Or, at least, talk as if they had not.) In an ideal world,
sure, we'd have a nice compiler, but sometimes we're stuck with the
compiler we've got and we have to make do.
Making your code unreadable is not the solution.


I agree that making code unreadable is not the solution, but I do not agree
that ('a' == str[i]) is unreadable.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #24
"code_wrong" <ta*@tac.ouch.co.uk> writes:
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:c4**********@news6.svr.pol.co.uk...
5)
char *foo(int x)
{
char buff[32];
sprintf(buff, "%d", x);
return buff;
}


returns pointer to local variable which will be destroyed
when the function has finished executing and will not be.


A text representation of the value of `x' better fit in 31 characters,
otherwise undefined behavior is invoked before the return statement is
reached.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #25

"Richard Heathfield" <do******@address.co.uk.invalid> wrote in

I agree that making code unreadable is not the solution, but I do not > agree that ('a' == str[i]) is unreadable.

In itself it doesn't damage readability too much. The problem is the
cumulative effect of too many constructs like that (such as for(;;),
typedefing every single basic type, complex indirection, etc).
Nov 14 '05 #26

"Richard Heathfield" <do******@address.co.uk.invalid> wrote in

I agree that making code unreadable is not the solution, but I do not > agree that ('a' == str[i]) is unreadable.

In itself it doesn't damage readability too much. The problem is the
cumulative effect of too many constructs like that (such as for(;;),
typedefing every single basic type, complex indirection, etc).
Nov 14 '05 #27
Malcolm wrote:

"Richard Heathfield" <do******@address.co.uk.invalid> wrote in

I agree that making code unreadable is not the solution, but I do not > agree that ('a' == str[i]) is unreadable.

In itself it doesn't damage readability too much. The problem is the
cumulative effect of too many constructs like that (such as for(;;),


....which I don't use...
typedefing every single basic type,
....which I don't do...
complex indirection, etc).


....and which I only do when it is necessary.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #28
Malcolm wrote:

"Richard Heathfield" <do******@address.co.uk.invalid> wrote in

I agree that making code unreadable is not the solution, but I do not > agree that ('a' == str[i]) is unreadable.

In itself it doesn't damage readability too much. The problem is the
cumulative effect of too many constructs like that (such as for(;;),


....which I don't use...
typedefing every single basic type,
....which I don't do...
complex indirection, etc).


....and which I only do when it is necessary.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #29

"Régis Troadec" <re**@wanadoo.fr> wrote in message
> What is wrong with

> 3)

> for(i=0;i<strlen(str);i++)
> if(str[i] == 'a')
> answer++;

I'm not sure that someone can say this code is wrong or not, since > the

type of i is unknown. If i is declared as size_t, it should work.

The question is, what is wrong with it? What's wrong is that C evaluates the
strlen() call on every loop iteration, so we have an O(N*N) algorithm which
can be trivially transformed into an O(N) algorithm. This is just a stupid
waste of processor cycles, which may or may not matter depending on where
the bottlenecks are in the program.
Nov 14 '05 #30

"Régis Troadec" <re**@wanadoo.fr> wrote in message
> What is wrong with

> 3)

> for(i=0;i<strlen(str);i++)
> if(str[i] == 'a')
> answer++;

I'm not sure that someone can say this code is wrong or not, since > the

type of i is unknown. If i is declared as size_t, it should work.

The question is, what is wrong with it? What's wrong is that C evaluates the
strlen() call on every loop iteration, so we have an O(N*N) algorithm which
can be trivially transformed into an O(N) algorithm. This is just a stupid
waste of processor cycles, which may or may not matter depending on where
the bottlenecks are in the program.
Nov 14 '05 #31
Richard Heathfield wrote:
Malcolm wrote:

"Richard Heathfield" <do******@address.co.uk.invalid> wrote in
I agree that making code unreadable is not the solution, but I do not >


agree that ('a' == str[i]) is unreadable.

In itself it doesn't damage readability too much. The problem is the
cumulative effect of too many constructs like that (such as for(;;),

...which I don't use...

typedefing every single basic type,

...which I don't do...

complex indirection, etc).

...and which I only do when it is necessary.

Has no one mentioned that..
answer++;
...treats an undefined variable?

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #32
Richard Heathfield wrote:
Malcolm wrote:

"Richard Heathfield" <do******@address.co.uk.invalid> wrote in
I agree that making code unreadable is not the solution, but I do not >


agree that ('a' == str[i]) is unreadable.

In itself it doesn't damage readability too much. The problem is the
cumulative effect of too many constructs like that (such as for(;;),

...which I don't use...

typedefing every single basic type,

...which I don't do...

complex indirection, etc).

...and which I only do when it is necessary.

Has no one mentioned that..
answer++;
...treats an undefined variable?

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #33
Joe Wright wrote:
Has no one mentioned that..
answer++;
..treats an undefined variable?


I think a guy called Joe pointed it out.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #34
Joe Wright wrote:
Has no one mentioned that..
answer++;
..treats an undefined variable?


I think a guy called Joe pointed it out.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #35

"Joe Wright" <jo********@comcast.net> wrote in message

Has no one mentioned that..
answer++;
..treats an undefined variable?

All the examples are code fragments. If you are looking at real code then
you won't always know what is in scope at any one time.
There's also not much point giving as a problem something which will be
picked up at compile time.
Nov 14 '05 #36

"Joe Wright" <jo********@comcast.net> wrote in message

Has no one mentioned that..
answer++;
..treats an undefined variable?

All the examples are code fragments. If you are looking at real code then
you won't always know what is in scope at any one time.
There's also not much point giving as a problem something which will be
picked up at compile time.
Nov 14 '05 #37
In article <c4**********@news-reader4.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message
de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
> "Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
>> I need to attend technical interview on C. wat type of
>> questions should be expected?
>>
> See if you can answer these.

> What is wrong with

> 3)

> for(i=0;i<strlen(str);i++)
> if(str[i] == 'a')
> answer++;

Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.


If strlen (str) == 1000000, then this would be wrong enough to count as
a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type
of i is unknown.
If i is declared as size_t, it should work.


You failed the interview.
Nov 14 '05 #38
In article <c4**********@news-reader4.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message
de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
> "Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
>> I need to attend technical interview on C. wat type of
>> questions should be expected?
>>
> See if you can answer these.

> What is wrong with

> 3)

> for(i=0;i<strlen(str);i++)
> if(str[i] == 'a')
> answer++;

Am I blind or something? Other than doing more work than necessary (and
such slowing the program down) I can find nothing wrong with this.


If strlen (str) == 1000000, then this would be wrong enough to count as
a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type
of i is unknown.
If i is declared as size_t, it should work.


You failed the interview.
Nov 14 '05 #39

"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message
de news:ch*********************************@slb-newsm1.svr.pol.co.uk...

Hi,
In article <c4**********@news-reader4.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:

> Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
> > "Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
> >> I need to attend technical interview on C. wat type of
> >> questions should be expected?
> >>
> > See if you can answer these.
>
> > What is wrong with
>
> > 3)
>
> > for(i=0;i<strlen(str);i++)
> > if(str[i] == 'a')
> > answer++;
>
> Am I blind or something? Other than doing more work than necessary (and > such slowing the program down) I can find nothing wrong with this.

If strlen (str) == 1000000, then this would be wrong enough to count as a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type of i is unknown.
If i is declared as size_t, it should work.


You failed the interview.


I'm sorry but I don't think so. I agree with Malcolm's answer concerning the
problem of inefficiency but my opinion is this problem is as much a feature
of algorithm analysis and can occur in many other languages. Another point
is that you outlined a possible serious bug in your previous message when
strlen(str) == 1000000. Sure, It could be wrong enough and turn into an
infinite loop according to the type of i, if it's a short int for example,
wasn't that you meant?

Regis


Nov 14 '05 #40

"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message
de news:ch*********************************@slb-newsm1.svr.pol.co.uk...

Hi,
In article <c4**********@news-reader4.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:

> Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
> > "Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
> >> I need to attend technical interview on C. wat type of
> >> questions should be expected?
> >>
> > See if you can answer these.
>
> > What is wrong with
>
> > 3)
>
> > for(i=0;i<strlen(str);i++)
> > if(str[i] == 'a')
> > answer++;
>
> Am I blind or something? Other than doing more work than necessary (and > such slowing the program down) I can find nothing wrong with this.

If strlen (str) == 1000000, then this would be wrong enough to count as a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type of i is unknown.
If i is declared as size_t, it should work.


You failed the interview.


I'm sorry but I don't think so. I agree with Malcolm's answer concerning the
problem of inefficiency but my opinion is this problem is as much a feature
of algorithm analysis and can occur in many other languages. Another point
is that you outlined a possible serious bug in your previous message when
strlen(str) == 1000000. Sure, It could be wrong enough and turn into an
infinite loop according to the type of i, if it's a short int for example,
wasn't that you meant?

Regis


Nov 14 '05 #41
In article <c4**********@news-reader5.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
I'm sorry but I don't think so. I agree with Malcolm's answer concerning the
problem of inefficiency but my opinion is this problem is as much a feature
of algorithm analysis and can occur in many other languages. Another point
is that you outlined a possible serious bug in your previous message when
strlen(str) == 1000000. Sure, It could be wrong enough and turn into an
infinite loop according to the type of i, if it's a short int for example,
wasn't that you meant?


How long does it take to make one million calls to strlen () when the
length of the string is one million? How likely is it that the user of
the program believes that the program just crashed and gives up? If this
program runs on a portable computer, will it finish with one battery?

Since this thread is about a job interview: How likely is it that
customers will complain and stop buying your software if the programmers
do nonsense like this?
Nov 14 '05 #42
In article <c4**********@news-reader5.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
I'm sorry but I don't think so. I agree with Malcolm's answer concerning the
problem of inefficiency but my opinion is this problem is as much a feature
of algorithm analysis and can occur in many other languages. Another point
is that you outlined a possible serious bug in your previous message when
strlen(str) == 1000000. Sure, It could be wrong enough and turn into an
infinite loop according to the type of i, if it's a short int for example,
wasn't that you meant?


How long does it take to make one million calls to strlen () when the
length of the string is one million? How likely is it that the user of
the program believes that the program just crashed and gives up? If this
program runs on a portable computer, will it finish with one battery?

Since this thread is about a job interview: How likely is it that
customers will complain and stop buying your software if the programmers
do nonsense like this?
Nov 14 '05 #43
"Régis Troadec" wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit:

.... snip ...
> >
> > > What is wrong with
> >
> > > 3)
> >
> > > for(i=0;i<strlen(str);i++)
> > > if(str[i] == 'a')
> > > answer++;
> >
.... snip ...
You failed the interview.


I'm sorry but I don't think so. I agree with Malcolm's answer
concerning the problem of inefficiency but my opinion is this
problem is as much a feature of algorithm analysis and can occur
in many other languages. Another point is that you outlined a
possible serious bug in your previous message when strlen(str)
== 1000000. Sure, It could be wrong enough and turn into an
infinite loop according to the type of i, if it's a short int
for example, wasn't that you meant?


Nobody seems to have bothered to point out that str itself may be
volatile and is being dynamically modified, in which case those
who want to store the entry value of strlen(str) are introducing
bugs. As a matter of fact, there is no need to EVER call strlen
for this:

for (p = str; *p; p++)
if ('a' == *p) answer++;

The original code is inefficient, not wrong. Bubblesort is also
inefficient, not wrong.

The thing that is *wrong* with the original code is the lack of
blanks in the source.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #44
"Régis Troadec" wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit:

.... snip ...
> >
> > > What is wrong with
> >
> > > 3)
> >
> > > for(i=0;i<strlen(str);i++)
> > > if(str[i] == 'a')
> > > answer++;
> >
.... snip ...
You failed the interview.


I'm sorry but I don't think so. I agree with Malcolm's answer
concerning the problem of inefficiency but my opinion is this
problem is as much a feature of algorithm analysis and can occur
in many other languages. Another point is that you outlined a
possible serious bug in your previous message when strlen(str)
== 1000000. Sure, It could be wrong enough and turn into an
infinite loop according to the type of i, if it's a short int
for example, wasn't that you meant?


Nobody seems to have bothered to point out that str itself may be
volatile and is being dynamically modified, in which case those
who want to store the entry value of strlen(str) are introducing
bugs. As a matter of fact, there is no need to EVER call strlen
for this:

for (p = str; *p; p++)
if ('a' == *p) answer++;

The original code is inefficient, not wrong. Bubblesort is also
inefficient, not wrong.

The thing that is *wrong* with the original code is the lack of
blanks in the source.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #45
In article <40***************@yahoo.com>,
CBFalconer <cb********@yahoo.com> wrote:
Nobody seems to have bothered to point out that str itself may be
volatile and is being dynamically modified, in which case those
who want to store the entry value of strlen(str) are introducing
bugs. As a matter of fact, there is no need to EVER call strlen
for this:

for (p = str; *p; p++)
if ('a' == *p) answer++;
You mean str points to volatile data, not str is volatile, right? If the
string pointed to by str is dynamically modified, then your code is very
likely to fail: str[101] might be zero, but when p == &str [50], str
[101] is changed to nonzero and str [0] is set to zero.

The original code is inefficient, not wrong.
When the code becomes so inefficient that it cannot do what it is
supposed to do, that makes it wrong.

Bubblesort is also
inefficient, not wrong.

Nov 14 '05 #46
In article <40***************@yahoo.com>,
CBFalconer <cb********@yahoo.com> wrote:
Nobody seems to have bothered to point out that str itself may be
volatile and is being dynamically modified, in which case those
who want to store the entry value of strlen(str) are introducing
bugs. As a matter of fact, there is no need to EVER call strlen
for this:

for (p = str; *p; p++)
if ('a' == *p) answer++;
You mean str points to volatile data, not str is volatile, right? If the
string pointed to by str is dynamically modified, then your code is very
likely to fail: str[101] might be zero, but when p == &str [50], str
[101] is changed to nonzero and str [0] is set to zero.

The original code is inefficient, not wrong.
When the code becomes so inefficient that it cannot do what it is
supposed to do, that makes it wrong.

Bubblesort is also
inefficient, not wrong.

Nov 14 '05 #47

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@news-reader4.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:

> Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
> > "Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
> >> I need to attend technical interview on C. wat type of
> >> questions should be expected?
> >>
> > See if you can answer these.
>
> > What is wrong with
>
> > 3)
>
> > for(i=0;i<strlen(str);i++)
> > if(str[i] == 'a')
> > answer++;
>
> Am I blind or something? Other than doing more work than necessary (and > such slowing the program down) I can find nothing wrong with this.

If strlen (str) == 1000000, then this would be wrong enough to count as a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type of i is unknown.
If i is declared as size_t, it should work.


You failed the interview.


Why? If the interviewer is looking at knowledge of the C language, then
this is an excellent answer and I would be pleased to get it. Given
certain assumptions about the types of i, str, and answer, there is
nothing "wrong" with this as an example of C code. Pointing out how the
code would go wrong with inappropriate types for these variables would
be a great answer about C itself.

Having got (or not got) that answer I would then go on to say "Is there
anything wrong with that code as a section of a real-world program?" and
I would expect to get the performance answer.

The moral is to think about all aspects of the question, and don't assume
that the questioner is really asking the question that his words make up.
Talk about all aspects of the question.

Nov 14 '05 #48

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@news-reader4.wanadoo.fr>,
"Régis Troadec" <re**@wanadoo.fr> wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> a écrit dans le message de news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <c4**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:

> Malcolm <ma*****@55bank.freeserve.co.uk> scribbled the following:
> > "Cherrish Vaidiyan" <un***************@yahoo.com> wrote in
> >> I need to attend technical interview on C. wat type of
> >> questions should be expected?
> >>
> > See if you can answer these.
>
> > What is wrong with
>
> > 3)
>
> > for(i=0;i<strlen(str);i++)
> > if(str[i] == 'a')
> > answer++;
>
> Am I blind or something? Other than doing more work than necessary (and > such slowing the program down) I can find nothing wrong with this.

If strlen (str) == 1000000, then this would be wrong enough to count as a serious bug.


I'm not sure that someone can say this code is wrong or not, since the type of i is unknown.
If i is declared as size_t, it should work.


You failed the interview.


Why? If the interviewer is looking at knowledge of the C language, then
this is an excellent answer and I would be pleased to get it. Given
certain assumptions about the types of i, str, and answer, there is
nothing "wrong" with this as an example of C code. Pointing out how the
code would go wrong with inappropriate types for these variables would
be a great answer about C itself.

Having got (or not got) that answer I would then go on to say "Is there
anything wrong with that code as a section of a real-world program?" and
I would expect to get the performance answer.

The moral is to think about all aspects of the question, and don't assume
that the questioner is really asking the question that his words make up.
Talk about all aspects of the question.

Nov 14 '05 #49
In <f0**************************@posting.google.com > un***************@yahoo.com (Cherrish Vaidiyan) writes:
Hope everyone is doing fine.i feel pointers to be the most toughest
part in C. i have just completed learning pointers & arrays related
portions. I need to attend technical interview on C. wat type of
questions should be expected? Which part of C language do the staff
give more concern?
The interviewers have just mentioned that .. i will have interview on
C.
Also can anyone can help me with sites where i can go thru sample
programs in C?


If you read (and understand) the FAQ, you should be well prepared for
an interview dealing with language technicalities.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #50

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

Similar topics

35
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
3
by: Dougie | last post by:
Hi, I have an interview later in the week and I've been told that I'll be asked technical questions about ASP.NET. The job is for the position of .NET Web developer and I just wondered if any...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.