Data Structures & AlgorithmsModerateTCS NQT · Infosys
For the values [2, 5, 3, 8, 1, 6], how many values give remainder 1 or 2 when divided by 3 (counted once for each value)?
- A. 4
- B. 1
- C. 3
- D. 6
Correct answer: A. 4
Compute x mod 3 for each value: 2, 2, 0, 2, 1, 0. Counting remainders 1 or 2 gives four values; the trap is counting only the remainder-1 class or the complementary set.
More Coding Interview Problems questions
- Array [4, 4, 7, 1, 7, 2]. How many index pairs i < j have equal values a[i] = a[j]?
- Array [3, 7, 2, 9, 4]. How many contiguous subarrays sum to exactly 9?
- Problem: Fresh oranges (1) rot their neighbours (4-directional) one minute at a time; 0 is empty and 2 is rotten. How many minutes until…
- Problem: How many distinct palindromic substrings does 'aaaa' have? Run the code. python s = 'aaaa' print(len({s[i:j] for i in…
- Problem: Count the start indices i where a permutation of 'abc' begins in s = 'cbaebabacd' (the anagram-window problem). Run the code…
- Problem: What is the length of the shortest substring of 'aabbcxa' that contains every letter of 'abc'? Run the sliding-window code…