Ebay Interview Question
Software Engineer InternsCountry: United States
Interview Type: Phone Interview
Here we take comma, number and space as separators between words? If so, the thought would be: find out each word' front and back, then reverse it. Code in C:
int isSeparator(char c)
{
return c == ',' || isdigit(c) || isspace(c);
}
void reverse(char* s, char* e)
{
char c;
for(; s < e; ++s, --e){
c = *s;
*s = *e;
*e = c;
}
}
char* reverseWord(char* s)
{
if(s == NULL || *s == '\0') return s;
char *head = s, *tail = s;
while(1){
//skip continuous separators till next word's front
for(head = tail; *head != '\0' && isSeparator(*head); ++head) ;
if(*head == '\0') break;
//now head points to a word's front, we go on to find out the word's back
for(tail = head + 1; *tail != '\0' && !isSeparator(*tail); ++tail) ;
//now *tail == '\0' or *tail is a separator, so we reverse s[head,tail)
reverse(head, tail - 1);
}
return s;
}
String SentenceReversal(String sentence)
{
String newString = new String(null);
String tempString = new String(null);
for(int i = 0; i<sentence.length; i++){
if((sentence.charAt(i) == ' ')||sentence.charAt(i) >= '0' && <=9) )
{
if (tempString!=null){
newString.append(WordReversal(tempString));
tempString=null;}
newString.append(sentence.charAt(i))
}
else
{
tempString.append(sentence.charAt(i));
}
return newString;
}
String WordReversal(String word){
String reverseWord = new String(null);
for (int i = word.length-1 i>=0;i--){
reverseWord.append(word.charAt(i))
}
return word;
}
String SentenceReversal(String sentence)
{
String newString = new String(null);
String tempString = new String(null);
for(int i = 0; i<sentence.length; i++){
if((sentence.charAt(i) == ' ')||sentence.charAt(i) >= '0' && <=9) )
{
if (tempString!=null){
newString.append(WordReversal(tempString));
tempString=null;}
newString.append(sentence.charAt(i))
}
else
{
tempString.append(sentence.charAt(i));
}
return newString;
}
String WordReversal(String word){
String reverseWord = new String(null);
for (int i = word.length-1 i>=0;i--){
reverseWord.append(word.charAt(i))
}
return word;
}
public class ReverseSentence {
public static void main(String argv[]) {
String sentence = "1 My name is rachana, but I like name rajashree, 234 I like 123456cooking";
System.out.println(reverseSentence(sentence));
}
private static String reverseSentence(String sentence) {
StringBuffer reversed = new StringBuffer();
char[] chars = sentence.toCharArray();
for(int i =0; i<chars.length-1;i++) {
if(!isChar(chars[i])) {
reversed.append(chars[i]);
} else {
int j = i;
StringBuffer word = new StringBuffer();
while(isChar(chars[j]) && j < chars.length-1) {
word.append(chars[j]);
j++;
}
String wordR = reverse(word.toString());
reversed.append(wordR);
i=j-1;
}
}
return reversed.toString();
}
private static boolean isChar(char c) {
if((c > 64 && c<90) || (c>96 && c<122)) {
return true;
}
return false;
}
private static String reverse(String word) {
char[] A = word.toCharArray();
int max = word.length() - 1;
int min = 0;
while (min < max) {
char temp = A[max];
A[max] = A[min];
A[min] = temp;
max--;
min++;
}
return new String(A);
}
}
It could ez solve by using the functions of StringBuilder:
insert(offset,char)
append(char)
static String reverseWords(String s) {
if (s==null||s.length()==0)
return "";
StringBuilder result = new StringBuilder();
int j=0; // position of last [comma,space,numbers]
for (int i=0;i<s.length();i++) {
char c = s.charAt(i);
if (c==' '||Character.isDigit(c)||c==','){
result.append(c);
j=i+1;
} else result.insert(j, c);
}
return result.toString();
}
}
public class StringReverse
{
public static void main(String args[])
{
String s = "1 My name is rachana, but I like name rajashree, 234 I like 123456cooking";
reverse(s);
}
public static void reverse(String s)
{
int m=0;
char b[]= new char[s.length()];
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if(Character.isDigit(c) || c == ',' || c == ' ')
{
b[i] = c;
}
}
for(int i=s.length()-1; i>=0; i--)
{
char c = s.charAt(i);
if(Character.isDigit(c) || c == ',' || c == ' ')
{
continue;
}
else
{
while(b[m] !='\u0000')
{
m++;
}
b[m] = c;
m++;
}
}
System.out.println(new String(b));
}
}
The output for the string "I love, driving 5 cars" is "I evol, gnivird 5 racs"
public class reverseWord {
public void revWord(String str){
int flag=0;
String word="";
String finalString="";
char[] charArray = str.toCharArray();
for(int i=0;i<charArray.length;i++){
if(charArray[i]!=' '&&charArray[i]!=','&&!Character.isDigit(charArray[i])){
flag=1;
}
else if(flag==1&&(charArray[i]==' '||charArray[i]==','||Character.isDigit(charArray[i])))
flag=2;
if(flag==1&&i==str.length()-1){
flag=2;
}
if(flag==1){
word=word+charArray[i];
}
else if(flag==2){
word= new StringBuffer(word).reverse().toString();
finalString=finalString+word;
flag=0;
word="";
}
if(flag==0){System.out.println("final else");
finalString=finalString+charArray[i];
}
}
System.out.println(finalString);
}
public static void main(String args[]){
reverseWord list1=new reverseWord();
list1.revWord("I love, driving 5 cars");
}
}
examples?
- Anonymous April 21, 2014which company interviewed you?