Microsoft Interview Question
Software Engineer / DevelopersI have 3.5yrs exp. It was a written test which had the questions I posted. Test was answerable which I screwed it up.
Slight modification in the code:
strcpy(buffer,s);
buffer[l]='\n'; // Add a new line character to buffer;
return buffer;
according to me it's outpur would be - contents of s + '\n' + garbage values till '\0' as you have over written '\0'
char* NewLine(char* s)
{
char buffer[1024];
l = strlen(s);
strcpy(s,buffer); // i think this line should be strcpy(buffer,s) and also you need to check if 1024 is a large enough buffer
s[l]='\n'; // this line should be buffer[i]='\n' and you need to add buffer[i+1] = '\0' ;
return s;
}
so I think the correct version should be
char* NewLine(char* s)
{
char buffer[1024];
l = strlen(s);
if(I<1023){
strcpy(s,buffer);
buffer[l]='\n'; // Add a new line character to s;
buffer[I]='\n';
return buffer;
}
else
return null;
}
1. strcpy... may overflow the allocated buffer (i.e. 1024) and may result in
- Crime_Master_GoGo July 10, 2010crash,exception
2. What if length of s is >= 1024? this will work fine only when length of s <1024
3. What if s is not a null terminated string
4. you are returning a local variable...whose scopes end with this function.
4. is what they were looking for i guess?
how much experinace do you have Rahul? and what were the other questions you were asked? How did it go over all? where were you interviewed?