Microsoft Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

1. strcpy... may overflow the allocated buffer (i.e. 1024) and may result in
crash,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?

- Crime_Master_GoGo July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have 3.5yrs exp. It was a written test which had the questions I posted. Test was answerable which I screwed it up.

- Rahul July 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Slight modification in the code:

strcpy(buffer,s);
buffer[l]='\n'; // Add a new line character to buffer;
return buffer;

- Rahul July 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

buffer is a local variable. Nothing is returned.

- Anonymous July 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry. wrong comment

- Anonymous July 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This function does nothing useful, since the returned string cannot be stored in the local variable buffer. We have to create a new memory to store the returned string which is the purpose of this function.

- Anonymous July 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the output should be the original string
appended with \n and 1024 bytes dump of the calling stack.

- fiddler.g July 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what kind of type "l" ?

- bushido@ce.kmitl.net July 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

"l" is the length of the string s

- sushbis July 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

according to me it's outpur would be - contents of s + '\n' + garbage values till '\0' as you have over written '\0'

- Anonymous August 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

based on the revision, as long as the buffer is not overflowed, this answer should be correct

- K November 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think last one is correct

- Anonymous August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Anonymous March 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* newline(char* s)
{
int l = strlen(s);
static char buffer[1024];
strcpy(buffer,s);
l = strlen(buffer);
buffer[l]='\n';
buffer[l+1]='\0';
return buffer;
}

- rasmus April 06, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More