Data Structures & AlgorithmsHardProduct Company SDE · sliding window OA
Problem: Using at most one flip of a 0 to 1, what is the longest run of consecutive 1s in [1, 0, 1, 1, 0, 1]? Run the sliding-window code.
Problem: Using at most one flip of a 0 to 1, what is the longest run of consecutive 1s in [1, 0, 1, 1, 0, 1]? Run the sliding-window code.
a = [1, 0, 1, 1, 0, 1]
l = best = zeros = 0
for r, x in enumerate(a):
if x == 0:
zeros += 1
while zeros > 1:
if a[l] == 0:
zeros -= 1
l += 1
best = max(best, r - l + 1)
print(best)What is printed?- A. 3
- B. 4
- C. 5
- D. 6
Correct answer: B. 4
Flipping the 0 at index 4 gives the run 1,1,1,1 at indices 2 to 5, of length 4. The window tracks at most one zero.
More Coding Interview Problems questions
- Problem: How many unordered pairs of words in ['abc', 'bca', 'xyz', 'cab'] are anagrams of each other? Run the code. python from itertools…
- Problem: How many subsets of [1, 2, 3, 4, 5] have a sum of exactly 7? Run the code. python from itertools import combinations a = [1, 2…
- Problem: What is the length of the longest arithmetic subsequence of [1, 3, 5, 7, 4]? Run the O(n^2) DP code. python a = [1, 3, 5, 7, 4] n…
- Problem: What is the minimum path sum from the top to the bottom of the triangle [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]], moving to an…
- Problem: How many unordered pairs (i < j) of 1..10 have a sum divisible by 5? Run the code. python a = list(range(1, 11)) print(sum(1 for…
- Problem: Minimum number of operations to reduce x = 5 to 0 by removing a number from either end of [1, 1, 4, 2, 3] (each removal subtracts…