Amazon Interview Question for Quality Assurance Engineers


Team: Amazon Wireless
Country: India
Interview Type: Written Test




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

string reverse(string s) {
		int length = s.Length();
		char[] a = s.toCharArray();
		for(int i=0, j = length - 1; i<=j; i++, j--) {
			char temp = a[i];
			a[i] = a[j];
			a[j] = temp;
		}
		return a.toString();
	}
	
	List<string> reverseWords(string s) {
		List<string> result = new List<string>();
		string[] words = s.split(" ");
		foreach(word in words) {
			result.add(reverse(word));
		}
		return result;
	}

- tus124 July 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What do the interviewer exactly mean by do write program without using any in-built functions?

Is it inclusive of all the library functions like substring, charat,.. and so on or that would be only to avoid using the functions like 'sort' or 'reverse' which would help us in achieving the given problem in a few lines?

- GK July 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

package javaProgramming;

public class Test1 {

public static void main(String[] args) throws Exception{
String input = "This is an example";
String [] array = input.split(" ");
StringBuilder sb = new StringBuilder();

for (int i=0;i<array.length;i++){
char [] temp = new char[array[i].length()];
for(int j=0;j<temp.length;j++){
temp[j] = array[i].charAt(temp.length - 1 - j);
}
sb = sb.append(new String(temp)).append(" ");
}

System.out.println("Output for "+input+" is : "+sb);
}
}

- Anonymous August 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void reverseWords(String str){
		StringBuilder string = new StringBuilder(str);
		
		Matcher matcher = Pattern.compile("\\w+").matcher(string);
		while(matcher.find()){
			reverseStr(string, matcher.start(), matcher.end()-1);
		}

                System.out.println(string);
	}
	
	
	private static void reverseStr(StringBuilder str, int i, int j){
		while(i<j){
			char tmp = str.charAt(i);
			str.setCharAt(i, str.charAt(j));
			str.setCharAt(j, tmp);
			i++;
			j--;
		}
	}

- X July 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

string = "This is an example"

x = []
y = ""

for i in range(1, len(string)+1):
    j = -1*i
    y = y + string[j]

#print (y)
k = ''
for i in range(0, len(string)):
    
    if string[i] != ' ':
        k = k +string[i]
    if (string[i] == ' ') or (i == (len(string)-1)):
        x.append(k)
        k = ''
#print (x)

z = []
q = ''
for j in range(0, len(x)):
    t = str(x[j])
    for p in range(1,len(t)+1):
        p = -1*p
        q = q + t[p]
    z.append(q)
    q = ''
#print (z)  


rev_str = ''
for g in range(0,len(z)):
    rev_str = rev_str + ' ' + str(z[g])

print (rev_str.lstrip())

- Prashant Shukla April 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverseEachWord(string& str, int start, int end)
{
    while(start<end)
    {
        swap(str[start++], str[end--]);
    }
}
    
void reverseWord(string& str)
{
    int len = str.length();
    int start = 0, end = 0;
    for(int i=0;i<len;i++)
    {
        start = i;
        end = i;
        while(i< len && str[i] != ' ')
        {
            end++;
            i++;
        }
        reverseEachWord(str, start, end-1);
        if(str[i] == ' ')
            continue;
    }
}

- skum July 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
void reverseEachWord(char str[], int start, int end)
{char t;

while(start<end)
{
t=str[start];
str[start]=str[end];
str[end]=t;
start++;
end--;
}
}

