Torin/Darren WhoEver (wolfieboy) wrote,
Torin/Darren WhoEver
wolfieboy

  • Mood:

Interviews, skill assessments, such

I realize that I haven't kept people up-to-date on the latest interview games; it just doesn't always occur to me unless something unusual happens.

I took a skill assessment today. Some of it was in C which worried me a little since my C has been rather sporadic for a while but I took care of the following questions quite handily. If you see any errors in the code I provided, please feel free to tell me.

Question 3

Given an array of strings, implement a function that returns the length of the longest one.

assuming that the array of strings is terminated by a string of length zero, i.e. it consists only of null and that the strings will continue to persist after function returns. This allows us to just return a pointer to the string. We also assume that if two strings are of equal length, we choose the first one.
We could be sent the number of strings that are in the array if we needed to be able to count zero-length strings.

char *LongestString(char **stringArray)
{
char *longest;
int curLength, maxLength = 0;
char **cur = stringArray;

while (**cur != `\0')
{
curLength = strlen(*cur);
if (curLength > maxLength)
{
maxLength = curLength;
longest = *cur;
}
cur += curLength + 1;
}
return longest;
}


Question 4

Is there anything wrong/dangerous with the following code:

char *string = malloc(100);
sprintf ( string, "XYZ = %d", get_some_integer() );
free ( string );


If so, how would you re-write the code?

multiple things
1. the return from the malloc wasn't checked
2. the length of the integer being put into string wasn't checked

char *string = malloc(100);
if (string != NULL)
{
snprintf(string, 100, "XYZ = %d", get_some_integer());
free(string);
}


Question 5

Given an array of pointers declared:

int *array[100];

Is there any difference between this code:

int i = 0;
for (;i<100;i++) array[i]=0;


and this code:

memset ( array, 0, sizeof(int *)*100 );

no

Question 6

Anything wrong with this code:

char * a, b,c;
a="hello";
b=c=a;


Yes, quite a bit. Only a has been declared a pointer to char. The others are single characters only.

[.Edit.] Well, it seems that they liked my code well enough to want to interview me further...
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 12 comments