Rebalance shard ranges under overlap limit
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates skills in interval and range management, constraint enforcement, and algorithmic problem solving related to overlapping integer ranges and minimizing data movement when adjusting shard boundaries.
Constraints
- 1 <= limit <= 10^5
- 0 <= len(shards) <= 2 * 10^5
- Each shard range is inclusive: start <= end
- Shard ids are unique strings
- start and end can be negative and fit in 32-bit signed integers
Examples
Input: (1, [('A', 0, 100), ('B', 80, 180)])
Expected Output: [('A', 0, 100), ('B', 101, 180)]
Explanation: The overlap [80..100] exceeds limit=1, so delay B to start at 100+1=101.
Input: (1, [('A', 0, 2), ('B', 5, 7)])
Expected Output: [('A', 0, 4), ('B', 5, 7)]
Explanation: No overlaps, but keys 3..4 are uncovered within [0..7]. Extend A to cover the gap.
Input: (2, [('A', 0, 4), ('B', 1, 5), ('C', 2, 6)])
Expected Output: [('A', 0, 4), ('B', 1, 5), ('C', 5, 6)]
Explanation: At key 2, starting C would create 3 overlaps (>2), so delay C until after A ends: 4+1=5.
Input: (1, [('A', 0, 0), ('B', 0, 0), ('C', 1, 1)])
Expected Output: [('A', 0, 0), ('C', 1, 1)]
Explanation: B would overlap at key 0, so it is delayed to 1, becoming empty (1>0) and removed.
Input: (1, [('X', -5, -1), ('Y', -3, 2)])
Expected Output: [('X', -5, -1), ('Y', 0, 2)]
Explanation: Overlap at -3..-1 exceeds limit=1, so delay Y to start at -1+1=0.
Input: (1, [])
Expected Output: []
Explanation: No shards to rebalance.
Input: (1, [('A', 0, 0), ('B', 0, 1), ('C', 1, 2)])
Expected Output: [('A', 0, 0), ('B', 1, 1), ('C', 2, 2)]
Explanation: B is delayed to start at 1, then C is delayed to start at 2 to avoid overlap > 1.
Input: (3, [('S', 5, 5)])
Expected Output: [('S', 5, 5)]
Explanation: Single shard already satisfies the overlap limit and covers the full span [5..5].
Hints
- Use a sweep-line approach: process shards in increasing start order, and track currently-active shards by their end values.
- If overlap would exceed `limit`, delay the shard that is trying to start now to just after the earliest-ending active shard (`min_end + 1`).