Data Structures & AlgorithmsModerateCAT · TCS NQT
Problem: How many integers from 1 to 100 are divisible by exactly one of 2 and 3?
- A. 67
- B. 83
- C. 51
- D. 16
Correct answer: C. 51
Multiples of 2: 50, of 3: 33, of both (6): 16, so exactly one is 50 + 33 − 2·16 = 51. The union is 67, ignoring the overlap gives 83, and 16 counts the numbers divisible by both.
More Coding Interview Problems questions
- Problem: What is the last non-zero decimal digit of 10! (3628800)?
- Problem: Five people (including Alice and Bob) stand in a row. In how many arrangements do Alice and Bob stand next to each other?
- Problem: The array [3, 1, 2] undergoes one pass of bubble sort (compare each adjacent pair from left to right and swap if out of order)…
- Problem: In the array [2, 9, 4, 7, 1, 8], find the 4th smallest element using the quickselect idea (its final answer is the value that…
- Problem: In the sorted array [1, 2, 4, 7, 8, 11, 13], how many pairs (i, j) with i < j have a[j] − a[i] ≤ 2?
- Problem: Piles of bananas are [3, 6, 7, 11] and you must finish all of them within 8 hours. Each hour you eat up to k bananas from one…