1

This is an interview question. I need to implement a data structure that supports the following operations:

  1. Insertion of an integer in $O(1)$

  2. Deletion of an integer (for example, if we call delete(7), then 7 is deleted from the data structure, if the integer 7 exists in it) in $O(1)$.

  3. Return maximum integer in the data structure (without deleting it) in $O(1)$.

You can also use up to $O(n)$ space.

I thought of something similar to this question, but here we have $O(\mathrm{log}\ n)$.

Do you know how can we implement these operations in $O(1)$?

Edit: forgot important thing - you can assume the numbers that will be inserted are integers in the range $[0,n]$.

Thinh D. Nguyen
  • 2,275
  • 3
  • 23
  • 71
John
  • 113
  • 6
  • (Retrun?) (the purpose here proposition?) Is this to be a set or a multiset / bag (insert 7 twice, delete it once: still in)? – greybeard Oct 01 '18 at 22:17
  • @greybeard, it's a multi-set. Nothing to return in the insertion and deletion, only at the maximum operation. – John Oct 02 '18 at 14:31

2 Answers2

5

Suppose we have such data structure. We can find in $O(1)$ the max, delete the max in $O(1)$ and repeat it $n$ times. Hence, we can sort $n$ numbers in $O(n)$. Therefore, constructing such data structure must take $\Omega(n\log(n))$ (like sorting, in general, can't be done better than $n\log(n)$. Hence, you might have some constraint on data). Also, as deleting is in $O(1)$, means it must be found. Hence, Searching and Finding is in $O(1)$.

Therefore, you must know the position of each value (something like counting sort, but with this constraint that $max <= n$ to take the $O(n)$ space!). Hence, you can act like a counting sort by saving the max value in a variable besides the array, by accepting some constraint on data (not in general).

OmG
  • 3,572
  • 1
  • 14
  • 23
  • Thanks, indeed I forgot an assumption about the integers and edited the post. However, I still don't find a way to implement the operations as needed. Can you elaborate more on how you can do this with something that is similar to counting sort (thought of counting array, but it doesn't help by itself)? – John Oct 02 '18 at 14:26
  • @John my pleasure. By the new assumption, it's exactly the same as counting sort plus a variable to store max and update after each inserition. read more counting sort on Wikipedia. – OmG Oct 02 '18 at 14:41
  • how do you update the maximum when you delete an element from the data structure? in this case I see only wallking backward in the counting array till I find a number with is not zero (and that isn't O(1) ) – John Oct 02 '18 at 14:48
  • @John you don't need this. As I've said, you've stored the max in constructing time, in a variable. When you delete a number, just check with the value of maximum in that varible and do the proper act in $O(1)$, – OmG Oct 02 '18 at 14:57
  • Let's say that someone entered the numbers 3,1000,5,6,2 to the data structure - now max is 1000. After that, he performed delete(1000) so now max needs to be updated to 6. So how do you update the max to 6 in this case? the only way that I see is to go through 999 downwards till we find that 6 is the largest number entered (after the deletion of 1000). – John Oct 02 '18 at 17:35
  • seems I deleted the max value. Hi can I find the new max value in O(1)? – smrt28 Oct 02 '18 at 17:39
  • @smrt28 Hence, you can construct a linked list in size $n$, to have filled value indices (which is sorted). using this list, you can find the next filled value in $O(1)$ from the end of the list which is the position of the max value – OmG Oct 02 '18 at 18:02
  • but than you have a problem with insertion. How would you find the concatenated items between which you're inserting? – smrt28 Oct 02 '18 at 18:08
  • @smrt28 it can be an array instead of list, and then store the index of the array of filled indices, with their value in the main array (which is sorted using counting sort technique). In this way, you have access in $O(1)$ to the indices of that deleted and inserted value using these bi-directional references. – OmG Oct 02 '18 at 18:13
  • I'm sorry, I don't see it. With the array you have a problem with maximum again. IMO you can't make it with all 3 ops in O(1) – smrt28 Oct 02 '18 at 18:24
  • @smrt28 there is notany problem with maximum! It is a bi-directional indexing. Deleting maximum, you can find from the second array, the index of the next maximum in O(1). – OmG Oct 02 '18 at 18:41
  • 2
    @OmG, I agree with smart28 - don't see it as well. Can you please describe in your post a pseudo code of how you implement these operations in O(1)? – John Oct 03 '18 at 12:01
0

You can implement first 2 ops easily by creating an array from 0 to max and store counts of added/removed items there. Regarding the maximum....

I think the question is just little tricky. Since max is constant, you can iterate whole array and it would be also in O(1).

smrt28
  • 137
  • 6
  • How can you iterate the whole array in O(1)? – John Oct 11 '18 at 11:55
  • "you can assume the numbers that will be inserted are integers in the range [0, N]" - N is constant, no matter how many items are inserted. Just think how it would work if N=1. Clear? – smrt28 Oct 11 '18 at 12:22