Amazon Interview Question for Software Engineer / Developers






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

first reverse the whole string
then reverse all the chars in the words alone..
eg., sandman is coming
first step: gnimoc si namdnas
sec step: coming is sandman

- ssn October 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, same solution on Pearl book.

- Saga Yao March 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In perl:
$line=<>;
@arr=split(" ",$line);
reverse(@arr);
print @arr;

- Anonymous October 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string reverseSENTENCE(string p)
{
   
   reverse(p.begin(),p.end());
   
   cout<<p<<"\n";
   
   int prev=0,curr,i;
   for(i=0;i<p.size();i++)
   {
      
      if(p[i]==' ')
      {
      reverse(p.begin()+prev,p.begin()+i);
      prev=i+1;
      }
           
      
      }
      
      reverse(p.begin()+prev,p.end());
      
      return p;
      
      }

- Anonymous July 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the working version of the algorithm in C to do word reversals in a string/sentence.

/*
   Algorithm..
   1. Reverse whole sentence first.
   2. Reverse each word individually.
   All the reversing happens in-place.
*/
#include <stdio.h>
void rev(char *l, char *r);
int main(int argc, char *argv[])
{
    char buf[] = "the world will go on forever";
    char *end, *x, *y;
    // Reverse the whole sentence first..
   for(end=buf; *end; end++);
   rev(buf,end-1);
   // Now swap each word within sentence...
   x = buf-1;
   y = buf;
   while(x++ < end)
   {
      if(*x == '\0' || *x == ' ')
      {
        rev(y,x-1);
        y = x+1;
      }
   }
   // Now print the final string....
   printf("%s\n",buf);
   return(0);
}
// Function to reverse a string in place...
void rev(char *l,char *r)
{
   char t;
   while(l<r)
   {
      t     = *l;
      *l++ = *r;
      *r-- = t;
   }
}

- klpd September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what the crap,... question is asking to reverse the words of a string not the string itself. like "It is a dog." should be "tI si a god."

- kk November 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

perhaps you should brush up on your English comprehension skill too, homie!

- anon January 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One of the most efficient methods is to use a stack for it, push all the characters in the given string from the end position and when there is any space encountered pop all the characters from the stack and concatenate them together along with space at the end to form a string and keep repeating the step until the stack is empty.

Imlementation:

void reversewords(string str){
stack<char> s;
string st = "";
for(int i = str.length() - 1; i >= 0; i--){
if(str[i] == ' '){
while(!s.empty()){
char c = s.top();
s.pop();
st += c;
}
st += str[i];
}
else
s.push(str[i]);
}
while(!s.empty()){
char d = s.top();
s.pop();
st += d;
}
cout<<st<<endl;
}

- swapnilkant11 June 02, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 vote

First split the whole string into an string array, the array should be dynamic.Then run the loop up to the length of the array and reverse each cell one by one.

- Qasim Khan November 10, 2008 | 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