Microsoft Interview Question Developer Program Engineers

  • microsoft-interview-questions
    0
    of 0 votes
    18
    Answers

    Remove whitespace characters in a string, in place and with out shifting

    - irraju on July 29, 2012 in India Report Duplicate | Flag
    Microsoft Developer Program Engineer

Country: India


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

char s[] = "how are     you";
	int n = strlen(s);
	int i=0;
	while(s[i] != '\0')
	{
		if(s[i] == ' ')
			s[i] = 7; // ascii for bell
		i++;
	}
	cout << s << endl;

- Anonymous on July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

instead of 7 you can use character "audible" i.e..'\a'...
when display receives this character it will not get printed
s[i] = '\a'; // ascii for audible

- Anonymous on July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about we fill the white spaces with null character('\0') and concatenate the sub strings.. plzz comment

- Anonymous on July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

concatenation of 2 strings requires O(m+n) extra space. so this approach is not inplace.

- sandeep on July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No we are not using extra space. we are splitting the string into substrings and concatenating at the same time.

- Anonymous on July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok...my bad..

- Anonymous on July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class string // Just deleting character
{
public static void main(String[] args)
{
String x="teja is a good boy";
x=x.replaceAll(" ","");
System.out.println(x);
}
}

- TEJA on July 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think in place and without shifting is not possible together...any clues?

- Victor on July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string remBlank(std::string input){
std::string newstring;
for(int i=0;i<input.length();i++){
if(input.at(i)==32){
newstring+="%20"; }
else{newstring+=input.at(i);} }
return newstring;}

- PRAJWALPRAS on August 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about this code.... correct me if i am wrong...!!
int main()
{
char s[] = "how are you friends";
int i=0;
while(s[i] != '\0')
{
if(s[i] == ' ' && s[i+1]==' ')
{ s[i+1] = '\b'; i++; }
else if(s[i]== ' ') s[i]='\a';
i++;
}
printf("%s\n", s);
}

- Atul on August 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "how are you";
int n = str.Length;
int i=0;

char[] s = str.ToCharArray();

Console.WriteLine("Original string : " + str);
for (i = 0; i < n; i++)
{
if (s[i] == ' ')
s[i] = '\a'; // ascii for bell

}

Console.Write("String w/o space : ");
for (i = 0; i < n; i++)
Console.Write(s[i]);

Console.Read();
}
}
}

- Amrita on August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void removeSpace(char[] string) {
		if (string == null || string.length == 0) {
			return;
		}

		int p1 = 0;
		int p2 = 0;

		do {
			while (p2 < string.length && string[p2] == ' ') {
				p2++;
			}
			if (p2 == string.length) {
				break;
			}
			string[p1++] = string[p2++];
		} while (p1 < string.length && p2 < string.length);

		for (int p = p1; p < string.length; p++) {
			string[p] = 0;
		}

		for (char c : string) {
			System.out.print(c + " ");
		}
		
		System.out.println();
	}

- Kevin on March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

fun(char *a)
{
for(i=j=0;a[i];i++)
if(a[i]!=' ')a[j++]=a[i];
a[j]='\0';
}

- xyz on July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is wrong with this code pls explain

- xyz on July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

logic correct, but question given than without shifting... otherwise it is very easy question not a MS question

- Anonymous on July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous talking about easy/MS: What does the question exactly mean? Care to elaborate?

- Anonymous on July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are not allowed to shift the characters from one location to another. here if there is space you are shifting the character from i to i-1 location... so total length of the string will be decreased but expected is should be of same length.

- Anonymous on July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What does not shifting even mean? Can I shift from i+100 to i?

Ridiculous question.

A simpler way would be to ask for an O(n) time, in-place algorithm. Which I suspect it was, but OP managed to screw up.

- Anonymous on July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

one possible but fullish way is to use linked list instead of char array...... here we can delete the node with whitespace....

not a answer but a possible way :)

- harry8906 on July 30, 2012 | Flag


Add a Comment
Name:

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

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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