Microsoft Interview Question for Software Engineer / Developers


Country: Europe
Interview Type: Phone Interview




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

steps
1> reverse your string
2> swap first word with last word increment start and decrement end

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

void ReverseWords(string &s)
{
	int start = 0;
	for (int i = 0; i < s.size(); ++i) {
		if (s[i] == ' ') {
			start = i + 1;
		} else {
			if (i + 1 == s.size() ||
				s[i + 1] == ' ')
			{
				reverse(s.begin() + start, s.begin() + i + 1);
			}
		}
	}
}

- Alex November 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int slow = 0, fast = 0;

for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if(c == ' '){
fast = i - 1;
reverse(str, slow, fast);
slow = i + 1;
}
}
reverse(str, slow, str.length() - 1);

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

char *reverse(char *s) {
  char *l, *r, *s_orig = s;

  while (*s) {
    while(*s && *s == ' ') s++;
    l = s;
    while(*s && *s != ' ') s++;
    r = s-1;

    for (; l < r; l++, r--) {
      char c = *l; *l = *r; *r = c;
    }
  }

  return s_orig;
}

- sergey.v.zimin November 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) -

public static void main(String[] args) {
    String str = "the brown fox ran fast";
    reverse(str);
  }
  
  public static void reverse(String str){
    int n = str.length()-1;
    char[] carr = str.toCharArray();
    Stack<String> stack = new Stack<String>();
    
    String temp = "";
    while(n >= 0){
      if(carr[n] == ' '){
          stack.push(temp);
      	  temp = "";	
      }else{
      	temp += carr[n];
      }
      n--;
    }
    stack.push(temp);
    
    while(!stack.isEmpty()){
    	System.out.print(stack.pop() + " ");
    }
  }

- sudip.innovates November 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution I proposed in C#:

public static void Main(String[] args){
            string sentence= "the brown fox ran fast";
            string [] words=sentence.Split(' ');
            sentence="";

            for(int i=0;i<words.Length;i++)
            {
                sentence+=ReverseWord(words[i]);
                if(i!=words.Length-1) sentence+=" ";
            }
            Console.WriteLine(sentence);
        }

        public static string ReverseWord(string word)
        {
           string output="";
           for(int i=0;i<word.Length;i++)
           {
               output+=word[word.Length-i-1];
           }
           return output;
        }

- Leo November 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution I proposed in C#:

public static void Main(String[] args){
            string sentence= "the brown fox ran fast";
            string [] words=sentence.Split(' ');
            sentence="";
          
            for(int i=0;i<words.Length;i++)
            {
                sentence+=ReverseWord(words[i]);
                if(i!=words.Length-1) sentence+=" ";
            }
            Console.WriteLine(sentence);
        }

        public static string ReverseWord(string word)
        {
           string output="";
           for(int i=0;i<word.Length;i++)
           {
               output+=word[word.Length-i-1];
           }
           return output;
        }

- Leo November 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution I proposed in C#:

public static void Main(String[] args){
            string sentence= "the brown fox ran fast";
            string [] words=sentence.Split(' ');
            sentence="";
          
            for(int i=0;i<words.Length;i++)
            {
                sentence+=ReverseWord(words[i]);
                if(i!=words.Length-1) sentence+=" ";
            }
            Console.WriteLine(sentence);
        }

        public static string ReverseWord(string word)
        {
           string output="";
           for(int i=0;i<word.Length;i++)
           {
               output+=word[word.Length-i-1];
           }
           return output;
        }

- rignanese.leo November 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution in Java:

static String reverseAnyWords(String initialString) {
  StringBuilder reversedString = new StringBuilder();

  StringBuilder word = new StringBuilder();
  for(int ii = 0; ii < initialString.length(); ii++) {  // O(N)
    if (!Character.isSpaceChar(initialString.charAt(ii))) {  // O(1)
      word.append(initialString.charAt(ii));
    }
    else {
      if (word.length() != 0) {
        reversedString.append(word.reverse());
        word.delete(0, word.length());
      }
      reversedString.append(initialString.charAt(ii));
    }
  }

  // Last piece
  if (word.length() != 0) {
    reversedString.append(word.reverse());
    word.delete(0, word.length());
  }

  return reversedString.toString();
}

- Mike L November 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string str = "the brown fox ran fast";
            string output = "";
            string rev = "";
            foreach (char ch in str)
            {
                if (ch != ' ')
                    rev = ch.ToString() + rev;
                else
                {
                    rev += ch.ToString();
                    output += rev;
                    rev = "";
                }
            }

            output +=  rev;

- My solution in c# November 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string str = "the brown fox ran fast";
            string output = "";
            string rev = "";
            foreach (char ch in str)
            {
                if (ch != ' ')
                    rev = ch.ToString() + rev;
                else
                {
                    rev += ch.ToString();
                    output += rev;
                    rev = "";
                }
            }

            output +=  rev;

- My solution in C# November 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void ReverseSentence(String str){
        String[] arr=str.split(" ");
        
        //start tranversing through array
        for(int i=0; i<arr.length; i++){
            StringBuilder rev=new StringBuilder(arr[i]);
            System.out.print(rev.reverse()+" ");
        }
    
    System.out.println();
    }

