Workday Interview Question

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

Interview Answers

Anonymous

Nov 11, 2012

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

Anonymous

Nov 11, 2012

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

Anonymous

Nov 11, 2012

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

Anonymous

Nov 11, 2012

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