char* reverseWord(char str[])
{
int len = strlen(str),i;
int start = 0, end = 0;
for( i=0;i<len;i++)
{
start = i;
end = i;
while(i< len && str[i] != ' ')
{
end++;
i++;
}
reverseEachWord(str, start, end-1);
if(str[i] == ' ')
continue;
}
return str;
}
int main()
{
char a[100];
gets(a);
printf("%s ",reverseWord(a));

- Rahul July 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is critical that you divide by 2 or you will get the same word, and you don't have to worry about the median or mid character

function reverseChars(char[] strArray) {
 

     if ( strArray.length < 2 ) return;

     int mid = strArray.length/2;
     for ( int i = 0; i < mid; ++i ) {
         char tmp = strArray[i];
         strArray[i] = strArray[strArray.length-i-1];
         strArray[strArray.length-i-1] = tmp;
     }
}

- stephenpince July 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
void reverseEachWord(char str[], int start, int end)
{char t;

while(start<end)
{
t=str[start];
str[start]=str[end];
str[end]=t;
start++;
end--;
}
}

char* reverseWord(char str[])
{
int len = strlen(str),i;
int start = 0, end = 0;
for( i=0;i<len;i++)
{
start = i;
end = i;
while(i< len && str[i] != ' ')
{
end++;
i++;
}
reverseEachWord(str, start, end-1);
if(str[i] == ' ')
continue;
}
return str;
}
int main()
{
char a[100];
gets(a);
printf("%s ",reverseWord(a));

}

- Rahul July 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String reverse(String seg)
  {
    int length=seg.length();
    char[] cTemp=seg.toCharArray();
    for(int front=0, back=length-1;front<=back;front++,back--)
     {
       char ctemp1=cTemp[front]; 
       cTemp[front]=cTemp[back];
       cTemp[back]=ctemp1;
     }
     String s=new String(cTemp);
     return s;
  }
 String reverseWords(String Words)
 { 
   String[] words1=Words.split(" ");
   String ans="";
   for(String Temp:words1){
      ans=ans+" "+reverse(Temp);
   } 
   return ans;
 }

- vijayarahuraman5 July 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java there are two ways:

1:
String array[] = str.split(" ");


for (int i = 0; i < array.length; i++) {
StringBuffer buffer = new StringBuffer();
buffer = buffer.append(array[i]);
buffer = buffer.reverse();
array[i] = buffer.toString();
System.out.print(array[i] + " ");
}



2 :



String array[] = str.split(" ");

for (int i = 0; i < array.length; i++) {
StringBuffer buffer = new StringBuffer();

for (int j = array[i].length() - 1; j >= 0; j--)
{
buffer =buffer.append(array[i].charAt(j));

}
array[i] = buffer.toString();
System.out.print(array[i]+" ");

- Rishbah Tiwari July 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String array[] = str.split(" ");
//There are two ways
//first
		
		  for (int i = 0; i < array.length; i++) { StringBuffer buffer = new
		  StringBuffer();
		  
		  for (int j = array[i].length() - 1; j >= 0; j--) { buffer =
		  buffer.append(array[i].charAt(j));
		  
		  } array[i] = buffer.toString(); System.out.print(array[i]+" ");
//Second
		 for (int k= 0; k < array.length; k++) {
			StringBuffer buffer1= new StringBuffer();
			buffer1 = buffer1.append(array[i]);
			buffer1 = buffer1.reverse();
			array[i] = buffer1.toString();
			System.out.print(array[i] + " ");

}

- Rishabh Tiwari July 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverseWordsInString(string& str)
{
	int wordIndex = 0;

	for (int i = 0; i <= str.length(); i++) {
		if (str[i] == NULL || str[i] == ' ') {
			for (int j = i - 1; j >= wordIndex; j--) {
				char temp = str[j];
				str[j] = str[wordIndex];
				str[wordIndex] = temp;
				wordIndex++;
			}
			wordIndex = i + 1;
		}
	}
}

- ken.yeck July 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var s = "This is an example";
var words = s.split(" ");
for (var i = 0; i < words.length; i++) {
    var letters = words[i].split("");
    while (letters.length > 0)
        process.stdout.write(letters.pop());
    process.stdout.write(" ");
}

- brazilcoder August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char str[100], temp;
int i, j = 0;

printf("\n Enter the string: ");
gets(str);
i = 0;
j = strlen(str) - 1;
while(i<j){
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\n The reversed string is: %s", str);
printf("\n \t The length of the string is: %d", strlen(str));
getch();
}

- singh.chakresh August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static char[] Reverse(char[] str)
        {
            if (str.Length == 0)
                return new char[0];
            else if (str.Length == 1)
                return new char[1] { str[0] };


            char[] res = new char[str.Length];
            int si = 0;
            int ei = 0;

            for (int i = si; i < str.Length; i++, ei++)
            {
                if (str[i] != ' ')
                {
                    continue;
                }
                else
                {
                    res[i] = ' ';

                    for (int j = si; j < ei; j++)
                    {
                        res[j] = str[ei - 1 - (j - si)];
                    }
                    si = ei + 1;
                }
            }
            if (ei >= str.Length)
                ei = str.Length - 1;
            if (si <= ei)
            {

                for (int j = si; j <= ei; j++)
                {
                    res[j] = str[ei - (j - si)];

                }

            }


            return res;


        }

The above code is in C# and do not use any inbuilt string functions. It just manipulates the array items

- newdevin August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string.h>
using namespace std;
int main() 				//amit don hai
{
	int k=0,i,l1,j;
	char ch[445];
	gets(ch);
	l1=strlen(ch);
	for(i=k;i<l1;i++)
	{
		if(ch[i]==' ' || ch[i]=='\0')
		{
		 	for(j=i-1;j>=k;j--)
			cout<<ch[j];
			cout<<" ";
			 k=i+1;	
		}
		else if(i==l1-1)
		{
			for(j=l1-1;j>=k;j--)
			cout<<ch[j];
		}
	}
	return 0;
}

- amit August 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string.h>
using namespace std;
int main() //amit don hai
{
int k=0,i,l1,j;
char ch[445];
gets(ch);
l1=strlen(ch);
for(i=k;i<l1;i++)
{
if(ch[i]==' ' || ch[i]=='\0')
{
for(j=i-1;j>=k;j--)
cout<<ch[j];
cout<<" ";
k=i+1;
}
else if(i==l1-1)
{
for(j=l1-1;j>=k;j--)
cout<<ch[j];
}
}
return 0;
}

- amit August 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;


void reverseString(string& s){
int length = s.size() -1;
for(int i=0; i<=length/2;i++){
swap(s[i],s[length-i]);
}
}
int main(){

string str ="This is an example";
cout << "The original string is: " << str << endl;

reverseString(str);
cout << "The reversed string is: " << str << endl;

return 0;
}

- hh August 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;


void reverseString(string& s){
	int length = s.size() -1;
	for(int i=0; i<=length/2;i++){
		swap(s[i],s[length-i]);
	}
}
int main(){

	string str ="This is an example";
	cout << "The original string is: " << str << endl;

	reverseString(str);
	cout << "The reversed string is: " << str << endl;

	return 0;
}

- hh August 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;


void reverseString(string& s){
	int length = s.size() -1;
	for(int i=0; i<=length/2;i++){
		swap(s[i],s[length-i]);
	}
}
int main(){

	string str ="This is an example";
	cout << "The original string is: " << str << endl;

	reverseString(str);
	cout << "The reversed string is: " << str << endl;

	return 0;
}

- Ryan August 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main() {

char str [] = "This is example";
int i = 0,j = 0,k = 0,wordbegin=0;

for (i = 0; i <= strlen(str); i++)
{

if (str[i] == ' ' || str[i] == '\0')
{
for(j=wordbegin, k = i - 1; j<=k; j++, k--)
{
char temp = str[j];
str[j] = str[k];
str[k] = temp;
}
wordbegin = i+1;
}

}

printf ("Output: %s",str);
return 0;
}

- Nasir August 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) throws Exception{
String input = "This is an example";
String [] array = input.split(" ");
StringBuilder sb = new StringBuilder();

for (int i=0;i<array.length;i++){
char [] temp = new char[array[i].length()];
for(int j=0;j<temp.length;j++){
temp[j] = array[i].charAt(temp.length - 1 - j);
}
sb = sb.append(new String(temp)).append(" ");
}

System.out.println("Output for "+input+" is : "+sb);
}

- Karthick Arunachalam August 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) throws Exception{
		String input = "This is an example";
		String [] array = input.split(" ");
		StringBuilder sb = new StringBuilder();
		
		for (int i=0;i<array.length;i++){
		char [] temp = new char[array[i].length()];
			for(int j=0;j<temp.length;j++){
				temp[j] = array[i].charAt(temp.length - 1 - j);
			}
			sb = sb.append(new String(temp)).append(" ");
		}
		
		System.out.println("Output for "+input+" is : "+sb);
	}

- Karthick Arunachalam August 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

public static void main(String[] args) throws Exception{
String input = "This is an example";
String [] array = input.split(" ");
StringBuilder sb = new StringBuilder();

for (int i=0;i<array.length;i++){
char [] temp = new char[array[i].length()];
for(int j=0;j<temp.length;j++){
temp[j] = array[i].charAt(temp.length - 1 - j);
}
sb = sb.append(new String(temp)).append(" ");
}

System.out.println("Output for "+input+" is : "+sb);
}

and

- Karthick Arunachalam August 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseString
{
public String reverseStringPlain(String sampleString)
{
char[] sampleCharArray=sampleString.toCharArray();
//System.out.println("Original String");
//displayCharArray(sampleCharArray);

char tempChar='a';
int arrayStartWalker=0;

for (int arrayEndWalker=sampleCharArray.length-1; arrayEndWalker>=sampleCharArray.length/2; arrayEndWalker--)
{
if (arrayStartWalker!=arrayEndWalker)
{
tempChar=sampleCharArray[arrayStartWalker];
sampleCharArray[arrayStartWalker]=sampleCharArray[arrayEndWalker];
sampleCharArray[arrayEndWalker]=tempChar;
}
arrayStartWalker++;
}

//System.out.println("reverse string");
//displayCharArray(sampleCharArray);
return(new String(sampleCharArray));

}

public void displayCharArray(char[] sampleCharArray)
{
for (char sampleChar : sampleCharArray)
{
System.out.print(sampleChar);
}
System.out.println("");
}




}

- Khasim Peera Durgam August 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseString
{
public String reverseStringPlain(String sampleString)
{
char[] sampleCharArray=sampleString.toCharArray();
//System.out.println("Original String");
//displayCharArray(sampleCharArray);

char tempChar='a';
int arrayStartWalker=0;

for (int arrayEndWalker=sampleCharArray.length-1; arrayEndWalker>=sampleCharArray.length/2; arrayEndWalker--)
{
if (arrayStartWalker!=arrayEndWalker)
{
tempChar=sampleCharArray[arrayStartWalker];
sampleCharArray[arrayStartWalker]=sampleCharArray[arrayEndWalker];
sampleCharArray[arrayEndWalker]=tempChar;
}
arrayStartWalker++;
}

//System.out.println("reverse string");
//displayCharArray(sampleCharArray);
return(new String(sampleCharArray));

}

public void displayCharArray(char[] sampleCharArray)
{
for (char sampleChar : sampleCharArray)
{
System.out.print(sampleChar);
}
System.out.println("");
}




}

- Khasim Peera Durgam August 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {

public static void main(String[] args) {

String str = "This is good";

char[] carr = str.toCharArray();

String tmpStr = "";

for (int i = carr.length - 1, j = 0; i >= 0; i--, j++) {

tmpStr += carr[i];
}

System.out.println(tmpStr);

}

}

- Deepthi September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {

	public static void main(String[] args) {

		String str = "This is good";

		char[] carr = str.toCharArray();

		String tmpStr = "";

		for (int i = carr.length - 1, j = 0; i >= 0; i--, j++) {

			tmpStr += carr[i];
		}

		System.out.println(tmpStr);

	}

}

- Deepthi September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {

	public static void main(String[] args) {

		String str = "This is good";

		char[] carr = str.toCharArray();

		String tmpStr = "";

		for (int i = carr.length - 1, j = 0; i >= 0; i--, j++) {

			tmpStr += carr[i];
		}

		System.out.println(tmpStr);

	}

}

- Deepthi September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {

	public static void main(String[] args) {

		String str = "This is good";

		char[] carr = str.toCharArray();

		String tmpStr = "";

		for (int i = carr.length - 1, j = 0; i >= 0; i--, j++) {

			tmpStr += carr[i];
		}

		System.out.println(tmpStr);

	}

}

- Deepthi September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "This is good";

		char[] carr = str.toCharArray();

		String tmpStr = "";

		for (int i = carr.length - 1, j = 0; i >= 0; i--, j++) {

			tmpStr += carr[i];
		}

		System.out.println(tmpStr);

- Deepthi September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "This is good";

		char[] carr = str.toCharArray();

		String tmpStr = "";

		for (int i = carr.length - 1, j = 0; i >= 0; i--, j++) {

			tmpStr += carr[i];
		}

		System.out.println(tmpStr); and

- Anonymous September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "This is good";

		char[] carr = str.toCharArray();

		String tmpStr = "";

		for (int i = carr.length - 1, j = 0; i >= 0; i--, j++) {

			tmpStr += carr[i];
		}

		System.out.println(tmpStr);

- Anonymous September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A raw C based solution would be below. I am sure there might be a faster way to solve this.

//Gets to the end of the current word
int getNextToken(char *pStr) {
	int nTokenLen = 0;
	//"abc defg gi"
	while(*pStr != '\0') {
		if(*pStr == ' ') break;
		nTokenLen++;
		pStr++;		
	}
	return nTokenLen;
}

//Skips the white spaces before a word
char * skipWhiteSpaces(char *pWN) {
	while(*pWN != '\0') {
		if(*pWN != ' ') break;
		pWN++;
		}
	return pWN;
}

//Reverses the current word, so changes "Hello" to "olleH"
void reverseString(char *pWB, char *pWE) {
	while(pWB < pWE) {
		char ch = *pWB;
		*pWB = *pWE;
		*pWE = ch;
		pWB++;
		pWE--;
	}
}

//pWN -> Points to the new Word ptrWordNew
//pWE -> Points to the end of the current word ptrWordEnd
//pWB -> Points to the begining of the current word ptrWordBegin
void reverseWordsInAString(char *pStr) {
	char *pWB = NULL, *pWE = NULL, *pWN = NULL;
	pWN = pStr;

	int nNextTokenLen = getNextToken(pStr);
	do {
		pWB = pWE = pWN;
		if(nNextTokenLen) {
			pWE += (nNextTokenLen - 1); //moves to the end of word
			pWN += nNextTokenLen;		//moves to the next space

			reverseString(pWB, pWE);	
		}
		if(*pWN == '\0') break;
		if(*(pWN + 1) == ' ')
			pWN = skipWhiteSpaces(pWN);
		else
			pWN++;
		nNextTokenLen = getNextToken(pWN);
	}while(nNextTokenLen);
}

void test_CC_5694329337151488() {
	char pszSentence[] = "Hello      My Friend How Are You";
	cout<<"Original string.."<<pszSentence<<endl;
	reverseWordsInAString(pszSentence);
	cout<<"Words reversed to.."<<pszSentence;
}

- Sumeet September 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void swap (char[] arr, int i, int j) 
	{
		char t = arr[i];
		arr[i] = arr[j];
		arr[j] = t;
	}
	
	public static String wordReverse(String str)
	{
		char[] input = str.toCharArray();
		int len = str.length();
		int st = 0;
		int end = 0;
		for (int i = 0; i < len; i++) {
			if (input[i]==' '|| i+1 == len) {
				end = i-1;
				if (i+1 == len) {
					end = i;
				}
				reverse(input, st, end);
				st = i+1;
			}
			
			
		}
		
		return new String(input);
	}
	
	
	public static void reverse(char[] arr, int st, int end)
	{
		int len = end-st+1;
		for (int i = 0; i < len/2;i++ ) {
			swap(arr,st+i,end-i);
		}

}

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

public static void swap (char[] arr, int i, int j) 
	{
		char t = arr[i];
		arr[i] = arr[j];
		arr[j] = t;
	}
	
	public static String wordReverse(String str)
	{
		char[] input = str.toCharArray();
		int len = str.length();
		int st = 0;
		int end = 0;
		for (int i = 0; i < len; i++) {
			if (input[i]==' '|| i+1 == len) {
				end = i-1;
				if (i+1 == len) {
					end = i;
				}
				reverse(input, st, end);
				st = i+1;
			}
			
			
		}
		
		return new String(input);
	}
	
	
	public static void reverse(char[] arr, int st, int end)
	{
		int len = end-st+1;
		for (int i = 0; i < len/2;i++ ) {
			swap(arr,st+i,end-i);
		}

}

- vj October 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void swap (char[] arr, int i, int j) 
	{
		char t = arr[i];
		arr[i] = arr[j];
		arr[j] = t;
	}
	
	public static String wordReverse(String str)
	{
		char[] input = str.toCharArray();
		int len = str.length();
		int st = 0;
		int end = 0;
		for (int i = 0; i < len; i++) {
			if (input[i]==' '|| i+1 == len) {
				end = i-1;
				if (i+1 == len) {
					end = i;
				}
				reverse(input, st, end);
				st = i+1;
			}
			
			
		}
		
		return new String(input);
	}
	
	
	public static void reverse(char[] arr, int st, int end)
	{
		int len = end-st+1;
		for (int i = 0; i < len/2;i++ ) {
			swap(arr,st+i,end-i);
		}

}

- vj October 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static String reverseString(String s){
	s+=s+" ";
	String myString="";
	int lastIndex=0;
	int lenght=0;
	for(int i=0; i < s.length(); i++){
		int l=0;
		if(s.charAt(i)==' '){	
			String subString =s.substring(lastIndex, i);		
			lenght=(i-lastIndex);
			char[] ch= new char[lenght];
			l=lenght;
			for(int j=0;j< lenght;j++){
				l=l-1;
			ch[j]=subString.charAt(l);
			}
			myString +=String.valueOf(ch) +" ";
			lastIndex=i+1;
			}
	}
	return myString;

}

- Jitendra December 30, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseString {
public static void main(String[] args) {
String str ="This is String Reverse Example ";

String s = reverseString(str);
System.out.println("Reverse String is -> "+ s);
}
public static String reverseString(String s){
s+=s+" ";
String myString="";
int lastIndex=0;
int lenght=0;
for(int i=0; i < s.length(); i++){
int l=0;
if(s.charAt(i)==' '){
String subString =s.substring(lastIndex, i);
lenght=(i-lastIndex);
char[] ch= new char[lenght];
l=lenght;
for(int j=0;j< lenght;j++){
l=l-1;
ch[j]=subString.charAt(l);
}
myString +=String.valueOf(ch) +" ";
lastIndex=i+1;
}
}
return myString;
}
}

- Jitendra Rai December 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String reverseString(String s){
s+=s+" ";
String myString="";
int lastIndex=0;
int lenght=0;
for(int i=0; i < s.length(); i++){
int l=0;
if(s.charAt(i)==' '){
String subString =s.substring(lastIndex, i);
lenght=(i-lastIndex);
char[] ch= new char[lenght];
l=lenght;
for(int j=0;j< lenght;j++){
l=l-1;
ch[j]=subString.charAt(l);
}
myString +=String.valueOf(ch) +" ";
lastIndex=i+1;
}
}
return myString;
}

- Jitendra Rai December 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String s="This is my book";

String[] str=s.split(" ");

for(int i=0;i<str.length;i++){

StringBuffer sb=new StringBuffer(str[i]);
sb.reverse();
str[i]=sb.toString();
System.out.println(str[i]);

}

- Anonymous January 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String s="This is my book";
String[] str=s.split(" ");
for(int i=0;i<str.length;i++){
StringBuffer sb=new StringBuffer(str[i]);
sb.reverse();
str[i]=sb.toString();
System.out.println(str[i]);
}

- Anonymous January 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;

namespace ReverseStringInSentence
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = "This is an example";

            Console.WriteLine(ReverseStrings(input));

            Console.ReadLine();
        }

        public static string ReverseStrings(string sentence)
        {
            if (sentence == null) throw new ArgumentNullException();
            if (sentence == string.Empty) throw new ArgumentException("Input is an empty string");

            string[] words = sentence.Split(' ');
            var reveresedSentence = string.Empty;

            foreach (var word in words)
            {
                var reversedWord = string.Empty;
                var stack = new Stack<char>();

                foreach (var ch in word)
                {
                    stack.Push(ch);
                }
                foreach (var item in stack.ToArray())
                {
                    reversedWord += stack.Pop();
                }

                reveresedSentence += string.Format("{0} ", reversedWord);
            }

            return reveresedSentence;
        }
    }
}