- In Java by using StringBuilder capabilities. November 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function reverStringWords($str){
    $input_array = explode(' ', $str);
    foreach($input_array as $index => $word){
        $characters = str_split($word);
        $limit = floor(count($characters) / 2);
        for($i = 0; $i < $limit; $i++){
            $new_index = count($characters) - $i -1;
            
            // Swap.
            $tmp = $characters[$i];
            $characters[$i] = $characters[$new_index];
            $characters[$new_index] = $tmp;
        }
        
        $input_array[$index] = implode('', $characters);
    }
    print_r(implode(' ', $input_array));
}
reverStringWords('Hello World!');

- antonio.safwat November 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def reverse(s):
	return (' '.join(s.split()[::-1]))

- Anonymous November 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
Stack<Character> stack = new Stack<Character>();
String s = new String("the brown fox ran fast");
char[] chararr = s.toCharArray();
StringBuffer sb = new StringBuffer();
for(char c : chararr) {
if(c != ' ') {
stack.push(c);
} else {
while(!stack.isEmpty()) {
sb.append(stack.pop().toString());
}
sb.append(' ');
}
}
while(!stack.isEmpty()) {
sb.append(stack.pop().toString());
}
System.out.print(sb);
}

- YapFei March 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
Stack<Character> stack = new Stack<Character>();
String s = new String("the brown fox ran fast");
char[] chararr = s.toCharArray();
StringBuffer sb = new StringBuffer();
for(char c : chararr) {
if(c != ' ') {
stack.push(c);
} else {
while(!stack.isEmpty()) {
sb.append(stack.pop().toString());
}
sb.append(' ');
}
}
while(!stack.isEmpty()) {
sb.append(stack.pop().toString());
}
System.out.print(sb);
}

- YapFei March 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
Stack<Character> stack = new Stack<Character>();
String s = new String("the brown fox ran fast");
char[] chararr = s.toCharArray();
StringBuffer sb = new StringBuffer();
for(char c : chararr) {
if(c != ' ') {
stack.push(c);
} else {
while(!stack.isEmpty()) {
sb.append(stack.pop().toString());
}
sb.append(' ');
}
}
while(!stack.isEmpty()) {
sb.append(stack.pop().toString());
}
System.out.print(sb);
}

- YapFei March 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String a[])
{

String string="The brown fox ran fast abc 12345 .\\nys>{}?[[]";

StringTokenizer strs = new StringTokenizer(string," ");
List<String> stringList =new ArrayList<>();
String strintToPrint="";
while (strs.hasMoreElements())
{
stringList.add(strs.nextToken());
}

Collections.reverse(stringList);

for(String str : stringList){

strintToPrint+=new StringBuilder().append(str).reverse().toString()+" ";
}

System.out.println(strintToPrint.trim());

}

- Anonymous May 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

xpublic static void main(String a[])
	{
		
		String string="The brown fox ran fast abc 12345 .\\nys>{}?[[]";
		
		StringTokenizer strs = new StringTokenizer(string," ");
		List<String> stringList =new ArrayList<>();
		String strintToPrint="";		
		while (strs.hasMoreElements())
		{
			stringList.add(strs.nextToken());		
		}
		
		Collections.reverse(stringList);
		
		for(String str : stringList){
					
			strintToPrint+=new StringBuilder().append(str).reverse().toString()+" ";
		}
		
		System.out.println(strintToPrint.trim());
		
	}

- Anonymous May 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String a[])
{
String string="The brown fox ran fast abc 12345";
StringTokenizer strs = new StringTokenizer(string," ");
List<String> stringList =new ArrayList<>();
String strintToPrint="";
while (strs.hasMoreElements())
{
stringList.add(strs.nextToken());
}
Collections.reverse(stringList);
for(String str : stringList){
strintToPrint+=new StringBuilder().append(str).reverse().toString()+" ";
}
System.out.println(strintToPrint.trim());
}

- Anonymous May 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String string="The brown fox ran fast abc 12345 .\\nys>{}?[[]";
StringTokenizer strs = new StringTokenizer(string," ");
List<String> stringList =new ArrayList<>();
String strintToPrint="";
while (strs.hasMoreElements())
{
stringList.add(strs.nextToken());
}
Collections.reverse(stringList);
for(String str : stringList){
strintToPrint+=new StringBuilder().append(str).reverse().toString()+" ";
}
System.out.println(strintToPrint.trim());

- Anonymous May 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void WordsInSentenceReverse(String str){
        StringBuilder sbFinal = new StringBuilder();
        for(String s : str.split(" ")){
            sbFinal.append(new StringBuilder(s).reverse() + " ");
        }
        System.out.println(sbFinal.toString().trim());
    }

- Bukary Kandeh May 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# implementation. Complexity O(n):

static string Transform(string str) {
            var sb = new StringBuilder(str.Length);
            var cursor = 0;

            for (var i = 0; i < str.Length; i++) {
                if (char.IsLetter(str[i])) {
                    sb.Insert(cursor, str[i]);
                }
                else {
                    sb.Append(str[i]);
                    cursor = i + 1;
                }
            }
            
            return sb.ToString();
    }

- ersha June 26, 2018 | 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