Microsoft Interview Question for Software Engineer in Tests


Country: United States




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

You don't have to worry abt spaces. Just reverse the entire string.

public static void revString(char[] charArr, int start , int end){
		while(start < end){
			char tmp = charArr[start];
			charArr[start] = charArr[end];
			charArr[end] = tmp;
			start++;
			end--;
		}
	}

- Say March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You didn't notice that the order of word remained unchanged.

- codewarrior March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You didn't notice that the order of word remained unchanged.

- codewarrior March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Code : Thsi will work for white space
public static void ReverseString()
{
try
{
string str = "this is a pen";
char[] strReverse = str.ToCharArray(0, str.Length);
//char[] str = new char[] { "this is a pen" };

int i = 0;
int j = strReverse.Length - 1;

while (i <= j)
{
char temp = strReverse[i];
strReverse[i] = strReverse[j];
strReverse[j] = temp;

i++;
j--;

Console.WriteLine();
foreach (char strPrint in strReverse)
{
Console.Write(strPrint);
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}

}

OutPut:
nhis is a pet
neis is a pht
neps is a iht
nep is asiht
nep ais siht
nep a si siht
nep a si siht

- Sidhartha March 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why we need to care about the white space?

Here is an implementation on string reversal:

basicalgos.blogspot.com/2012/03/4.html

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

It is very simple to reverse the whole string. But if wang to reverse string and keep every word unchanged, need addition code.

#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
// Reverse characters within lpszStart .. lpszEnd.
void ReverseString(char* lpszStart, char* lpszEnd)
{
    if (lpszStart >= lpszEnd)
    {
        return;
    }

    char* l = lpszStart;
    char* r = lpszEnd;
    while (l < r)
    {
        char ch = *l;
        *l = *r;
        *r = ch;
        ++l;
        --r;
    }
}

// Reverse the whole sentence, but every word is not reversed.
void ReverseSentence(char* lpszSentence)
{
    if (lpszSentence == NULL)
    {
        return;
    }

    char* lpszStart = lpszSentence;
    char* lpszEnd = lpszSentence + strlen(lpszSentence) - 1; // Exclude the null terminator.
    ReverseString(lpszStart, lpszEnd);

    // Then search word and reverse every word.
    lpszStart = lpszSentence;
    lpszEnd = lpszSentence;
    while (true)
    {
        // Forward search blank character.
        while (*lpszEnd != ' ' && *lpszEnd != '\0')
        {
            ++lpszEnd;
        }

        // Now lpszEnd should point to a blank or null terminator.
        ReverseString(lpszStart, lpszEnd-1);

        if (*lpszEnd == '\0')
        {
            break;
        }
        else
        {
            lpszStart = ++lpszEnd;
        }
    }

    printf(lpszSentence);
}

void main()
{
    char sz[] = "hello world";
    ReverseSentence(sz);
    getch();
}

- codewarrior March 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what do you mean by

char* lpszStart = lpszSentence;
char* lpszEnd = lpszSentence + strlen(lpszSentence) - 1;

can you explain?

- unkown March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually the lpszStart points to the first character while lpszEnd points to the last character (not the null termintaor). Here is a bug, if the sentence's first character is blank, it cannot work, and if there are more than one blanks between each word, also cannot work.
I was asked this question today in MSFT interview.

- codewarrior March 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

string rev(string s)
{

    string res = "";
    string cur = "";

    for(int i=s.size()-1;i>=0;i--)
    {
        if(!isspace(s[i]))
        {
            cur = cur + s[i];
        }
        else
        {
            res = res + s[i]; // inorder to push the space character to the result

            for(int j=cur.size()-1;j>=0;j--)
            {
                res = res + cur[j];            
            }
             
            cur = "";
        }
    }
}

- hitman March 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

at the end of the function:

return res;

- hitman March 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string.h>
using namespace std;

int find_end(char* arr,int start)
{
int i;
for(i=start; *(arr+i)!=' ' && *(arr+i)!= '\0' ;i++)
return i;
}
void reverse(char* arr,int start, int end)
{
while(start<end)
{
char temp;
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}


int main()
{
char arr[]="Abhishek Gupta";

int start,end;
for(start=0;*(start+arr)!='\0' && start<strlen(arr); )
{
end=find_end(arr,start);
if(start==end)
break;
reverse(arr,start,end-1);
start=end+1;
}
cout<<arr<<endl;
return 0;

}

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

or you could use the concept of stack..

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

O(n) solution is one parse:
1. set two int start and right be the index of first non-space char so that start==right and s[start]!=' '
2. for(;right< size of string s;right++) do 3
3. if s[right] is space then we reverse s[start...right-1] and set start=right+1

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

private static void ReverseString(char[] input)
        {
            if(input == null || input.Length < 2)
                return;

            var spaces = new Queue<int>();
            spaces.Enqueue(0);
            for (int i = 0; i < input.Length; i++)
            {
                if(input[i] == ' ')
                {
                    spaces.Enqueue(i-1);
                    spaces.Enqueue(i+1);
                }
            }
            spaces.Enqueue(input.Length - 1);

            while (spaces.Count > 0)
            {
                var left = spaces.Dequeue();
                var right = spaces.Dequeue();
                while (left < right)
                {
                    var temp = input[left];
                    input[left] = input[right];
                    input[right] = temp;
                    ++left;
                    --right;
                }
            }
        }

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

You could use split:

public static String reverseWords(String input){

String output = "";

String[] inputList = splitWords(input," ");

for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";

return output;
}

and the implementation of Split:

public static String reverseWords(String input){

String output = "";

String[] inputList = splitWords(input," ");

for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";

return output;
}

- andrei.marfievici May 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You could use split:

public static String reverseWords(String input){

String output = "";

String[] inputList = splitWords(input," ");

for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";

return output;
}

and the implementation of Split:

public static String reverseWords(String input){

String output = "";

String[] inputList = splitWords(input," ");

for(int i = inputList.length-1; i>=0; i--)
output = output + inputList[i] + " ";

return output;
}

- andrei.marfievici May 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static String[] splitWords(String s, String d){

int count = 0;

for (int i = 0; i < s.length(); i++) {

if(s.charAt(i)==d.toCharArray()[0])
count++;
}

String[] output = new String[count+1];

int occurence = 0;
int first = 0;
int last = 0;

for(int i=0;i<s.length();i++){

if(s.charAt(i)==d.toCharArray()[0] && occurence < count){

last = i;
output[occurence] = s.substring(first, last);
first = i + 1;
occurence++;
}
else if(occurence == count){
output[occurence] = s.substring(i,s.length());
break;
}
}



return output;

}

- andrei.marfievici May 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void ReverseSentence(string input)
{
	if (string.IsNullOrEmpty(input))
		throw new Exception("String Empty");

	char[] cArray = input.ToCharArray();
	int i = 0;
	int j = 0;
	while (true)
	{
		while (j < cArray.Length && cArray[j] == ' ')
			j++;
		if (j == cArray.Length)
			break;
		i = j;
		while (j < cArray.Length && cArray[j] != ' ')
			j++;
		ReverseWord(i, j - 1, cArray);
                }
}

public void ReverseWord(int start, int end, char[] inputWord)
{
	char temp;
	while (start < end)
	{
		temp = inputWord[start];
		inputWord[start] = inputWord[end];
		inputWord[end] = temp;
		start++;
		end--;
	}
}

- Andy September 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in-place version:

void word_reverse()
{
        string str = "    a Hello World!    another_word! ab c ";
        char[] array = str.ToCharArray();            
            int word_start = 0;
            int word_end = 0;
            for (word_end = 0; word_end < array.Length; word_end++)
            {
                if (str[word_end] == ' ')
                {
                    Reverse(ref array, word_start, word_end - 1);
                    word_start = word_end + 1;
                    continue;
                }
                else if (word_end == array.Length - 1 && array[word_end] != ' ')
                {
                    Reverse(ref array, word_start, word_end);
                    break;
                }
            }
}

void Reverse(ref char[] array, int start, int end)
        {
            while (end >= start)
            {
                char tmp = array[end];
                array[end] = array[start];
                array[start] = tmp;
                end--;
                start++;
            }
        }

- iman.goodarzi March 08, 2013 | 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