I believe, everyone would have got this question or have asked during the interviews.

I came to this question again through one blog where he suggested to solve it via looping n/2 times like shown below -

I came to this question again through one blog where he suggested to solve it via looping n/2 times like shown below -

And if you ask then response will be like-

You are iterating n/2 times, so runtime complexity is n/2

And my understanding is, you are not just counting the numbers, you are accessing the elements in the array. So have you reduced those operations.

So don't take count of iterations as complexity, first optimize the number of operations & their complexity then the count of iterations will matter.

If reducing the number of operations X iterations ~ number of operations X reduced ierations

then you may not be getting any benefit or any considerable benefit.

So you can try below function which runs n times-

You are iterating n/2 times, so runtime complexity is n/2

And my understanding is, you are not just counting the numbers, you are accessing the elements in the array. So have you reduced those operations.

So don't take count of iterations as complexity, first optimize the number of operations & their complexity then the count of iterations will matter.

If reducing the number of operations X iterations ~ number of operations X reduced ierations

then you may not be getting any benefit or any considerable benefit.

So you can try below function which runs n times-

Some may claim that for small or small number of Strings, there may not be any difference.

So I request you to run these function for large Strings & multiple times.

I executed these functions for Strings of length 1,000,000 & executed each function for 100,000 times, to get the below results-

reverse took -

reverseEach took -

I think, generally you will not be running this, so many times or for so big Strings, so running n/2 times will not be giving much visible benefit. But running n/2 times is good when you do reverse an array in-place, without requiring a new array.

For reversing the String, it is senseless.

To get the best performance try Reverse String InPlace.

Below image shows the result when executed various functions to reverse the String or character array for Strings of

I believe, few point are uncovered & or there will be something to discuss, so please provide your comments to have a discussion around it & should it be asked like this in interviews?

So I request you to run these function for large Strings & multiple times.

I executed these functions for Strings of length 1,000,000 & executed each function for 100,000 times, to get the below results-

reverse took -

**225 Sec**reverseEach took -

**254 Sec**I think, generally you will not be running this, so many times or for so big Strings, so running n/2 times will not be giving much visible benefit. But running n/2 times is good when you do reverse an array in-place, without requiring a new array.

For reversing the String, it is senseless.

To get the best performance try Reverse String InPlace.

Below image shows the result when executed various functions to reverse the String or character array for Strings of

**length 1 million & executed for 100,000 times**.I believe, few point are uncovered & or there will be something to discuss, so please provide your comments to have a discussion around it & should it be asked like this in interviews?