Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many pairs (i < j) of [1, 4, 9, 16, 25] have a[j] - a[i] divisible by 3? Run the code.
Problem: How many pairs (i < j) of [1, 4, 9, 16, 25] have a[j] - a[i] divisible by 3? Run the code.
a = [1, 4, 9, 16, 25] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if (a[j] - a[i]) % 3 == 0))What is printed?
- A. 12
- B. 2
- C. 6
- D. 0
Correct answer: C. 6
Residues mod 3 are 1, 1, 0, 1, 1, so the four 1s give C(4,2) = 6 pairs. Ordered pairs give 12, adjacent pairs only give 2, and testing the sum instead of the difference gives 0.
More Coding Interview Problems questions
- Problem: What is the number of set bits in 2^30 - 1? Run the code. python print(bin(2 30 - 1).count('1')) What is printed?
- Problem: How many integers in [1, 300] are multiples of 6 but not of 9? Run the code. python print(sum(1 for x in range(1, 301) if x % 6…
- Problem: How many ordered triples (a, b, c) of positive integers satisfy a + b + c = 7? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the largest possible value of gcd(a, b) when a + b = 60 and a != b with a, b positive? Run the code. python from math…
- Problem: What is the complexity of a single query in a sparse-table range minimum structure?
- Problem: What is the purpose of a 'suffix link' in Ukkonen's or Aho-Corasick's construction?