Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the number of inversions in the array [1, 3, 2, 5, 4, 6]? Run the code.
Problem: What is the number of inversions in the array [1, 3, 2, 5, 4, 6]? Run the code.
a = [1, 3, 2, 5, 4, 6] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] > a[j]))What is printed?
- A. 2
- B. 3
- C. 1
- D. 4
Correct answer: A. 2
Only the adjacent swapped pairs (3,2) and (5,4) form inversions.
More Coding Interview Problems questions
- Problem: What is the maximum sum of a subarray with at most one element deleted from [1, -2, 3, -4, 5]? Run the code. python a = [1, -2…
- Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code. python from itertools import product…
- Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code…
- Problem: What is the number of trailing zeros in 30!? Run the code. python n = 30 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c)…
- Problem: How many integers in [1, 500] are perfect squares or multiples of 7? Run the code. python print(sum(1 for x in range(1, 501) if…
- Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code…