0
0
DSA Pythonprogramming~20 mins

Rabin Karp String Matching in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rabin Karp Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Rabin Karp hash calculation
What is the output of the following code that calculates the initial hash values for pattern and text window?
DSA Python
def initial_hash(s, length, base=256, mod=101):
    h = 0
    for i in range(length):
        h = (h * base + ord(s[i])) % mod
    return h

pattern = "abc"
text = "abcd"
pattern_hash = initial_hash(pattern, len(pattern))
text_hash = initial_hash(text, len(pattern))
print(pattern_hash, text_hash)
A87 88
B94 95
C89 90
D85 86
Attempts:
2 left
💡 Hint
Calculate hash by multiplying previous hash by base and adding ASCII value, then mod by 101.
Predict Output
intermediate
2:00remaining
Rabin Karp rolling hash update output
What is the output of the rolling hash update after sliding the window by one character?
DSA Python
def rolling_hash(old_hash, left_char, right_char, base=256, mod=101, pattern_len=3):
    h = old_hash - ord(left_char) * (base ** (pattern_len - 1))
    h = (h * base + ord(right_char)) % mod
    return h

old_hash = 88
new_hash = rolling_hash(old_hash, 'a', 'd')
print(new_hash)
A23
B25
C24
D26
Attempts:
2 left
💡 Hint
Subtract left char's weighted value, multiply by base, add new char, then mod.
🧠 Conceptual
advanced
1:30remaining
Why use modulo in Rabin Karp hashing?
Why do we use modulo operation in the Rabin Karp hashing algorithm?
ATo increase the hash value exponentially
BTo keep hash values within a fixed range and avoid integer overflow
CTo convert characters to ASCII values
DTo sort the pattern and text before matching
Attempts:
2 left
💡 Hint
Think about large numbers and computer memory limits.
🔧 Debug
advanced
2:30remaining
Identify the error in this Rabin Karp implementation snippet
What error will this code produce when running the Rabin Karp algorithm?
DSA Python
def rabin_karp_search(text, pattern):
    base = 256
    mod = 101
    m = len(pattern)
    n = len(text)
    hpattern = 0
    htext = 0
    h = 1

    for i in range(m-1):
        h = (h * base) % mod

    for i in range(m):
        hpattern = (base * hpattern + ord(pattern[i])) % mod
        htext = (base * htext + ord(text[i])) % mod

    for i in range(n - m + 1):
        if hpattern == htext:
            if text[i:i+m] == pattern:
                return i
        if i < n - m:
            htext = (base * (htext - ord(text[i]) * h) + ord(text[i+m])) % mod
    return -1

print(rabin_karp_search("hello", "ll"))
AReturns 2
BReturns -1
CTypeError
DIndexError
Attempts:
2 left
💡 Hint
Check the loop ranges and hash updates carefully.
🚀 Application
expert
3:00remaining
Number of hash comparisons in worst case
In the worst case, how many hash comparisons does the Rabin Karp algorithm perform when searching a pattern of length m in a text of length n?
AO(n - m + 1)
BO(n * m * log m)
CO(1)
DO(m * (n - m + 1))
Attempts:
2 left
💡 Hint
Consider when many hash collisions happen and substring checks occur.