Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Written Test




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

@Charu:

1) (*a != 0) is equivalent to (*a != '\0'). When you compare an int with a char, the char just gets promoted to an int, as with other integer types.
2) You're right, we should have kept a (fix) reference to the beginning of the memory buffer allocated by malloc.
3) Yes, after the while loop you need the *f = '\0', or *f = 0.
4) That's not totally right. The pointer f (and also p in your modified example) will indeed die after the function returns, but the buffer pointed by it will remain intact on the heap. You should not return a reference or a pointer to neither f nor p, but it's completely safe to return p itself.

Last detail, the malloc should have been as follows:

f = (char*)malloc((strlen(a) + 1) * sizeof(char));

Otherwise, after you have filled your buffer with strlen(a) bytes inside the while loop, you will be writing your '\0' terminator outside of the allocated memory block.

- Luis March 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

On top of that cout << f; can't work because cout takes a pointer to the beginning of the string not the end, right? the return will not return the wanted string for the same reason.

- albinoadriano April 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

There are 4 mistakes in this piece of code.
1. while(*a != 0 ) is incorrect, it should be while(*a != '\0')
2. f is incremented in the while loop, so we have lost the reference for the first char of string f.
3. Last char in a char string should be terminating char '\0'.
4. you cant return the reference of a local variable. The life time of this variable is in this method call only.
I think the correct code should be :
void cp(char *a)
{
char *f,*p;
f=(char *)malloc(strlen(a)*sizeof(char));
p = f;
while(*a != '\0') //*a != 0
{
*f=*a;
f++;
a++;
}
*f = '\0';
printf("%s\n",p);
//return f;
}

Please correct me if i am wrong.

- Charu March 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

1. (*a != 0) is fine.
2. Correct.
3. Correct.
4. Should be fine. It's returning an address to a memory on the heap.

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

f is not null terminated.

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

f is not null terminated.

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

// creates a copy of a and also dumps to stdout
char* cp(char *a) 
{ 
char *f; 
f=(char*)malloc(strlen(a)*sizeof(char));  // need to allocate an extra character for null termination, malloc may fail, so should initialize f to null, and probably check for it
while(*a != 0) 
{ 
*f=*a; // pointer to beginning of character string is lost, should have a char *g = f before the while and return g instead
f++; 
a++; 
}  // f is not null terminated, should do *f = '\0';
cout<< f; 
return f; 

}

- babesh March 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 - memory allocated to f should be + 1.
2 - Keep "a" in some temp variable "temp".
3 - just after while loop assign '\0' to *f
4 - assign temp to f. i.e. beginning of the new string.
--------------------------
5 - No error check for "a" - It could be NULL
6 - No error check for "f" after malloc - memory allocation might fail.

- ashish March 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* cp(char *a)
{
char *f;
int n = strlen(a);
f=(char*)malloc(n*sizeof(char));
while(*a != 0)
{
*f=*a;
f++;
a++;
}
*f = '\0';
f -= n;
cout<< f;
return f;
}

- Shubha Debnath March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
{
public static void Main()
{
string str = "Delhi is Capital of India";
Console.WriteLine("Input");
printaArray(str);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Output");
string[] sarr = str.Split(' ');
int l = sarr.Length;
for (int i = 0; i < l; i++)
{
if (i % 2 == 0)
{
ConvertToCapital(sarr[i]);
}
else
ReverseString(sarr[i]);
}

Console.Read();
}

public static void ConvertToCapital(string str)
{
char[] carr = str.ToCharArray();
for (int i = 0; i < carr.Length; i++)
{
if (carr[i] >= 'a' && carr[i] <= 'z')
carr[i] = (char)((int)carr[i] - 32);
}
printaArray(new string(carr));

}

public static void ReverseString(string str)
{
char[] ch = str.ToCharArray();
int l = ch.Length-1;
char[] temp = new char[l+1];
int i = 0;
while (l >= 0)
{
temp[i++] = ch[l--];
}
printaArray(new string(temp));
}
public static void printaArray(string sarr)
{
Console.Write("{0} ",sarr);
}
}

- sharath April 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
{ public static void Main()
{ string str = "Delhi is Capital of India";
Console.WriteLine("Input");
printaArray(str);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Output");
string[] sarr = str.Split(' ');
int l = sarr.Length;
for (int i = 0; i < l; i++)
{ if (i % 2 == 0)
{ ConvertToCapital(sarr[i]); }
else
ReverseString(sarr[i]); }
Console.Read();
}
public static void ConvertToCapital(string str)
{ char[] carr = str.ToCharArray();
for (int i = 0; i < carr.Length; i++)
{ if (carr[i] >= 'a' && carr[i] <= 'z')
carr[i] = (char)((int)carr[i] - 32); }
printaArray(new string(carr)); }
public static void ReverseString(string str) {
char[] ch = str.ToCharArray();
int l = ch.Length-1;
char[] temp = new char[l+1];
int i = 0;
while (l >= 0) {
temp[i++] = ch[l--]; }
printaArray(new string(temp)); }
public static void printaArray(string sarr) {
Console.Write("{0} ",sarr); } }

- sharath April 03, 2015 | 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