Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "in-place" mean?

Tags:

semantics

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

What does in-place mean?

like image 474
NibblyPig Avatar asked May 06 '10 09:05

NibblyPig


People also ask

What does in-place mean software?

In-place means that the algorithm does not use extra space for manipulating the input but may require a small though non-constant extra space for its operation. Usually, this space is O(log n), though sometimes anything in O(n) (Smaller than linear) is allowed.

What put in-place means?

to put (a new policy) in, into place : to implement (a new policy), to establish.

What does in-place mean in Python?

Definition - In-place operation is an operation that changes directly the content of a given linear algebra, vector, matrices(Tensor) without making a copy. The operators which helps to do the operation is called in-place operator.

What does in-place mean sorting?

(algorithm) Definition: A sort algorithm in which the sorted items occupy the same storage as the original ones. These algorithms may use o(n) additional memory for bookkeeping, but at most a constant number of items are kept in auxiliary memory at any time. Also known as sort in place.


3 Answers

In-place means that you should update the original string rather than creating a new one.

Depending on the language/framework that you're using this could be impossible. (For example, strings are immutable in .NET and Java, so it would be impossible to perform an in-place update of a string without resorting to some evil hacks.)

like image 133
LukeH Avatar answered Sep 30 '22 23:09

LukeH


In-place algorithms can only use O(1) extra space, essentially. Array reversal (essentially what the interview question boils down to) is a classic example. The following is taken from Wikipedia:

Suppose we want to reverse an array of n items. One simple way to do this is:

function reverse(a[0..n])
    allocate b[0..n]
    for i from 0 to n
       b[n - i] = a[i]
    return b

Unfortunately, this requires O(n) extra space to create the array b, and allocation is often a slow operation. If we no longer need a, we can instead overwrite it with its own reversal using this in-place algorithm:

function reverse-in-place(a[0..n])
    for i from 0 to floor(n/2)
       swap(a[i], a[n-i])

Sometimes doing something in-place is VERY HARD. A classic example is general non-square matrix transposition.

See also

  • In-place algorithm
  • In-place matrix transposition
like image 20
polygenelubricants Avatar answered Sep 30 '22 23:09

polygenelubricants


You should change the content of the original string to the reverse without using a temporary storage variable to hold the string.

like image 23
Charles Avatar answered Sep 30 '22 23:09

Charles