2

Programming question:

Reverse words in a string (words are separated by one or more spaces). Now do it in-place.

What does "in-place" mean in the above context for an interpreted language like PHP or JavaScript?

  • 3
    Possible duplicate in SO : stackoverflow.com/questions/2779797/what-does-in-place-mean – NINCOMPOOP Apr 17 '13 at 8:55
  • 1
    Possible? The "Reverse words..." part is duplicated word-for-word. – Steve314 Apr 17 '13 at 9:06
  • I apologize, I hadn't seen the dupe question. Feel free to close, merge or move this one. – Levi Hackwith Apr 17 '13 at 13:47
  • I went ahead and edited it to be more specific and remove duplication. – Levi Hackwith Apr 17 '13 at 13:49
  • 1
    @LeviHackwith: I really don't think that that context really changes anything. It's still the same question and the same answers still apply. – Joachim Sauer Apr 17 '13 at 14:20
6

It depends on how low-level the language you're working with is, but assuming something like C it means "do not take the string apart and put it back together in a different part of memory, modify the part of memory the string is already in".

I.e. not:

1. foo bar baz
2. foo bar baz -> baz
3. foo bar baz -> baz bar
4. foo bar baz -> baz bar foo

but:

1. foo bar baz
2. baz foo bar
3. baz bar foo

(Or however exactly your algorithm will work.)

5

It normally means to perform the operation without using any additional memory. In terms of space complexity, the auxiliary space complexity would be O(1).

In practice, depending on implementation, temporary variables may be allowed. In this case the output should use same location in memory as the input.

3

In this context, it refers to modifying the original variable, as opposed to using further memory. This means that, as opposed to running through the original string and copying each character to a new string variable, you need to swap pieces of the original as you are viewing it.

Wikipedia actually has an entire article on the topic, citing the topic's relevance and usefulness relating to minimizing memory usage.

0

In place will be something of this sort :

void reverse(char *str)
{
   if(!str)return;
   reverse(strtok(NULL," "));
   printf("%s ",revStr);
}

"An in-place algorithm (or in Latin in situ) is an algorithm which transforms input using a data structure with a small, constant amount of extra storage space. The input is usually overwritten by the output as the algorithm executes."
Source: In-place algorithm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.