Data Structures & AlgorithmsHardICPC regional · string round
Problem: Which claim about the Knuth-Morris-Pratt algorithm is correct?
- A. It is O(n * m) in the worst case because of the fallback loop
- B. It needs O(n^2) preprocessing of the pattern
- C. It only works when the pattern has no repeated characters
- D. Its total work is O(n + m) because the pattern pointer never moves back more than it has moved forward
Correct answer: D. Its total work is O(n + m) because the pattern pointer never moves back more than it has moved forward
Each text character advances the text pointer once, and each failure only falls back along the prefix function, which the amortised argument bounds by the total advances. The fallback is not a per-character quadratic step.
More Coding Interview Problems questions
- Problem: A player picks a number from 1 to 10 and the other player must guess it by yes/no questions. The minimum worst-case number of…
- Problem: What is the complexity of computing all prefix XOR values of an array of n = 10^6 integers and then answering q = 10^6 range-XOR…
- Problem: Consider a DP over subsets for assigning n jobs to n workers (cost matrix given), with state = mask of assigned jobs and…
- Problem: A string of length n is built using the operation 'append the reverse of the current string'. Starting with 'a', the length after…
- Problem: Which statement about Dijkstra's algorithm using a binary heap with lazy deletion (pushing duplicate entries and skipping stale…
- Problem: What does the following print? python x = 5 print(x << 3 | x >> 1, x & 6, x ^ 3)