Summarize a Stream of Online User IDs as Disjoint Ranges

Last updated: November 29, 2026

Quick Overview

User IDs that come online arrive one at a time as non-negative integers. Support add(id) and ranges(), where ranges() returns the IDs seen so far as a sorted list of disjoint [lo, hi] inclusive ranges.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


User IDs that come online arrive one at a time as non-negative integers. Support add(id) and ranges(), where ranges() returns the IDs seen so far as a sorted list of disjoint [lo, hi] inclusive ranges.

A data-stream interval question in the style of Meta's onsite, motivated by compactly representing which shards or ID blocks have reported in during a fleet-wide rollout. The interviewer checks that you merge with both neighbors correctly in O(log n) and think about which operation should be cheap when adds vastly outnumber reads.

What the Interviewer Expects
  • Defines the merge cases: new island, extend left, extend right, bridge two ranges
  • Uses a sorted structure keyed by range start
  • Ignores duplicates and IDs already inside a range
  • Discusses cost of add versus ranges and which to optimize
  • Tests bridging two ranges with one ID
Key Topics to Cover
Ordered map
Binary search
Interval merging
Data streams
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if ranges() is called far more often than add()?
  • How would you support remove(id)?
  • If IDs are dense, would a bitmap be better, and how would you compress it?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Operations: add(v) with v >= 0 and ranges() returning sorted inclusive [lo, hi] pairs. Duplicates are allowed in the stream. Assume many adds, so add should be O(log n) to find the spot. Number ...

High-Level Design

Store range starts in a sorted list and a dict start -> end. For v, binary search for the last range starting at or before v. If it already covers v, stop. Check whether v extends that left range ...


Submit Your Answer
Markdown supported