Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
8
of 10 vote

Reverse the individual strings before the white space
Example :
I am boy
I ma yob

after reverseral of each individual string reverse the entire string
o/p
boy am I
Example 2

Hello Life
a) olleH efiL
b) Life Hello
Note :
In place algorithm..No extra space needed...

- coding for fun February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agreed.

#include <cstdlib>
#include <cstring>
#include <iostream>

void reverse(char *src, char *end)
{
    while(src < end) {
        char temp = *src;
        *src++ = *end;
        *end-- = temp;
    }
}

void reverse_sentence(char *src, size_t len)
{
    if(len < 3) {
        return;
    }
    reverse(src, src + len - 2);
    char *end = src + len - 1;
    while(src != end) {
        char *start = src;
        for(; start < end && *start != ' '; ++start)
            ;
        reverse(src, start - 1);
        for(;start < end && *start == ' '; ++start)
            ;
        src = start;
    }
}

int main()
{
    char c[] = "hey there world.";
    std::clog << "before reversing  " << c << std::endl;
    reverse_sentence(c, std::strlen(c));
    std::clog << "after reversing  " << c << std::endl;
    return (EXIT_SUCCESS);
}

- smffap March 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
7
of 7 vote

1> Push individual words (like Hello,life) into stack
2> Just pop it..

- vips February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the solution which the interviewer is looking for... nice one :)

- chamy50 July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no need of additional data structures, it can be done in place. In your approach u need to create words from the sentence and then place it into stack. So much effort may not be required. We can reverse the complete sentence and then reverse every word in the output of the sentence reversal step.

- crystal.rishi2 October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In-place implementation of the reverse sentence words. Algorithm description inlined in s/c.

#include <stdio.h>
#include <string.h>

void rev_string(char *dat, int len)
{
	char *ps, *pe, tmp;

	if (1 == len)
		return;

	ps = dat; // the start of the data
	pe = dat + len - 1; // the end (last char) of the data

	while (ps < pe) {
		tmp = *ps;
		*ps = *pe;
		*pe = tmp;
		ps++;
		pe--;
	}    
}

/** 
    Do an in-place reversing of the words in sentence,eg.
    "1234 56  78 9" -> "9 78  56 1234" 

    Algorithm:
    1. Reverse the whole string
    2. Scan the resulting sentence from left to right and:
    a) Find each token (word delimited by whitespace)
    b) Reverse each token
    c) Loop back to a) while there are more tokens 
*/
void rev_sentence_words(char dat[], int len)
{
	int i, j;
	char *ps;

	rev_string(dat, len);

	for (i = 0; i < len; i++) {
		if (dat[i] == ' ')
			continue;
		j = i;
		ps = &dat[j];

		/* Find the next whitespace */
		while (dat[i] && dat[i] != ' ')
			i++; 
		rev_string(ps, i - j);
	}
}

int main(int argc, char* argv[])
{
	char dat[] = " the careercup just rocks ";

	printf("Before: <%s>\n", dat);
	rev_sentence_words(dat, strlen(dat));
	printf("After: <%s>\n", dat);

	return 0;
}

- ashot madatyan March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This should be in place otherwise it will be trivial.

- Rayden February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take another array of same size.
push each word at the end of the new array.
Given Array.am i right
Result Array.

1.            am
2.        i  am 
3. right i am

- SantiagoYMG March 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char* start, char* end)
{
while (start < end)
{
char tmp = *end;
*end = *start;
*start = tmp;

start++;
end--;
}
};

int main()
{
char str[] = "I love you";
char* start = str;
char* end = str + 1;
while(end <= start + 10)
{
if(*end == '\0' || *end == ' ')
{
reverse(start, end - 1);
start = end + 1;
}
end++;
}
reverse(str, str + 9);
printf("%s", str);
return 0;
}

- hl March 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>

void reverse(char *a, int i, int j)
{
if(i>j)
{
int temp=i;
i=j;
j=temp;
}
char temp;
while(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
}

void reversesentence(char * a, int length) //assumption
{
int i=0;
int j=0;
while(j<length-1)
{
while(a[j]==' '&&j<length-1)
{
j++;
i=j;
}
while(a[j]!=' '&&j<length-1)
{
j++;
}
if(a[j]==' ')
{
j--;
}
reverse(a,i,j);//first blank
j++;
i=j;
}
reverse(a,0,length-1);
}

void main()
{
char a[]=" i am lucky ";
reversesentence(a,strlen(a));
puts(a);
}

- luckycat March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char str[] = "Hello World";
char str2[] = "";
int i;
for(i=strlen(str)-1; i<=0; i--)
{
  if(str[i]==' ')
  {
    strcat(str2,str+i+1);
    strcat(str2," ");
    str[i]=0;  // cut
  }
}
// first that becomes last
strcat(str2,str);
printf("\n str: %s", str2);

- CheckThisResume.com March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops typo >=
for(i=strlen(str)-1; i>=0; i--)

- CheckThisResume.com March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//let input be the line read from any form input method you can think of e.g. sc or filereader

String[] array = input.split(" ");
for(int x = array.length()-1; x >=0; x--){
System.out.print(array[x] + " ");
}
System.out.println();

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

Reverse words, reverse sentance algo is great.
Here is another:
Time: O(2n)
Space: (On)

private static char[] ReverseWordsInSentance(char[] input)
        {
            if (input == null || input.Length < 2) return input;
            var output = new char[input.Length];
            var outputRunner = 0;
            var latestLimit = input.Length;
            for(int i = input.Length - 1; i >= -1; --i)
            {
                if(i==-1 || input[i] == ' ')
                {
                    for (int j = i + 1; j < latestLimit; ++j)
                    {
                        output[outputRunner++] = input[j];
                    }
                    if(i != -1)
                    {
                        latestLimit = i;
                        output[outputRunner++] = ' ';
                    }
                }
            }
            return output;
        }

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

import java.util.ArrayList;

public class StringToStringArray {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		char[] a = "Hello World How are you".toCharArray();
		ArrayList<String> myArrayList = new ArrayList<String>();
		boolean isFinalCharSpace = false;

		//get String array from Char array
		StringBuilder sb = new StringBuilder();
		int j = 0;
		for (int i = 0; i < a.length; i++) {

			isFinalCharSpace = false;
			if (a[i] != ' ') {
				sb.append(a[i]);
			} else if (a[i] == ' ') {
				if (sb.length() > 0) {
					myArrayList.add(sb.toString());
				} else {
					myArrayList.add(" ");
				}
				j++;
				sb.setLength(0);
				isFinalCharSpace = true;
			}
		}

		if (!isFinalCharSpace) {
			myArrayList.add(sb.toString());
			j++;
			sb.setLength(0);
		}

		int length = myArrayList.size();

		//swap the corresponding elements
		for (int k = 0; k < length / 2; k++) {
			String temp = myArrayList.get(k);
			myArrayList.set(k, myArrayList.get(length - 1 - k));
			myArrayList.set(length - 1 - k,temp);
		}

	}

}

- sid_tintin May 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

reverse the whole string , put in the stack , pop it out word by word

- Anonymous June 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse the whole sentence and then reverse the output word by word
CODE :

void reverseWordByWord (char *str) {
	char *start = str;
	int len = strlen(str);
	char *end = str+len-1;

	reverse(start, end);
	int i;
	for (i=0; i<len; i++) {
		if (str[i]== ' ') {
			end = &str[i-1];
			reverse(start,end);
			start = &str[i+1];
		}
	}
	//Reverse the last word !
	end = &str[i-1];
	reverse(start,end);

}

- crystal.rishi2 October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.


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