Amazon.com Interview Question
1,589 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer Intern at Amazon.com:
Write a program that reverses the words in a sentence.
See more for this Amazon.com Software Development Engineer Intern Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
0 of 1 people found this helpful
// O(n) times
function reverse1($word)
{
$result = "";
for ($index = strlen($word)-1; $index>=0; $index--)
{
$result[strlen($word)-1 - $index] = $word[$index];
}
return implode("", $result);
}
// O(n/2) times = O(n) times
function reverse2($word)
{
$temp = "";
if(strlen($word)%2 == 1)
$stop = strlen($word)/2;
else
$stop = strlen($word)/2-1;
for ($index = 0; $index<=$stop; $index++)
{
$temp = $word[$index];
$word[$index] = $word[strlen($word)-1 - $index];
$word[strlen($word)-1 - $index] = $temp;
}
return $word;
}
?>
Helpful Answer?
Yes |
No
Inappropriate?
StringBuilder result = new StringBuilder();
Stack<String> stack = new Stack<String>();
String[] temp;
temp = a.split(" ");
for (String s : temp){
stack.push(s);
}
while(!stack.empty()){
result.append(stack.pop() + " ");
}
return result.toString();
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: