Data Structures & AlgorithmsVery hardTCS CodeVita · Microsoft SDE OA
Array a = [3, 8, 2, 9, 5, 7, 6]. Count the index triples i < j < k such that the three values, sorted, are equally spaced (an arithmetic progression). Run the brute-force code.
Array a = [3, 8, 2, 9, 5, 7, 6]. Count the index triples i < j < k such that the three values, sorted, are equally spaced (an arithmetic progression). Run the brute-force code.
from itertools import combinations a=[3,8,2,9,5,7,6] print(sum(1 for t in combinations(a,3) if sorted(t)[1]-sorted(t)[0]==sorted(t)[2]-sorted(t)[1]))What is printed?
- A. 5
- B. 2
- C. 7
- D. 0
Correct answer: C. 7
Sort the three values and check that the two gaps are equal. Testing the spacing in array order rather than sorted order is a different condition, counting increasing triples ignores spacing, and comparing the outer span with one gap is a misread condition.
More Coding Interview Problems questions
- Count the binary strings of length 10 that contain no two adjacent 1s and also contain a run of at least three consecutive 0s. Brute-force…
- Count the pairs (a, b) with 1 ≤ a < b ≤ 20 such that a OR b (bitwise) equals 31. Run the code. python print(sum(1 for a in range(1,21) for…
- Floyd–Warshall is run on the 4-vertex directed weighted graph with matrix rows [0,3,∞,7], [8,0,2,∞], [5,∞,0,1], [2,∞,∞,0]. Sum the final…
- A rod of length 8 can be cut into integer pieces, and a piece of length L sells for p[L] with p = [1, 5, 8, 9, 10, 17, 17, 20] for L =…
- Array b = [1, 11, 2, 10, 4, 5, 2, 1]. Find the length of its longest bitonic subsequence: a subsequence that strictly increases and then…
- Count the 4-element subsets of {1, 2, ..., 12} whose sum is divisible by 5. Count by brute force over combinations. python from itertools…