Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-316

djb2 Hash

Problem

Compute the djb2 hash of a string: start from 5381 and for each character update the hash as hash times 33 plus the character code, keeping only the low 32 bits.

Multiplying by 33 is written as a left shift of five plus the value itself. The empty string hashes to 5381.

Hint

The result is masked to 32 bits after every character.

Test cases

case 1
in: [""]
expected: 5381
case 2
in: ["a"]
expected: 177670
case 3
in: ["hello"]
expected: 261238937
case 4
in: ["abc"]
expected: 193485963
case 5
in: ["foobar"]
expected: 4259602622
Starter codePython
def djb2_hash(data):
    # Your code here
    pass

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