0
0
DSA Pythonprogramming~20 mins

Substring Search Patterns in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Substring Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of naive substring search count
What is the output of the following code that counts occurrences of a pattern in a text using a naive approach?
DSA Python
def naive_search(text, pattern):
    count = 0
    for i in range(len(text) - len(pattern) + 1):
        match = True
        for j in range(len(pattern)):
            if text[i + j] != pattern[j]:
                match = False
                break
        if match:
            count += 1
    return count

print(naive_search('abababa', 'aba'))
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Check all possible starting positions where 'aba' can appear in 'abababa'.
🧠 Conceptual
intermediate
1:30remaining
Understanding the prefix function in KMP algorithm
What does the prefix function (also called the failure function) in the Knuth-Morris-Pratt (KMP) substring search algorithm represent?
AThe length of the longest proper prefix of the pattern that is also a suffix for each prefix of the pattern
BThe number of times the pattern appears in the text
CThe index where the pattern first appears in the text
DThe length of the pattern
Attempts:
2 left
💡 Hint
Think about what helps KMP avoid rechecking characters after a mismatch.
Predict Output
advanced
2:30remaining
Output of Rabin-Karp hash matches
What is the output of this Rabin-Karp substring search code that prints all starting indices where the pattern matches the text?
DSA Python
def rabin_karp(text, pattern):
    d = 256
    q = 101
    n = len(text)
    m = len(pattern)
    h = pow(d, m-1, q)
    p = 0
    t = 0
    result = []

    for i in range(m):
        p = (d * p + ord(pattern[i])) % q
        t = (d * t + ord(text[i])) % q

    for i in range(n - m + 1):
        if p == t:
            if text[i:i+m] == pattern:
                result.append(i)
        if i < n - m:
            t = (d * (t - ord(text[i]) * h) + ord(text[i + m])) % q
            if t < 0:
                t += q
    return result

print(rabin_karp('abracadabra', 'abra'))
A[1, 7]
B[0, 3]
C[3, 7]
D[0, 7]
Attempts:
2 left
💡 Hint
Check where 'abra' appears in 'abracadabra'.
🔧 Debug
advanced
2:00remaining
Identify the error in this KMP prefix function code
What error does this prefix function code for KMP produce when run?
DSA Python
def prefix_function(pattern):
    pi = [0] * len(pattern)
    k = 0
    for q in range(1, len(pattern)):
        while k > 0 and pattern[k] != pattern[q]:
            k = pi[k-1]
        if pattern[k] == pattern[q]:
            k += 1
        pi[q] = k
    return pi

print(prefix_function('ababc'))
AIndexError: list index out of range
B[0, 0, 1, 2, 0]
C[0, 0, 1, 2, 1]
DTypeError: unsupported operand type(s)
Attempts:
2 left
💡 Hint
Check the line where k is updated inside the while loop.
🚀 Application
expert
3:00remaining
Minimum window substring containing all pattern characters
Given a text and a pattern, which algorithmic approach efficiently finds the smallest substring in the text that contains all characters of the pattern (including duplicates)?
AKMP algorithm for exact pattern matching
BNaive substring search checking all substrings
CSliding window with two pointers and a frequency map
DRabin-Karp algorithm with rolling hash
Attempts:
2 left
💡 Hint
Think about how to track counts of characters while expanding and shrinking a window.