Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
Which statement about the KMP failure (lps) array of a pattern of length m is correct?
- A. It depends on the text being searched
- B. lps[i] can equal i + 1
- C. It can be built in O(m), and lps[i] ≤ i for every index i
- D. Building it takes O(m²) in the worst case
Correct answer: C. It can be built in O(m), and lps[i] ≤ i for every index i
Amortised analysis (k increases at most m times overall) gives O(m). lps[i] is a PROPER prefix length of p[0..i], so it is at most i. It depends only on the pattern.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [18…
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 200, 0, 200, 0…
- Items E, F, G, H, I, J are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many…
- One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j…
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…