ProgrammingVery hardCAT · MBA placement

A polynomial rolling hash of the string "abc" is computed as h = (h 31 + value) mod 1000, starting from h = 0, where value is the position of the letter in the alphabet (a = 1, b = 2, c = 3). What is printed?

A polynomial rolling hash of the string "abc" is computed as h = (h * 31 + value) mod 1000, starting from h = 0, where value is the position of the letter in the alphabet (a = 1, b = 2, c = 3). What is printed?
h = 0
for each letter c in "abc"
    h = (h * 31 + value(c)) mod 1000
end for
Print h
  1. A. 354
  2. B. 91
  3. C. 1026
  4. D. 26

Correct answer: D. 26

Going through the letters gives h = 1, then 33, then 1026, and reducing mod 1000 gives 26. Using code points instead of alphabet positions gives 354, base 32 gives 91, and printing the unreduced value 1026 is a different answer.

More Pseudocode and Code Tracing questions

Exams that ask this topic