Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: Compute the KMP prefix function (the longest proper prefix that is also a suffix, for each prefix) of the pattern "abacabab". What is the value at the last position (index 7)?
- A. 3
- B. 2
- C. 1
- D. 4
Correct answer: B. 2
The last prefix 'abacabab' has borders 'ab' (length 2) as its longest proper prefix-suffix match, so pi[7] = 2. Taking the single letter 'b' or the length of 'aba' would be the common slip.
More Coding Interview Problems questions
- Problem: Count the occurrences of the pattern "aaa" in the text "aaabaaaab", where occurrences may overlap (each starting index that…
- Problem: Round-robin scheduling with time quantum 3 is applied to processes that all arrive at time 0: P1 needs 7 units, P2 needs 4, P3…
- Problem: Josephus game: n = 37 people stand in a circle numbered 1 to 37. Starting from person 1, every 4th person is removed (counting…
- Problem: A deque starts empty. Operations in order: pushback(5), pushfront(2), pushback(8), popfront(), pushfront(9), pushback(1)…
- Problem: How many integers x in the range 1 ≤ x ≤ 1000 have exactly 4 set bits in their binary representation?
- Problem: A staircase can be climbed by steps of size 1, 3 or 4. How many distinct ordered sequences of steps climb exactly 13 stairs?