Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Overlapping versus non-overlapping substring counts. What is printed?
Overlapping versus non-overlapping substring counts. What is printed?
s, p = "bbaabbbabaaa", "aa" over = sum(1 for i in range(len(s) - len(p) + 1) if s[i:i + len(p)] == p) print(s.count(p), over, s.find(p, 3))
- A. 1 3 9
- B. 2 2 9
- C. 3 2 9
- D. 2 3 9
Correct answer: D. 2 3 9
str.count counts NON-overlapping occurrences scanning left to right, while the window loop counts overlapping ones; find(p, 3) searches from index 3. Output: 2 3 9.
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 sorted array was rotated to give [35, 45, 52, 68, 78, 1, 5, 12, 18, 27, 28]. Binary search finds the index of the minimum: while lo <…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 5 positions and then LEFT by 7 positions. What is the final array?
- Keys 28, 19, 53, 91, 92, 20, 74, 89 are inserted in this order into an empty hash table of size 10 with h(k) = k mod 10 and linear…
- Print a matrix in spiral order (right, down, left, up, repeat). What is printed? python m = [[10, 41, 52, 63], [20, 51, 42, 53], [40, 41…
- Binary search with mid = (lo + hi) // 2 (lo = 0, hi = n − 1, loop while lo ≤ hi) looks for 86 in [11, 13, 17, 24, 25, 32, 36, 58, 65, 86…
- Rabin–Karp with a good rolling hash is used to find a pattern of length m in a text of length n. Which statement is correct?