Bird
0
0
DSA Cprogramming~20 mins

Rabin Karp String Matching in DSA C - 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 snippet that calculates the initial hash values for pattern and text substring?
DSA C
int prime = 101;
int d = 256;
int pattern_len = 3;
char pattern[] = "abc";
char text[] = "abcd";

int pattern_hash = 0, text_hash = 0, h = 1;

for (int i = 0; i < pattern_len - 1; i++)
    h = (h * d) % prime;

for (int i = 0; i < pattern_len; i++) {
    pattern_hash = (d * pattern_hash + pattern[i]) % prime;
    text_hash = (d * text_hash + text[i]) % prime;
}

printf("%d %d %d", pattern_hash, text_hash, h);
A7 6 15
B6 7 16
C5 6 15
D6 6 16
Attempts:
2 left
💡 Hint
Calculate hash step by step using modulo arithmetic with prime 101.
Predict Output
intermediate
2:00remaining
Rabin Karp rolling hash update output
What is the output of the rolling hash update step in Rabin Karp for the given text and pattern?
DSA C
int prime = 101;
int d = 256;
int pattern_len = 3;
char text[] = "abcd";

int h = 16; // precomputed as 256^(3-1) % 101
int text_hash = 6; // hash of 'abc'

// Update hash to next substring 'bcd'
text_hash = (d * (text_hash - text[0] * h) + text[3]) % prime;
if (text_hash < 0) text_hash += prime;

printf("%d", text_hash);
A7
B6
C8
D5
Attempts:
2 left
💡 Hint
Apply the rolling hash formula carefully and adjust for negative values.
🧠 Conceptual
advanced
2:00remaining
Why use a prime number in Rabin Karp hashing?
Why is a prime number used as the modulus in the Rabin Karp hashing algorithm?
ATo reduce the chance of hash collisions by distributing hash values uniformly
BTo make the hash calculation faster by using prime arithmetic
CTo ensure the hash values are always positive integers
DTo simplify the rolling hash update formula
Attempts:
2 left
💡 Hint
Think about how prime modulus affects hash distribution.
🔧 Debug
advanced
2:00remaining
Identify the error in this Rabin Karp substring search snippet
What error will this code snippet cause when searching for pattern in text using Rabin Karp?
DSA C
int prime = 101;
int d = 256;
int pattern_len = 3;
char pattern[] = "abc";
char text[] = "abcd";

int pattern_hash = 0, text_hash = 0, h = 1;

for (int i = 0; i < pattern_len - 1; i++)
    h = (h * d) % prime;

for (int i = 0; i < pattern_len; i++) {
    pattern_hash = (d * pattern_hash + pattern[i]) % prime;
    text_hash = (d * text_hash + text[i]) % prime;
}

for (int i = 0; i <= strlen(text) - pattern_len; i++) {
    if (pattern_hash == text_hash) {
        for (int j = 0; j < pattern_len; j++) {
            if (text[i + j] != pattern[j])
                break;
            if (j == pattern_len - 1)
                printf("Pattern found at index %d", i);
        }
    }
    if (i < strlen(text) - pattern_len) {
        text_hash = (d * (text_hash - text[i] * h) + text[i + pattern_len]) % prime;
        if (text_hash < 0) text_hash += prime;
    }
}
AThe break statement only exits the inner loop, causing incorrect matches
BThe inner loop may print multiple times for the same index
CThe code may access text out of bounds when updating text_hash
DThe pattern_hash is not computed correctly
Attempts:
2 left
💡 Hint
Check when the print statement executes inside the inner loop.
🚀 Application
expert
2:00remaining
Number of hash comparisons in worst case for Rabin Karp
Given a text of length N and a pattern of length M, what is the worst-case number of hash comparisons Rabin Karp algorithm performs?
AO(N - M + 1)
BO(N * M)
CO(M * (N - M + 1))
DO(1)
Attempts:
2 left
💡 Hint
Consider the worst case when many hash collisions occur.