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

  • Thread starter Cherrish Vaidiyan
  • Start date
C

Cherrish Vaidiyan

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
 
J

Joona I Palaste

Cherrish Vaidiyan said:
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.
 
M

Malcolm

Cherrish Vaidiyan said:
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 == 'a')
answer++;

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

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

Malcolm

Joona I Palaste said:
What is wrong with

for(i=0;i<strlen(str);i++)
if(str == '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?
 
T

Thomas Stegen

Joona said:
3)

for(i=0;i<strlen(str);i++)
if(str == '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.
 
C

code_wrong

Malcolm said:
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 == '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
 
T

Thomas Stegen

code_wrong said:
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 == '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.
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 :)
 
C

CBFalconer

Malcolm said:
Joona I Palaste said:
What is wrong with

for(i=0;i<strlen(str);i++)
if(str == '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.
 
W

William L. Bahn

Malcolm said:
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 == '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)

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.
 
C

CBFalconer

code_wrong said:
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))
 
C

CBFalconer

Thomas said:
.... 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.
 
R

Richard Heathfield

CBFalconer said:
Thomas said:
... 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.
 
N

Neil Cerutti

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 == '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! ;-)
 
M

Malcolm

code_wrong said:
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.
 
C

Christian Bau

Joona I Palaste said:
Malcolm said:
See if you can answer these.
What is wrong with

for(i=0;i<strlen(str);i++)
if(str == '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.
 
C

Christian Bau

3)

for(i=0;i<strlen(str);i++)
if(str == '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)

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.[/QUOTE]

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 = 'a')

then you should get another compiler. Making your code unreadable is not
the solution.
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.
 
R

Régis Troadec

Christian Bau said:
Joona I Palaste said:
Malcolm said:
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

for(i=0;i<strlen(str);i++)
if(str == '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
 
R

Richard Heathfield

Christian said:
William L. Bahn said:
I
would also recommend reversing the operands in the if() statment:

if('a' == str)


I'm not daft enough to recommend it here, but I agree with William that it's
a good idea.
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 = '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) is unreadable.
 
M

Martin Dickopp

code_wrong said:
Malcolm said:
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
 

Ask a Question

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

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top