Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The union of the intervals [1, 5], [4, 8], [10, 12], [11, 15], [20, 22] covers some integer points. How many integers lie in the union (the endpoints are included)?
- A. 17
- B. 14
- C. 21
- D. 22
Correct answer: A. 17
Sorted merging gives [1, 8], [10, 15] and [20, 22], covering 8 + 6 + 3 = 17 integers. Using lengths e − s instead of e − s + 1 gives 14, adding the five intervals without merging gives 21, and the span 1..22 counts the gaps (22).
More Coding Interview Problems questions
- Problem: Given the intervals [1,4], [2,6], [3,5], [5,8], [7,9], [8,10] (closed, integer endpoints), what is the largest number of…
- Problem: A functional graph has n = 9 nodes where node i points to f(i) given by the list [1, 2, 0, 4, 5, 3, 3, 6, 7] (node i has exactly…
- Problem: Matrix chain multiplication: the dimensions of three matrices are given by the array p = [10, 30, 5, 60], meaning A1 is 10×30, A2…
- Problem: Maximum profit with at most two buy-sell transactions (one stock, you must sell before buying again) for the daily prices [3, 3…
- Problem: How many pairs i < j in the array [2, 8, 9, 18, 4, 32, 50, 72] have a product a[i] × a[j] that is a perfect square?
- Problem: How many three-digit numbers (100 to 999) have strictly increasing digits from left to right (for example 147 qualifies, 151 does…