Microsoft Interview Question: Write a function (in your pre... | Glassdoor

Interview Question

New Graduate - Software Development Engineer In Test Interview Redmond, WA

Write a function (in your preferred language C, C++, or

  Java) that will take the given string and return the reverse. (ie. input:"abc def ghi" output:"ihg fed cba") Optimize the above code (if possible). Now knowing that the original function you wrote in pt1 is being used by other programs and cannot be modified write another function that when called will utilize the function from pt1 to only reverse the words in the sentence. (ie. input:"abc def ghi" output:"ghi def abc") Test your code.

Interview Answer

2 Answers


public class TestRun2 {
    public static void main(String[] args) {
            String inputString = "abc def ghi";

            // now reverse the words

    public static String reverseString(String input) {
        char[] inputCharArray = input.toCharArray();

        char[] returnCharArray = new char[inputCharArray.length];
        for(int i=0; i

Benny on Dec 17, 2012

string reverseString(string str)
   int i = 0;
   while(i < str.size()/2)
      char temp = str[i];
      str[i] = str[str.size() - i - 1];
      str[str.size() - i - 1] = temp;
   return str;
int main(int argc, char *argv[])
   string temp = "abc def ghi";
   cout << temp << endl;
   cout << reverseString(temp) << endl;

To do the second part, give the algorithm each word, and concatenate them.

Anonymous on Jan 7, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.