Home/Data Structures & Algorithms/Hashing & Collision Resolution

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Robin Hood hashing, consistent hashing, Cuckoo hashing, Bloom filters, and HashDoS defenses.
Subarray Sum Equals K: Why Two Pointers fails with negative numbers and Hash Map is mandatory
This is a classic trap that catches even intermediate developers. Let's see why two pointers collapse and why prefix math is the ultimate solution. 1. Why Two Pointers Fails on Negative Numbers A sliding window relies on a fundamental monotonic invariant: If the current sum is too small, expanding tRead more
This is a classic trap that catches even intermediate developers. Let’s see why two pointers collapse and why prefix math is the ultimate solution.
1. Why Two Pointers Fails on Negative Numbers
A sliding window relies on a fundamental monotonic invariant:
The moment you introduce negative numbers, this invariant is destroyed! Expanding the right pointer might add
-10, making the sum smaller. Shrinking the left pointer might drop-5, making the sum larger. You can no longer make greedy left/right decisions!2. The Prefix Sum Invariant
Let
prefix[i]be the cumulative sum from index 0 toi.The sum of any contiguous subarray from index
j + 1toiis given by:sum(j+1 ... i) = prefix[i] - prefix[j].We want this subarray sum to equal
k:The Breakthrough: As you iterate through the array maintaining a running prefix sum
curr_sum, you simply ask the hash map: ‘How many times have we already seen a prefix sum equal tocurr_sum - kin the past?’Every time you find that value in the hash map, you have found a valid subarray that sums exactly to
k!Clean Python 3.12 Implementation
Why prefix_counts[0] = 1 is Critical
If you forget
prefix_counts[0] = 1, any subarray that starts at index 0 and sums tok(e.g.nums = [3, ...], k = 3) will producecurr_sum = 3, and look forcurr_sum - k = 0in the map. Without the base case, it would fail to count that valid subarray!Complexity Breakdown
- Time Complexity:
- Space Complexity:
See lessO(N). Single pass through the array withO(1)hash map lookups.O(N)to store prefix sum frequencies.How to implement Consistent Hashing with Virtual Nodes to eliminate hot spots in distributed caches?
Consistent Hashing is one of the foundational building blocks of distributed systems (used in Apache Cassandra, Amazon DynamoDB, Akamai CDN, and Envoy Proxy). 1. The Problem with Naive Modulo Hashing If you have 4 servers and use hash(key) % 4, when server 4 crashes, you now compute hash(key) % 3. BRead more
Consistent Hashing is one of the foundational building blocks of distributed systems (used in Apache Cassandra, Amazon DynamoDB, Akamai CDN, and Envoy Proxy).
1. The Problem with Naive Modulo Hashing
If you have 4 servers and use
hash(key) % 4, when server 4 crashes, you now computehash(key) % 3. Because almost every number changes its remainder modulo 3, nearly 100% of cached keys instantly miss, hammering your backend database in a catastrophic thundering herd.In Consistent Hashing, when a server is added or removed, only $1/N$ of keys need to be remapped on average. All other keys stay on their existing servers!
2. The Hash Ring & Why Virtual Nodes are Mandatory
Imagine a circular ring of numbers from $0$ to $2^{32} – 1$ (the output space of a 32-bit hash function like Murmur3 or MD5).
The Hot Spot Problem: If you only place 3 physical servers on the ring, their hash positions might be clustered close together (e.g. at 10 degrees, 25 degrees, and 280 degrees). Server 3 will end up handling 70% of all traffic, causing a massive hot spot!
The Solution (Virtual Nodes): Instead of hashing Server A once, we hash it 100 or 200 times under different labels (
"server-A#1","server-A#2", …,"server-A#200"). By distributing hundreds of virtual replicas uniformly across the 360-degree ring, standard deviations drop to near zero, and load is balanced evenly across all physical hardware.Production Python 3.12 Implementation with bisect
Complexity Breakdown
- Key Lookup:
- Node Add/Remove:
See lessO(log(R * N))using binary search, whereRis replicas (e.g. 150) andNis physical servers (e.g. 10). Searching an array of 1,500 numbers takes 11 comparisons (< 1 microsecond).O(R * log(R * N)). Adding a server only migrates keys from its immediate clockwise neighbor!