Microsoft Interview Question for Software Engineer in Tests






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

Total Six bug in the code

1. No validation of the inputs, if NULL is passed to any of the input function will crash while dereferencing (while(*s1)).
2. Buffer is not allocated based on the sum of the length of the input string, which will lead to buffer overrun (in this case stack overrun) if the combined string length is greater than 1024 (char buffer[1024]).
3. Address of the local variable is returned, buffer should be allocated on heap (dynamic memory) and its pointer should be returned (return buffer).
4. ++i is creating a byte between the two string used for concatenation to contain garbage value (buffer[++i]=s2).
5. pointer is being assigned to char value (buffer[++i]=s2).
6. The combined string is not null terminated.

- Swamy September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2 mistakes.
1) There is a gap in between two strings (have some garbage value).
2) string not ended with '\0'.

- Saumya July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

3) "buffer" is stack variable.

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

buffer array is local to the function. It should be declared static.

- Rohan July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

4) No check on length , if length of (S1+S2) > 1024 , we are writing beyond the boundary of array.

BTW, any idea how long MS takes to declare results for written tests ?

- Skand July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) There is a gap in between two strings (have some garbage value).
2) string not ended with '\0'.
3) size of buffer not enough, if size of (S1+S2) > 1024.
4) In copying second string [ buffer[++i]=s2; ] RHS should be *s2.

- Venkat July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we are not allocating the space via malloc so it shd be an error .... wat does gap betw strings mean ???

- guest August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1> return pointer to stack memory
2> size not checked
3> we were doing i++ then we switched to ++i so there is has been a double increment
4> and no null assigned at end

- Coder August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* find the bug in the following code which concatenates 2 strings ---*/
char * concatenate(char *s1,char *s2) 
{
    /* 1) s1 and s2 can be null and the method never checks if they are initialized */
    char buffer[1024];   /* 2) can produce overflow because lenghts of s1 and s2 are unknown */
    int i=0;
    while(*s1)
    {
        buffer[i++]=*s1;
        s1++;
    }
    while(*s2)
    {
        buffer[++i]=s2;  /* 3) ++i jumps an additional character */ /* 4) can't pass char* to char */
        s2++; 
    }
    return buffer;    /* 5) buffer doesn't end with null character */ 
/* 6) can't return a pointer to a previous declared function, must use malloc, not static */
}

- noaguila&foo_fighter August 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dont u all think that the two strings are having ther original pointers ass1 and s2... now if we perform s1++,we are changing s2,which means that access to array is lost from begining,hence..it will give compiler error..i.e."lvalue required in function main"

- rockstar September 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i mean we are changing s1 and s2..in thos operation

- rockstar September 02, 2011 | Flag


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