Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: Count the pairs (i, j) with i < j in the array [1, 2, ..., 10] whose sum is divisible by 4.
- A. 10
- B. 15
- C. 20
- D. 7
Correct answer: A. 10
Group 1..10 by residue mod 4: r0 = {4, 8}, r1 = {1, 5, 9}, r2 = {2, 6, 10}, r3 = {3, 7}; valid pairs are C(2,2) + C(3,2) + 3·2 = 1 + 3 + 6 = 10. Letting j start at i adds 5 self-pairs (15), counting ordered pairs gives 20, and forgetting the r2 + r2 pairs gives 7.
More Coding Interview Problems questions
- Problem: Find the majority element (appearing more than half the time) of [2, 2, 1, 1, 1, 2, 2] using the Boyer–Moore voting algorithm…
- Problem: Kadane's algorithm finds the maximum sum of a non-empty contiguous subarray of [−2, 1, −3, 4, −1, 2, 1, −5, 4]. What is that…
- Problem: The array [1, 2, 3, 4, 5, 6, 7] is rotated right by 3 positions (the last 3 elements move to the front). What is the element at…
- Problem: Merging two sorted lists [1, 4, 7] and [2, 3, 8, 9] with the standard two-pointer merge, how many element comparisons are made in…
- Problem: In the expression print(232, (23)2) what are the two values printed, in order?
- Problem: What does this print? x = [1, 2, 3] y = x y += [4] z = x[:] z.append(5) print(len(x), len(z))