🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: String Matching — KMP and Rabin-Karp: all 10 questions

Quiz: String Matching — KMP and Rabin-Karp · Question 8 of 10

What does this print? (code)

What does this print?

text, m = "4567", 2
h = int(text[:m])
values = [h]
for i in range(1, len(text) - m + 1):
    h = (h - int(text[i - 1]) * 10) * 10 + int(text[i + m - 1])
    values.append(h)
print(values)
  1. A.[45, 56, 67]
  2. B.[45, 46, 47]
  3. C.[4, 5, 6]
  4. D.[45, 560, 670]
Answer & explanation

Correct answer: [45, 56, 67]

Each step removes the first digit's tens and adds the next digit: 45 → 56 → 67.

Can you get the rest right without peeking?

Take the timed Quiz: String Matching — KMP and Rabin-Karp quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →