-4

I was recently asked this question in an interview.

Initially, I was asked to reverse a sentence without using inbuilt String api methods like split etc.

I/p : I like god O/p : god like I

I did this using a stack. His next question was to accomplish this without using additional memory.

How do we achieve this in java?

Thanks!

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • I assume you mean O(1) additional memory? And what does this have to do with Java? Do you want code or an algorithm? And how have you researched this? Googled algorithms elsewhere for instance? Questions with no effort shown are frowned upon here. – djechlin May 15 '13 at 22:28
  • I haven't been able to find a solution in google. The reason I mentioned java was because I did not want someone to suggested a pointer arithmetic based solution in C. I am looking for a solution , not really worried if someone suggests an algorithm or a code. – user2333846 May 15 '13 at 22:32
  • 2
    Java strings are immutable, so this cannot be solved in Java without creating a new string, which I would say means using additional memory (that is, it cannot be done in place, as it could in C). – rici May 15 '13 at 22:49

1 Answers1

5

Reverse the sentence in-place on a character-by-character basis, then reverse each word in the sentence in-place on a character-by-character basis.

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62