How to reverse a string in O(1) complexity (runtime)?

Viewed 19905

I faced an interview today and I was asked to reverse a string in single operation . I was feeling like it is impossible in less than O(n) . By the way , I was given a clue , "Swapping" ! So . . . does any answer exist ?

  • Sample Input : "abcdefghi" (or , any strings )
  • Sample Output :"ihgfedcba"
  • No built in function can be used . ( ex : strrev() )
  • The answer is not tricky like just printing/iterating reversely .
3 Answers
Related