So I got through a phone interview...

Started by
32 comments, last by Rainault 17 years, 6 months ago
EA was asking things like: "Explain the advantages and disadvantages of defining enumerated values using const, #define, and enum".

Or something along those lines. You might get questions like that which are meant to determine whether you understand what is actually going on "under the hood" rather than whether you understand a particular language's syntax completely.
Advertisement
My favourite C-Question is: What's the difference between a struct and a union?
Basically a union is like a struct but its memory is only equivalent with that of its largest variable. All the variables share that memory.

A nice example would be a union of a UINT16 and 2 UINT8 (bytes). That way you can write something into the short and then read out each byte individually.
Pretty useful if you don't know which endian configuration your target has.
Quote:Original post by Serapth
Quote:Original post by Ozymandias42
I think one of my favorites was "Explain to me an algorithm for sorting cards. Okay, good. Give me a different one. Okay, good. Give me a third one. Which one of those 3 would you choose? Why?"

I like algorithmic or logic questions like the one you mentioned... although asking for 3! implementations is a bit evil! ;).

I do that all the time actually. I'll give somebody a coding problem and when they're done ask them to brainstorm (not code) alternative solutions to various aspect of the problem and compare/contrast them. It's a good way to get them to think about how various solutions can be better as circumstances and assumptions vary.

Quote:
Well, thats a bit of a fib. The oddest question was... "Banana, Apple or Peach. Which is the best fruit?". To this day im not sure what they were trying to figure out with that one. For the record... my answer was "Is the apple red, or green?", which results in a laugh and later a job offer. So, if you ever get asked that question, the answer of course is green apples! [smile]

Probably they were just trying to help you relax. Some people will come into an interview wound up incredibly tight, especially if they just got out of a previous one and didn't to well. A little humor usually helps immensely. It may also be that they'd already made a decision about you and were just killing time.
-Mike
Quote:1.) In what situation would you want to mark a variable "volatile const"?

2.) In a C preprocessor macro, what does ## signify?

3.) What is the output of this code:
int c = 1;
c = ++c + c++ + c + ++c;
printf("%d",++c);


1) Good question. I've never actually used the keyword "volatile" before. *wanders off to look it up*
2) Token pasting, as stated in an earlier post.
3) As I understand, the correct answer is "undefined". The C standard doesn't allow you to change a variable twice in a single statement, and compilers can interpret that second line differently.

Quote:1. Write a function to reverse a string (char array). Optimize it (looking for the use of pointer arithmetic instead of array indexing).


void reverse(char * s, unsigned int n){   char * s_end = s + n - 1;   while (s < s_end)   {      *s ^= *s_end;      *s_end ^= *s;      *s ^= *s_end;      ++s;      --s_end;   }}


Quote:2. Which of these calls to your function will work, and why?

a)

int main()
{
std::string str = "Hello";
reverse(str.c_str(), 5);
}



b)

int main()
{
char * str = "Hello";
reverse(str, 5);
}



c)

int main()
{
char str[] = "Hello";
reverse(str, 5);
}


a shouldn't work; c_str() returns a const char *, which can't be directly cast into a char *. b and c should work fine, since they do the exact same thing.

Quote:3. Implement atoi/itoa.


/* Assumes we're in C and can't use C++ libraries or Boost */#include &lt;ctype.h&gt;int atoi(const char * s){   int result = 0, negative = 0;   while (isspace(*s)) ++s;   if (*s == '-')   {      negative = 1;      ++s;   }   while (*s != '\0')   {      if (!isdigit(*s)) break;      result *= 10;      result += *s - '0';      ++s;   }   return negative ? -result : result;}void itoa(int n, char * buffer, int radix){   int nextDigit, bufferSize = 0;   while (n &gt;= radix)   {      nextDigit = n % radix;      *buffer = nextDigit + '0';      ++buffer;      ++bufferSize;      n /= radix;   }   *buffer = n + '0';   ++buffer;   ++bufferSize;   *buffer = '\0';   reverse(buffer - bufferSize, bufferSize);}


Quote:4. This function returns an approximation (within 1e-6) to the result of a mathematical operation on val. What is that operation?

double f(double val)
{
double app = 1.0;
double err = 1.0;

while(err > 1e-6)
{
app = (app * app + val) / (2 * app);
err = val/app - app;
}
return app;
}

What is the error in the while loop?


Without running any test cases or compiling this code, my first guess is that this is a sqrt function. The error in the loop is that the while condition should be "abs(err) > 1e-6".

This topic is closed to new replies.

Advertisement