ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
h = 0
for ch in 'abc':
    h = (h * 31 + ord(ch)) % 1000
print(h)
  1. A. 990
  2. B. 3
  3. C. 1000
  4. D. 354

Correct answer: D. 354

The rolling hash goes 97, then (97 * 31 + 98) mod 1000 = 105, then (105 * 31 + 99) mod 1000 = 354.

More Python Programming questions

Exams that ask this topic