Amazon Interview Question
Web DevelopersTeam: AWS
Country: United States
Interview Type: Phone Interview
public class WordReverse {
public static String reverseWords(String startString){
String endString = "";
String[] startArray = startString.split(" ");
for (int i = startArray.length - 1; i >= 0; i--){
endString += startArray[i];
if (i != 0){
endString += " ";
}
}
return endString;
}
public static void main(String[] args){
String s = "these are words that will be reversed";
System.out.println(s);
System.out.println(reverseWords(s));
}
}
Where the hell did you even mention javascript? be more percise next time before downvoting me, asshole.
Here is the answer in Ruby:
require 'pry'
def reverse_sentence(sentence)
sent_arr = sentence.split(' ')
reversed = []
sent_arr.each do |word|
reversed.unshift(word)
end
reversed.join(' ')
end
print reverse_sentence('Hi how are you? Hi how are you? Hi how are you? Hi how are you?')
- Alex M.
The interviewer said it was exactly what they were looking for.
- Kevin.Pheasey April 10, 2013