Microsoft Interview Question for Software Engineer in Tests


Country: India
Interview Type: Written Test




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

<pre lang="" line="1" title="CodeMonkey12888" class="run-this">char* ReplaceString(const char* str) {

int strlen = 0, nspaces = 0;

while (str[strlen]) {
if (str[strlen] == ' ')
nspaces++;
strlen++;
}

int newLen = strlen + nspaces*2 + 1;
char * dst = new char[newLen];

int srcIndex=0,dstIndex=0;
while (str[srcIndex]) {
if (str[srcIndex] == ' ') {
dst[dstIndex++]='%';
dst[dstIndex++]='2';
dst[dstIndex++]='0';
++srcIndex;
} else {
dst[dstIndex++] = str[srcIndex++];
}
}

/*dst[dstIndex] = str[srcIndex]*/
dst[dstIndex] = '\0';


return dst;
}</pre><pre title="CodeMonkey12888" input="yes">
</pre>

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

Why use a new array. You can do it at the original array

public static void ReplaceFun(char[] str, int length) {
int spaceCount = 0, newLength, i = 0;
for (i = 0; i < length; i++) {
if (str[i] == β€˜ β€˜) {
spaceCount++;
}
}
newLength = length + spaceCount * 2;
str[newLength] = '\0';
ν―€τ€€‚τ€€ƒτ€€„τ€€…τ€€†τ€€‡τ€€ˆτ€€‰τ€€‡τ€€†τ€€Šτ€€ƒτ€€‹τ€€Œτ€€τ€€τ€€τ€€Žτ€€τ€€τ€€‘τ€€’τ€€‚τ€€ƒτ€€„τ€€…τ€€†τ€€‡τ€€ˆτ€€‰τ€€‡τ€€†τ€€Šτ€€ƒτ€€‹τ€€Œτ€€τ€€τ€€τ€€Žτ€€τ€€τ€€‘τ€€’τ€€‚τ€€ƒτ€€„τ€€…τ€€†τ€€‡τ€€ˆτ€€‰τ€€‡τ€€†τ€€Šτ€€ƒτ€€‹τ€€Œτ€€τ€€τ€€τ€€Žτ€€τ€€τ€€‘τ€€’τ€€‚τ€€ƒτ€€„τ€€…τ€€†τ€€‡τ€€ˆτ€€‰τ€€‡τ€€†τ€€Šτ€€ƒτ€€‹τ€€Œτ€€τ€€τ€€τ€€Žτ€€τ€€τ€€‘τ€€’str[newLength - 1] = β€˜0’str[newLength - 1] = β€˜0’dsdc
for (i = length - 1; i >= 0; i--) {
if (str[i] == β€˜ β€˜) {
str[newLength - 1] = β€˜0’;
str[newLength - 2] = β€˜2’;
str[newLength - 3] = β€˜%’;
newLength = newLength - 3;
} else {
str[newLength - 1] = str[i];
newLength = newLength - 1;
}
}
}

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

To edit the same string :

int main()
{
char str[1000];

while (cin.getline(str, 100))
{
int l=strlen(str),i,j;
for(i=0;i<l;i++)
{
str[l]='0';
if(str[i]==' ')
{
for(j=l-1;j>i;j--) str[j+2]=str[j];
str[i]='%';str[++i]='2';str[++i]='0';
l+=2;
}
}
str[l]='\0';
cout<<"Output : "<<str<<endl;
}
return 0;
}

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

Isn't '%20' is a space?

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

public static String replace2(String s){
    	char[] temp = s.toCharArray();
    	StringBuffer sb = new StringBuffer();
    	for(int i=0;i<temp.length;i++){
    		if(temp[i]==' '){
    			sb.append("%20");
    		}else{
    			sb.append(temp[i]);
    		}
    	}
    	return sb.toString();
    }

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

<pre lang="" line="1" title="CodeMonkey91764" class="run-this">int length=strlen(str);
for(int i=0;i<length;i++)
{
if(str[i]==' ')
{
length = length+2;
str[i]='%';
for(int j=length+1;j>i+2;j--)
str[j]=str[j-2];
str[i+1]='2';
str[i+2]='0';
}
}
str[length]='\0';

</pre><pre title="CodeMonkey91764" input="yes">
</pre>

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

StringBuffer sb = new StringBuffer("This is a test string");

for(int i=0;i<sb.length();i++)
{
if(sb.charAt(i) == ' ')
{
String str1 = sb.substring(i+1, sb.length());
sb.delete(i, sb.length());
sb.append("%20");
sb.append(str1);
}
}

System.out.println(sb);

- sarath October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//C#
static char[] ReplaceWithString(char[] _str)
        {

            int whiteSpaceCount = 0;
            for (int i = 0; i < _str.Length; i++)
            {
                if (_str[i] == ' ')
                {
                    whiteSpaceCount += 1;
                }
            }
            char[] replacedString = new char[_str.Length + ((whiteSpaceCount * 3) - whiteSpaceCount)];

            for (int i = 0,j =0; i < replacedString.Length; i++)
            {
                if (!(_str[j] == ' '))
                {
                    replacedString[i] = _str[j];
                }
                else 
                {
                    replacedString[i] = '%';
                    replacedString[++i] = '2';
                    replacedString[++i] = '0';
                }
                j++;
            }


            return (replacedString);
        }

- anup.h.nair December 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For java, Can we not use String methods? Like str.replaceAll("\\s", "\\%20"). Sorry I am new to this forum, please correct me if am wrong.

- Kalpana March 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, You can however the idea is not to use Standard library functions. Additionally, this will become a Java specific solution.

- Piyush September 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about this solution ?. I think it's easier than looping twice on the string

public String replace(String str)
	{
		if(str == null)
			return null;
		char[] word = str.toCharArray();
		String temp="";
		for(int i = 0 ; i < word.length; i++)
		{
			if(word[i] == ' ')
				temp += "%20";
			else
				temp +=word[i];
		}
		return temp;
	}

- Yasmin Fathy December 08, 2014 | 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