- ajay.majgaonkar February 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String stringReverser(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException();
}
if (str.length() < 2) {
return str;
}
String[] strArr = str.split(" ");
StringBuffer newHolder = new StringBuffer();
for (int i = 0; i < strArr.length; i++) {
int j = 0;
int len = strArr[i].length();
char[] newCharStr = strArr[i].toCharArray();
while (j < len) {
char temp = newCharStr[j];
newCharStr[j] = newCharStr[len - 1];
newCharStr[len - 1] = temp;
len--;
j++;
}
newHolder.append(String.valueOf(newCharStr));
if(i != strArr.length-1){
newHolder.append(" ");
}
}

return newHolder.toString();
}

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

public static String stringReverser(String str) {
        if (str == null || str.isEmpty()) {
            throw new IllegalArgumentException();
        }
        if (str.length() < 2) {
            return str;
        }
        String[] strArr = str.split(" ");
        StringBuffer newHolder = new StringBuffer();
        for (int i = 0; i < strArr.length; i++) {
            int j = 0;
            int len = strArr[i].length();
            char[] newCharStr = strArr[i].toCharArray();
            while (j < len) {
                char temp = newCharStr[j];
                newCharStr[j] = newCharStr[len - 1];
                newCharStr[len - 1] = temp;
                len--;
                j++;
            }
            newHolder.append(String.valueOf(newCharStr));
            if(i != strArr.length-1){
                newHolder.append(" ");
            }
        }

        return newHolder.toString();
    }

