Data Structures & AlgorithmsHardGATE CS · ICPC regional

A polynomial hash is H(s) = (Σ ord(s[i]) · B^(n−1−i)) mod M, computed left to right as h = (h·B + ord(c)) mod M, with B = 31 and M = 1000003. What is H("dsa")?

  1. A. 96882
  2. B. 99762
  3. C. 99763
  4. D. 312

Correct answer: B. 99762

Horner's rule computes the polynomial in one pass: each character multiplies the running hash by B then adds its code. Reversing the string changes which character gets the highest power, so the hash of the reversed string is a different number.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic