Data Structures & AlgorithmsModerateTCS NQT · hashing warm-up
Problem: How many pairs of equal values (i < j, a[i] = a[j]) does a = [1, 2, 1, 3, 2, 2] contain? Run the code.
Problem: How many pairs of equal values (i < j, a[i] = a[j]) does a = [1, 2, 1, 3, 2, 2] contain? Run the code.
from collections import Counter a = [1, 2, 1, 3, 2, 2] print(sum(v * (v - 1) // 2 for v in Counter(a).values()))What is printed?
- A. 4
- B. 3
- C. 5
- D. 6
Correct answer: A. 4
The value 1 appears twice (1 pair) and 2 appears three times (3 pairs), giving 4. Counting ordered pairs would double the answer.
More Coding Interview Problems questions
- Problem: How many longest increasing subsequences does [1, 3, 5, 4, 7] have? Run the DP that counts both length and number of optimal…
- Problem: What is the largest number of distinct characters in any window of length 3 of 'abcabc'? Run the code. python s = 'abcabc'…
- Problem: How many distinct values does x XOR y take over pairs of distinct elements of [1, 2, 3]? Run the code. python from itertools…
- Problem: What does the following print? It sums the entries at even positions of the array sorted in descending order. python a = [5, 1…
- Problem: What is the maximum sum of non-adjacent elements of [3, 2, 7, 10]? Run the DP. python a = [3, 2, 7, 10] inc = exc = 0 for x in a…
- Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code. python f = [1, 1] for in range(3)…