Data Structures & AlgorithmsVery hardTCS NQT · Infosys
What does this program print?
What does this program print?
from collections import Counter a = [3, 1, 2, 3, 1, 2, 3] c = Counter(a) pairs = sum(v * (v - 1) // 2 for v in c.values()) print(pairs, c.most_common(1)[0][0])
- A. 8 3
- B. 5 3
- C. 6 3
- D. 5 2
Correct answer: B. 5 3
Equal values form groups, and a group of size v contributes C(v,2) index pairs: 3 appears three times and 1 and 2 twice each, so the total is 3 + 1 + 1 = 5. The most common value is 3, so the output is 5 3.
More Coding Interview Problems questions
- What does this program print? python a = [6, 3, 8, 1] mx = [0] len(a) mx[-1] = a[-1] for i in range(len(a) - 2, -1, -1): mx[i] = max(a[i]…
- Array a = [7, -3, 2, 5, -8, 4, 1, -1]. How many contiguous subarrays have a sum divisible by 3? (Zero is divisible by 3.)
- Sorted list [3, 6, 9, 12, 15, 18, 21]. How many index triples i < j < k satisfy vals[i] + vals[j] = vals[k]?
- How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] sum to exactly 15?
- Array a = [9, 4, 7, 1, 8, 2, 6, 3]. How many index pairs i < j have |a[i] - a[j]| >= 5?
- Among [5, 3, 6, 12, 9, 10, 1], choose two different elements. What is the largest possible value of x OR y?