Data Structures19 sections · 729 units
Open in Course

When Hashing Fails

Collision attacks

In competitive programming, some judges create adversarial test cases that cause many hash collisions, degrading O(1)O(1) to O(n)O(n). Signs of a hash attack: TLE on unordered_map but AC on map. Solutions:

1.1. Use tree-based structures (map in C++, TreeMap in Java). O(logn)O(\log n) is slower but guaranteed.

2.2. Use a custom hash function that's harder to attack.

3.3. In Python, dict is generally resistant but not immune.