Check whether two string lists have the same rolling-hash checksum

Quick Overview

Decide whether two lists of strings have equal checksums, where a checksum is the sum of base-31 polynomial rolling hashes taken modulo a large prime. Tests modular arithmetic, Horner's rule and overflow-safe hashing over many strings.

Check whether two string lists have the same rolling-hash checksum

Company: Discord

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two lists of strings `list1` and `list2`, determine whether they have the same checksum, as a way to verify that two copies of some data are consistent. The checksum of a list is the sum of the rolling hashes of its strings. The rolling hash of a string `s` of length `n` is $$ \text{hash}(s) = \text{ord}(s_0) \cdot 31^{n-1} + \text{ord}(s_1) \cdot 31^{n-2} + \cdots + \text{ord}(s_{n-1}) \cdot 31^{0} $$ where `ord(c)` is the ASCII code of the character `c`. All arithmetic is performed modulo $M = 10^9 + 7$. Return `True` if the two checksums are equal and `False` otherwise. ### Function Signature ```python def data_integrity_checker(list1: list[str], list2: list[str]) -> bool: ``` ### Rules - The hash of an empty string is 0. - The sum of the hashes is also taken modulo M, so each checksum is a value from 0 to M - 1. - The two lists may have different lengths. Only the two checksums are compared, so two lists with different contents can still have equal checksums. ### Constraints - `1 <= len(list1), len(list2) <= 10^4` - `0 <= len(list1[i]), len(list2[i]) <= 1000` - All strings consist of lowercase English letters. ### Examples ```text Input: list1 = ["hello","world"], list2 = ["hello","world"] Output: True ``` Both lists hold the same strings in the same order, so their hashes and their checksums are identical. ```text Input: list1 = ["abc","def"], list2 = ["abc","deg"] Output: False ``` `"def"` and `"deg"` differ only in the last character, whose multiplier is 1. The hash of `"deg"` is the hash of `"def"` plus 1 (99333 versus 99334), so the checksums differ. ```text Input: list1 = ["ab","ba"], list2 = ["aa","bb"] Output: True ``` The hashes are 3105 and 3135 for `list1` and 3104 and 3136 for `list2`. Both checksums are 6240, so the answer is `True` even though the lists hold different strings.

Overview: Decide whether two lists of strings have equal checksums, where a checksum is the sum of base-31 polynomial rolling hashes taken modulo a large prime. Tests modular arithmetic, Horner's rule and overflow-safe hashing over many strings.

Discord logo
Discord
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two lists of strings list1 and list2, determine whether they have the same checksum, as a way to verify that two copies of some data are consistent.

The checksum of a list is the sum of the rolling hashes of its strings. The rolling hash of a string s of length n is

hash(s)=ord(s0)⋅31n−1+ord(s1)⋅31n−2+⋯+ord(sn−1)⋅310\text{hash}(s) = \text{ord}(s_0) \cdot 31^{n-1} + \text{ord}(s_1) \cdot 31^{n-2} + \cdots + \text{ord}(s_{n-1}) \cdot 31^{0}

where ord(c) is the ASCII code of the character c. All arithmetic is performed modulo M=109+7M = 10^9 + 7.

Return True if the two checksums are equal and False otherwise.

Function Signature

def data_integrity_checker(list1: list[str], list2: list[str]) -> bool:

Rules

  • The hash of an empty string is 0.
  • The sum of the hashes is also taken modulo M, so each checksum is a value from 0 to M - 1.
  • The two lists may have different lengths. Only the two checksums are compared, so two lists with different contents can still have equal checksums.

Constraints

  • 1 <= len(list1), len(list2) <= 10^4
  • 0 <= len(list1[i]), len(list2[i]) <= 1000
  • All strings consist of lowercase English letters.

Examples

Input: list1 = ["hello","world"], list2 = ["hello","world"]
Output: True

Both lists hold the same strings in the same order, so their hashes and their checksums are identical.

Input: list1 = ["abc","def"], list2 = ["abc","deg"]
Output: False

"def" and "deg" differ only in the last character, whose multiplier is 1. The hash of "deg" is the hash of "def" plus 1 (99333 versus 99334), so the checksums differ.

Input: list1 = ["ab","ba"], list2 = ["aa","bb"]
Output: True

The hashes are 3105 and 3135 for list1 and 3104 and 3136 for list2. Both checksums are 6240, so the answer is True even though the lists hold different strings.