Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-014

djb2 String Hash

Problem

Compute the djb2 string hash, a classic 32-bit hash function.

Start with h = 5381 and for every character update h = (h * 33 + ord(ch)) mod 2**32. Characters are processed from left to right and the empty string hashes to 5381.

Return the integer hash.

Hint

Multiply the running hash by 33 and add the character code, keeping it to 32 bits.

Test cases

case 1
in: [""]
expected: 5381
case 2
in: ["a"]
expected: 177670
case 3
in: ["ab"]
expected: 5863208
case 4
in: ["hello"]
expected: 261238937
Starter codePython
def djb2(s):
    # Your code here
    pass

4 test cases · run your code against them in the editor