Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Z-array: z[i] is the length of the longest common prefix of s and s[i:], with z[0] defined as 0. What is printed?
Z-array: z[i] is the length of the longest common prefix of s and s[i:], with z[0] defined as 0. What is printed?
s = "aaaaabaaa"
z = [0] * len(s)
for i in range(1, len(s)):
while i + z[i] < len(s) and s[z[i]] == s[i + z[i]]: z[i] += 1
print(z, sum(z))- A. [0, 4, 3, 1, 2, 0, 3, 2, 1] 16
- B. [0, 4, 3, 2, 0, 1, 3, 2, 1] 16
- C. [0, 4, 3, 2, 1, 0, 3, 2, 1] 4
- D. [0, 4, 3, 2, 1, 0, 3, 2, 1] 16
Correct answer: D. [0, 4, 3, 2, 1, 0, 3, 2, 1] 16
Each z[i] counts how far s[i:] agrees with the start of s. Output: [0, 4, 3, 2, 1, 0, 3, 2, 1] 16.
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
- A Python 2-D list is created two ways and one cell is set. What is printed? python a = [[0] 3] 3 b = [[0] 3 for in range(3)] a[0][1] = 5…
- An array of size 9 holds the numbers 1..9, except that one number is missing and one number appears twice: [6, 5, 1, 3, 4, 7, 2, 8, 2]…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "aababaaabbba", "aba" over = sum(1 for i in…
- Bars of width 1 have heights [2, 3, 1, 5, 2, 1, 1, 6]. What is the area of the largest rectangle that fits under the histogram (computed…
- For an array of n elements, how many contiguous subarrays are there, and how many subsequences (including the empty one)?
- A monotonic deque reports the maximum of every window of size 3 in [19, 6, 7, 16, 10, 2, 12, 8, 17]. What is the SUM of all reported…