Track Downloaded Byte Ranges of a Large File with Range Queries
Last updated: November 29, 2026
Quick Overview
Implement a structure over half-open integer ranges supporting add(l, r) to mark bytes as downloaded, remove(l, r) to unmark them, and query(l, r) to check whether every byte in [l, r) is currently marked.
0
0
0 solved
Implement a structure over half-open integer ranges supporting add(l, r) to mark bytes as downloaded, remove(l, r) to unmark them, and query(l, r) to check whether every byte in [l, r) is currently marked.
Framed around resumable downloads in Chrome or Google Drive sync, this tests maintaining a sorted set of disjoint intervals under insert and delete. Interviewers look for correct merging and splitting with binary search and attention to half-open boundaries where most bugs live.
What the Interviewer Expects
- Keeps a sorted list of disjoint, non-adjacent intervals
- Uses binary search to find the affected span of intervals
- Merges overlapping and touching intervals on add
- Splits intervals partially covered by remove
- Explains complexity and when a segment tree is preferable
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the total number of bytes downloaded at any time in O(1).
- Return the first missing byte at or after a position.
- Ranges come from many concurrent download threads; how do you make it thread safe without a global lock?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Ranges are half-open [l, r) with l < r.
addunions the range in;removesubtracts it;querychecks full coverage.- Many operations interleaved; the number of stored intervals can grow large...
High-Level Design
Store disjoint intervals sorted by start in spans, plus a parallel starts list for bisect (rebuilt after each mutation for clarity; a balanced tree or sortedcontainers would avoid that). add(l, r)...