crypto5 wrote:В джава хештаблица с ресайзом и проверкой на наличие элементов, поэтому вставка O(N).
А кто говорил про стандартную джавовскую хэштаблицу? Я оперировал исключительно Map<KeyT, ValueT> map = new ConcreteMap<KeyT, ValueT>(); и Set<T> set = new ConcreteSet<T>();
С такой прикидкой, что если интервьюеру захочется обсудить, что мы предполагаем о данной реализации, то мы и обсудим. Не захотел
Кстати, даже в джавовской реализации O(n) - это worst case. Вот кому-то не лень было объяснять, почему:
Hash tables are O(1) average and amortized case complexity, however is suffers from O(n) worst case time complexity. [And I think this is where your confusion is]
Hash tables suffer from O(n) worst time complexity due to two reasons:
If too many elements were hashed into the same key: looking inside this key may take O(n) time.
Once a hash table has passed its load balance - it has to rehash [create a new bigger table, and re-insert each element to the table].
However, it is said to be O(1) average and amortized case because:
It is very rare that many items will be hashed to the same key [if you chose a good hash function and you don't have too big load balance.
The rehash operation, which is O(n), can at most happen after n/2 ops, which are all assumed O(1): Thus when you sum the average time per op, you get : (n*O(1) + O(n)) / n) = O(1)