Describe the data structure where:
- Any element is indexed by an integral value, as in an array
- an integer can index a single value
- the integers used to index elements are adjacent: they go from 1 to
n without holes
- Getting the item at position
i (i.e.: the item associated with the integer i ) should be as fast as possible - Inserting a new item at position
i should be as fast as possible- this will shift to the right of any element with
i forward
- Deleting an item at position
i should also be as fast as possible- this will shift to the left any element from
i+1 forward
EDIT: a little thing I forgot about: item indices can only be shifted when adding / removing, they cannot be accidentally replaced.
In this description, n is the size of the structure (i.e., the number of elements it contains), and i is the total integer (1 <= i <= n ), of course.
I heard this from a guy I met at my faculty. I donโt know if this is an interview question, an exam question, just a mystery or what, but I think that could be all.
If I remember correctly (but, by the way, until December 24), he said that such a data structure can be implemented either with O(sqrt n) insertion / deletion, or access time O(1) , or using O(log n) for any operation.
EDIT: Some correct answers have been given. Read this if you donโt want to think about this issue anymore.
language-agnostic algorithm data-structures
peoro
source share