Amazon Interview Question
Quality Assurance EngineersCountry: United States
Interview Type: Phone Interview
public class rstr {
static String rString(String str){
StringBuilder sb = new StringBuilder();
for (int i = str.length()-1; i >=0; i--) {
sb.append(str.charAt(i));
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(rString("Trying is Good"));
}
}
public class rstr {
static char[] rString(String str){
char[] rstr = new char[str.length()];
System.out.println("str.length(): " + str.length());
for (int i = str.length()-1; i >= str.length()/2; i--) {
if(str.length()-1-i >= 0) {
rstr[str.length()-1-i] = str.charAt(i);
rstr[i] = str.charAt(str.length()-1-i);
}
}
return rstr;
}
public static void main(String[] args) {
System.out.println(rString("Trying is Good"));
}
}
int i = 0;
int j = str.length() - 1;
char[] strArr = str.toCharArray();
while(i != j) {
char c = str.charAt(i);
char l = str.charAt(j);
strArr[i] = l;
strArr[j] = c;
i++;
j--;
}
QA will make that throw an "Array Index Bounds Exception"
Your second line, LENGTH -1, <-- this "can" be 0 - 1 = -1
And no need to charAt insidethe loop. And you need to copy back to str.
Your method should be something like this:
char[] A = str.toCharArray();
for(int i = 0, j = str.length() - 1; i < j ; i++, j-- )
swap( A[i] , A[j] );
str=String.valueOf(A);
or if you insist
char[] A = str.toCharArray();
for(int i = 0, j = str.length() - 1; i < j ; i++, j-- )
{
A[i] = A[j];
A[j] = str.charAt(i);
}
str=String.valueOf(A);
No need for your l and c.
The most common thing .. it will not work for string with even size .. like .. "ABCD". Pointers will pass each other and one time they will through ArrayIndexOutOfBound something exception.
+1 @Ajeet.
And QA will always test empty string as first test case, so any developer should mentally check that too.
For fun only
One liner (C++ maybe) for maximum ugliness:
for(int i=0; int j=strlen(s)-1; i < j; s[i++]^=s[j--]^=s[i]^=s[j] ) ;
Wonder if that works....
public class Reverse {
static String s = "abcdefg";
static String reverse(){
char a[] = new char[s.length()];
a = s.toCharArray();
int i = 0, j = s.length()-1;
while(i < j){
char c = a[i];
a[i] = a[j];
a[j] = c;
i++;j--;
}
s = new String(a);
return s;
}
public static void main(String args[]){
if(s.length()==0)
System.out.println("Empty String");
else
System.out.println(reverse());
}
}
I'm new at this. Is using standard c/C++ libraries allowed for things like that?
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s = "Hello World!";
cout << s << endl;
//reverse string in-place
std::reverse(s.begin(), s.end());
cout << s << endl;
//reverse string
s = string(s.rbegin(), s.rend());
cout << s << endl;
cin.ignore();
return 0;
}
So here for reversing a string, we have very simple algo :-
Take the string and convert it into char array by using toCharArray() function and take a temp char var.
Now start a for loop which will run until half of the length of array.
In this loop just swap the values using temp var like first value of array store in temp, and last element of array move to first and temp move to last.
That all now print the array by using for loop.
Here is the code implementation for the same :
import java.util.*;
public class ReverseString{
public static void main(String []args){
String str = "this is me";
char[] arr = str.toCharArray();
int len = arr.length;
for(int i=0;i<arr.length/2;i++){
char temp = 0;
temp = arr[i];
arr[i] = arr[len-i-1];
arr[len-i-1] = temp;
}
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
}
swap first with last
- S O U N D W A V E October 21, 2013....
then?