Data Structures & AlgorithmsModerateTCS NQT · sliding window
Problem: What is the maximum sum of any 3 consecutive elements of [1, -2, 3, 4, -1]? Run the code.
Problem: What is the maximum sum of any 3 consecutive elements of [1, -2, 3, 4, -1]? Run the code.
a = [1, -2, 3, 4, -1] print(max(sum(a[i:i + 3]) for i in range(len(a) - 2)))What is printed?
- A. 5
- B. 6
- C. 2
- D. 7
Correct answer: B. 6
The windows sum to 2, 5 and 6 (3 + 4 - 1), so the maximum is 6.
More Coding Interview Problems questions
- Problem: How many integers in [1, 100] are perfect squares or perfect cubes? Run the code. python sq = {x x for x in range(1, 11)} cu = {x…
- Problem: What is the sum of the digits of 2^10 = 1024? Run the code. python print(sum(int(c) for c in str(2 10))) What is printed?
- Problem: What is the product of the distinct elements of [2, 2, 3, 4, 4]? Run the code. python a = [2, 2, 3, 4, 4] p = 1 for x in set(a)…
- Problem: What is the minimum absolute difference between two values of the sorted list [1, 5, 6, 9]? Run the code. python a = sorted([1…
- Problem: How many 2-element subsets of {1, 2, 3, 4, 5} have an even sum? Run the code. python from itertools import combinations…
- Problem: Which statement about the sparse table data structure for static range-minimum queries is correct?