Count Numbers Inside All Intervals
Company: Squarespace
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to compute intersections of numeric intervals and perform efficient counting over an array, testing competencies in range handling, multiplicity accounting, and time-complexity-aware algorithm design.
Constraints
- 0 <= len(nums) <= 200000
- 1 <= len(intervals) <= 200000
- -1000000000 <= nums[i], start, end <= 1000000000
- Each interval satisfies start <= end
Examples
Input: ([1, 3, 5, 7], [[2, 6], [1, 5], [3, 8]])
Expected Output: 2
Explanation: The intersection of all intervals is [3, 5]. The numbers 3 and 5 are inside it, so the answer is 2.
Input: ([2, 2, 3, 4, 5], [[1, 4], [2, 6], [2, 3]])
Expected Output: 3
Explanation: The common overlap is [2, 3]. The matching values are 2, 2, and 3. Duplicates are counted separately.
Input: ([1, 2, 3], [[1, 1], [2, 2]])
Expected Output: 0
Explanation: The largest start is 2 and the smallest end is 1, so there is no common overlap.
Input: ([], [[0, 10], [-5, 5]])
Expected Output: 0
Explanation: There are no numbers to count, so the result is 0.
Input: ([-3, -1, 0, 2], [[-2, 1]])
Expected Output: 2
Explanation: With only one interval, the overlap is [-2, 1]. The numbers -1 and 0 are inside it.
Hints
- A number belongs to every interval only if it lies in the common intersection of all intervals.
- Track the largest interval start and the smallest interval end, then count how many numbers fall within that inclusive range.