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- A. 354
- B. 91
- C. 1026
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The string "bca" may have exactly one swap of two characters made. The procedure below finds the lexicographically smallest string…
- The phrase "the cat and the hat" is searched for the whole word "the" (counted as a separate word). The procedure below counts the…
- Strings are run-length compressed. The procedure below counts the number of runs in "aaabbc" (a run is a maximal block of equal…
- The procedure below checks the bracket string "(()())" and prints the maximum nesting depth it reaches. The depth is raised by '(' and…
- The sentence "to be or not to be to" is split into words, and the procedure below prints the most frequent word (ties go to the first word…
- The string "aabbc" is examined by the procedure below, which counts the substrings (contiguous pieces, counted by position) that contain…