- Gokhan Akdugan February 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String stringReverser(String str) {
if (str == null || str.isEmpty()) {
throw new IllegalArgumentException();
}
if (str.length() < 2) {
return str;
}
String[] strArr = str.split(" ");
StringBuffer newHolder = new StringBuffer();
for (int i = 0; i < strArr.length; i++) {
int j = 0;
int len = strArr[i].length();
char[] newCharStr = strArr[i].toCharArray();
while (j < len) {
char temp = newCharStr[j];
newCharStr[j] = newCharStr[len - 1];
newCharStr[len - 1] = temp;
len--;
j++;
}
newHolder.append(String.valueOf(newCharStr));
if(i != strArr.length-1){
newHolder.append(" ");
}
}

return newHolder.toString();
}

- Gokhan Akdugan February 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String stringReverser(String str) {
        if (str == null || str.isEmpty()) {
            throw new IllegalArgumentException();
        }
        if (str.length() < 2) {
            return str;
        }
        String[] strArr = str.split(" ");
        StringBuffer newHolder = new StringBuffer();
        for (int i = 0; i < strArr.length; i++) {
            int j = 0;
            int len = strArr[i].length();
            char[] newCharStr = strArr[i].toCharArray();
            while (j < len) {
                char temp = newCharStr[j];
                newCharStr[j] = newCharStr[len - 1];
                newCharStr[len - 1] = temp;
                len--;
                j++;
            }
            newHolder.append(String.valueOf(newCharStr));
            if(i != strArr.length-1){
                newHolder.append(" ");
            }
        }

        return newHolder.toString();

}

- Gokhan Akdugan February 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
	String str="This is an example";
        String[] arr=str.split(" ");

        // using StringBuilder reverse method
        
        StringBuilder sb=new StringBuilder();
        for(int i=0; i< arr.length ;i++){
           sb.append(arr[i]+" ");          
        }
        sb.reverse();
        System.out.println(sb.toString());     
        
        // using StringBuilder without reverse method
        
        StringBuilder sb1 = new StringBuilder("");
        for(int i=arr.length-1; i>=0;i--){
        	char[] temp = arr[i].toCharArray();
        	String reverse = "";
            	for(int j = temp.length-1; j>=0 ; j--)
            		reverse = reverse + temp[j];
            	sb1.append(reverse+" ");
        }
        System.out.println(sb1.toString());
       
}

- RK April 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Python Solution
string = "This is an example"

x = []
y = ""

for i in range(1, len(string)+1):
j = -1*i
y = y + string[j]

#print (y)
k = ''
for i in range(0, len(string)):

if string[i] != ' ':
k = k +string[i]
if (string[i] == ' ') or (i == (len(string)-1)):
x.append(k)
k = ''
#print (x)

z = []
q = ''
for j in range(0, len(x)):
t = str(x[j])
for p in range(1,len(t)+1):
p = -1*p
q = q + t[p]
z.append(q)
q = ''
#print (z)


