Data Structures & AlgorithmsHardProduct Company SDE · prefix-count OA
Problem: What is the minimum number of bit flips needed to make the binary string '00110' non-decreasing (all 0s then all 1s)? Run the code.
Problem: What is the minimum number of bit flips needed to make the binary string '00110' non-decreasing (all 0s then all 1s)? Run the code.
s = '00110'
print(min(s[:k].count('1') + s[k:].count('0') for k in range(len(s) + 1)))What is printed?- A. 2
- B. 0
- C. 3
- D. 1
Correct answer: D. 1
Flipping the final 0 to 1 gives 00111, which is sorted with a single flip. Each split point k costs the 1s to its left plus the 0s to its right.
More Coding Interview Problems questions
- Problem: How many pairs (i < j) of 'aabab' have s[i] = 'a' and s[j] = 'b'? Run the code. python s = 'aabab' print(sum(1 for i in…
- Problem: How many distinct products a[i] a[j] (i < j) does [1, 2, 3, 4] produce? Run the code. python from itertools import combinations…
- Problem: What is the smallest positive integer missing from [3, 4, -1, 1]? Run the code. python a = [3, 4, -1, 1] m = 1 while m in a: m +=…
- Problem: What is the sum of the lengths of all contiguous subarrays of [1, 2, 3]? Run the code. python a = [1, 2, 3] print(sum(len(a[i:j])…
- Problem: What is 6! mod 7, the value predicted by Wilson's theorem for a prime p = 7 (where (p-1)! ≡ -1 mod p, i.e. 6 mod 7)? Run the…
- Problem: How many integers in [1, 50] are divisible by both 2 and 3? Run the code. python print(sum(1 for x in range(1, 51) if x % 2 == 0…