Microsoft Interview Question: Given a string (understood to... | Glassdoor

Interview Question

Software Development Engineer In Test (SDET) Interview Redmond, WA

Given a string (understood to be a sentence), reverse the

  order of the words. "Hello world" becomes "world Hello"
Answer

Interview Answer

2 Answers

1

2 ways.
At the low level: reverse the entire string. 'Hello World' becomes "dlroW olleH". Then reverse each word, becomes "World Hello".
At a higher level: Tokenize the words and push them onto a stack, then pop them out.

dopey on May 6, 2011
0

class Solution {
  public static void main(String[] args) {

    String input = "Hello World this is a string";
    reversestring(input);

  }

  public static void reversestring(String input){

    // Stack stack = new Stack();
    String[] str = input.split(" ");
    for(int i = str.length-1;i>=0;i--)
    {

      System.out.print(" "+str[i]);
    }

  }
}

Rupal on Nov 13, 2018

Add Answers or Comments

To comment on this, Sign In or Sign Up.