Microsoft Interview Question for Software Engineer / Developers






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

string greater than 1000 will not cause buffer overflow always.
returning local variable is the bug

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

Hi Sambit,

In the above code you've given there is NO error. Are you sure you are not missing something? Can you elaborate on the "copy algo".

P.S. Only the first element - buf[0] - will be initialized to 0. It will be displayed a null character if you print it as %c or 0 if you print it as %d.

Let us know.

Cheers

- Researcher May 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

SORRY There IS a bug in this. I overlooked it at first.

Actually it is a warning thrown by the compiler- you are trying to return an address of an automatic variable local to copy function.

Initially I thought I'll make it static or global.. but guess what it will even then compile and give the correct output. Try this:

#include<iostream>
using namespace std;

char * copy (char *);

int main()
{
char *result;
char something='x';
char *whatever = &something;

result = copy(whatever);
result[0] = 'X';
cout<<result;

return 0;
}

char * copy(char *p)
{
//I skipped char buf[1000]={0} coz its the null character
char buf[1000] = {'A','B','C'};
//copy algo[I skipped code here]
return buf;
}

- Researcher May 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are 2 bugs in this code.
First, is that if the length of source string is greater than 1000, we will have buffer overflow here and crush.
Second - returning the adress of local variable. the return value shall be allocated using new operator.

- gevorgk May 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u pls explain wat will be the consequences of this bug.. as in.. can u explain "the return value shall be allocated using new operator" ??

- rhino June 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Buffer overflow will cause stack demage and crush.
Returning adress of local variable can cause unexpected behaviour, because after function return the memory can be oerwritten by some ohter routine. So the correct function shall look like this

char * copy(char *p)
{
char* buf = new char[strlen(p)];
//copy algo[I skipped code here]
return buf;
}

- gevorgk June 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

char* copy(char* s)
{int l;
char buf[1000];
l = strlen(s);
strcpy(buf,s);
buf[l]='\n'; // Add a new line character to s;
return buf;}
int main(){
char s[32]="hello";
printf("%s",copy(s));
}


@gevork ......buddy dis code works f9....
its returning wat it has to...
comment on it...!?

- aditya rajhans October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

MS IDC makes fool of people.
People have to come to office on weekends due to workload and do night outs, no work life balance. They pay 10-20% more make people labour.

Do take the feedback from employees before joining MS.

And work is junk, all junk wor from Redmond is transferred to IDC. Ask any team, whether they design, implement products or just do porting or maintenance or make tools.

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

LOL.. Did you quit MS IDC??

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

I told the warning msg what 'Researcher' mentioned here.
@Anonymous : I feel you are actually correct. Same thing I have heard from lot of my friends also. Better to say - I have never heard any good-words for MS.

- Sambit June 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

buf is a local variable and you are returning an address of a local variable which will not exist once you are our of the function and hence you will just pass garbage.

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

this is a case of dangling pointer...in which after the function returns the memory allocated to buffer is deallocated...hence in main we might try to access this location which is no longer in existence.

- Anonymous August 30, 2010 | 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