rev_str = ''
for g in range(0,len(z)):
rev_str = rev_str + ' ' + str(z[g])

print (rev_str.lstrip())

- Prashant Shukla April 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string = "This is an example"

x = []
y = ""

for i in range(1, len(string)+1):
    j = -1*i
    y = y + string[j]

#print (y)
k = ''
for i in range(0, len(string)):
    
    if string[i] != ' ':
        k = k +string[i]
    if (string[i] == ' ') or (i == (len(string)-1)):
        x.append(k)
        k = ''
#print (x)

z = []
q = ''
for j in range(0, len(x)):
    t = str(x[j])
    for p in range(1,len(t)+1):
        p = -1*p
        q = q + t[p]
    z.append(q)
    q = ''
#print (z)  


rev_str = ''
for g in range(0,len(z)):
    rev_str = rev_str + ' ' + str(z[g])

print (rev_str.lstrip())

- Prashant April 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class ReverseString {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the sentence");
		String sent = sc.nextLine();

		ReverseString rs = new ReverseString();
		rs.stringReverse(sent);
	}

	private void stringReverse(String str){
		boolean flag=true;
		int beginIndex=0;
		int endIndex=0;
		char[] strArr = str.toCharArray();
		int i=0;
		while(flag){
			if(strArr[i+1]!=' ' || strArr[i+1]==strArr.length-1){
				endIndex++;
				i++;
				if(i==str.length()-1){
					printString(strArr, beginIndex, endIndex);
					flag=false;
				}
			}
			else{
				printString(strArr, beginIndex, endIndex);
				endIndex++;
				beginIndex=endIndex+1;
				i++;
			}
		}
	}

	private void printString(char[] strArr, int beginIndex, int endIndex) {
		// TODO Auto-generated method stub
		for(int j=endIndex; j>=beginIndex; j--){
			System.out.print(strArr[j]);
			
		}
		System.out.print(" ");
	}
}

