PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates a candidate's ability to reason about interval overlap counting, manage range endpoints efficiently, and balance time-space trade-offs when identifying points that maximize coverage.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Find integer in most intervals

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

##### Question Given N integer intervals [ai, bi] (positive integers), find an integer that belongs to the maximum number of intervals (return any such integer if several exist). Follow-up: When every endpoint is < M and M is small, how would you optimize?

Quick Answer: This question evaluates a candidate's ability to reason about interval overlap counting, manage range endpoints efficiently, and balance time-space trade-offs when identifying points that maximize coverage.

You are given N closed intervals of positive integers, intervals[i] = [ai, bi] with ai <= bi. Find an integer x that lies in the maximum number of intervals. If multiple integers achieve this maximum coverage, return the smallest such integer.

Constraints

  • 1 <= N <= 200000
  • 1 <= ai <= bi <= 10^9
  • Intervals are closed: x is covered by [a,b] if a <= x <= b
  • If multiple integers tie for maximum coverage, return the smallest integer

Solution

from typing import List, Dict

def most_overlapped_point(intervals: List[List[int]]) -> int:
    # Return the smallest integer that lies in the maximum number of closed intervals.
    events: Dict[int, int] = {}
    for a, b in intervals:
        events[a] = events.get(a, 0) + 1
        events[b + 1] = events.get(b + 1, 0) - 1

    max_count = -1
    best_x = 0
    curr = 0
    for x in sorted(events.keys()):
        curr += events[x]
        if curr > max_count:
            max_count = curr
            best_x = x

    return best_x
Explanation
Use a sweep-line over integer coordinates with a difference map: for each [a,b], add +1 at a and -1 at b+1. After sorting the event positions, a running prefix sum gives the number of active intervals at each integer. Track the maximum prefix value and record the smallest position where it occurs. This corresponds to the smallest integer covered by the most intervals. For the follow-up where all endpoints are less than a small M, build an array diff of size M+2, apply the same +1/-1 updates, compute a prefix sum, and find the index with the maximum value in O(N+M) time.

Time complexity: O(N log N). Space complexity: O(N).

Hints

  1. Transform intervals into events: +1 at ai and -1 at bi+1 (difference array idea for inclusive intervals).
  2. Sort event positions and compute a running sum (prefix) to track active intervals; the first position reaching a new maximum is your answer.
  3. If all endpoints are less than a small M, use an array of length M+2 for O(N+M) time via difference array and prefix sums.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,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.

Related Coding Questions

  • Solve Two Backtracking Array Problems - Meta (hard)
  • Solve Array, Matrix, and Recommendation Problems - Meta (medium)
  • Find a String Containing Another - Meta (medium)
  • Solve Subarray Sum and Local Minimum - Meta (hard)
  • Validate abbreviations and brackets - Meta (medium)