PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • easy
  • Squarespace
  • Coding & Algorithms
  • Software Engineer

Count Numbers Inside All Intervals

Company: Squarespace

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are given: 1. An integer array `nums`. 2. A list of intervals `intervals`, where each interval is represented as `[start, end]`. The intervals are not sorted. Count how many elements in `nums` belong to **every** interval. Additional assumptions for clarity: - Interval boundaries are inclusive. - Each interval satisfies `start <= end`. - If a value appears multiple times in `nums`, count each occurrence separately. Example: - `nums = [1, 3, 5, 7]` - `intervals = [[2, 6], [1, 5], [3, 8]]` The overlap of all intervals is `[3, 5]`, so the valid numbers are `3` and `5`. The answer is `2`. Design an efficient algorithm to solve this problem.

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.

You are given an integer array `nums` and a list of inclusive intervals `intervals`, where each interval is represented as `[start, end]`. The intervals are not sorted. Count how many elements in `nums` belong to every interval. A value should be counted once for each time it appears in `nums`. If the common overlap of all intervals is empty, the answer is `0`.

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

  1. A number belongs to every interval only if it lies in the common intersection of all intervals.
  2. Track the largest interval start and the smallest interval end, then count how many numbers fall within that inclusive range.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.