- sahilchalke3 August 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One line python code:
str = "This is an example"
' '.join((a[::-1] for a in str.split()))

- Dilip August 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

one line python code should do it

str = "This is an example"
' '.join((a[::-1] for a in str.split()))

- Dilip August 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class WordReverse
{
public static void main(String[] args)
{
String str ="This is me.., i'm the boss";

String[] words = str.split(" ");
List al = new ArrayList();
for (int i = 0; i < words.length; i++)
{
al.add(strRrev(words[i]) + " ");
System.out.print(al.get(i));
}



}

public static String strRrev(String str)
{
String temp = "";
for (int i = str.length()-1; i>=0 ; i--)
{
temp = temp + str.charAt(i);
}


return temp;
}
}

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

/**
   * Javascript Implementation.
   * reverseStringInPlace
   
Write program for the following case 
Reverse string (string is stored in an array) 
Input:- "This is an example" 
Output:-sihT si na elpmaxe

*/


function reverseStringInPlace(str) {
  let words = str.split(' ');
  for(let index = 0, length = words.length; index < length; index++) {
    words[index] = words[index].split('').reverse().join('');      
  }
  return words.join(' ');
}

let str = 'This is an example';
console.log("reverseStringInPlace ->");
console.log("Input  : " + str);
console.log("Output : " + reverseStringInPlace(str));

- Amit Bansal March 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String reverseGivenString(String input) {
		String strTemp = "";
		for(int i=input.length()-1; i>=0; i--) {
			strTemp += "" + input.charAt(i);
		}
		return strTemp;
	}

	public static void main(String[] args) {
		String str = "This is test program";
		for(String s:str.split(" ")) {
			System.out.print(reverseGivenString(s) + " ");
		}
	}

- GK May 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python Solution:

a = "This is an example"
new = a.split(" ")
rev =[]
stre =""
for i in new:
    for j in i:
        stre = j+stre
    rev.append(stre)
    stre =""
print(rev)

- ranjani92s January 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good Solution

- Nick January 30, 2020 | Flag


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