Data Structures & AlgorithmsModerateTCS NQT · string basics
Problem: How many distinct substrings of length 2 does 'abcabc' contain? Run the code.
Problem: How many distinct substrings of length 2 does 'abcabc' contain? Run the code.
s = 'abcabc'
print(len({s[i:i + 2] for i in range(len(s) - 1)}))What is printed?- A. 3
- B. 4
- C. 5
- D. 6
Correct answer: A. 3
The length-2 substrings are ab, bc, ca, ab, bc, so only 'ab', 'bc' and 'ca' are distinct.
More Coding Interview Problems questions
- Problem: What is the length of the longest valid (well-formed) parenthesis substring of ')()())'? Run the stack code. python s = ')()())'…
- Problem: What is the value of the reverse Polish expression '2 1 + 3 '? Run the stack code. python tokens = '2 1 + 3 '.split() st = [] for…
- Problem: What is the maximum matching size in the bipartite graph with left vertices {0,1,2}, right vertices {0,1,2} and edges (0,0)…
- Problem: How many integers x in [1, 100] satisfy x^2 ≡ 1 (mod 8)? Run the code. python print(sum(1 for x in range(1, 101) if (x x) % 8 ==…
- Problem: What is the smallest positive integer x with x ≡ 1 (mod 4) and x ≡ 2 (mod 5)? Run the code. python print(next(x for x in range(1…
- Problem: How many integers in [1, 100] have non-decreasing decimal digits (for example 1245 or 3399, but 2 digits only here)? Run the…