Workday

  www.workday.com
  www.workday.com

Interview Question

Associate Software Engineer Interview Pleasanton, CA

How would you reverse a the words in a string? (Be able to

  defend answer)
Answer

Interview Answer

4 Answers

0

String reverse(String s){
length = s.length()-1;
s1 = "";
while(length >=0){
s1 += s[length];
length--;
}

return s1;
}
//wrote the code in 1 minute, did not test it or walk through it, use at your own risk

Jason Peng on Nov 11, 2012
0

I got the other idea of not using a buffer (a better solution)
String reverseWithoutBuffer(String s){
if (s.length() == 0 || s.length() == 1){return s;}
length = s.length()-1;
i = 0;
while (i != length){
String s1 = s[i];
s[i] = s[length];
s[length] = s1;
i--;
length--;
}

return s;
}
//did not test it or walk through it, use at your own risk

Jason Peng on Nov 11, 2012
0

I mean i++ (not i--), sorry

Jason Peng on Nov 11, 2012
0

Sorry for another error,
it should be while (i < length)

Jason Peng on Nov 11, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.