Merge Overlapping Line Ranges for Pull Request Review Comments

Last updated: November 29, 2026

Quick Overview

Given a list of inclusive line ranges (start, end) that reviewers commented on in one file of a pull request, merge ranges that overlap or touch so the diff view can draw one highlighted block per merged range. Return the merged ranges sorted by start line.

GitHub
Coding & Algorithms
Software Engineer
GitHub
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a list of inclusive line ranges (start, end) that reviewers commented on in one file of a pull request, merge ranges that overlap or touch so the diff view can draw one highlighted block per merged range. Return the merged ranges sorted by start line.

A warm-up in the style of GitHub's phone screens, framed around the pull request Files Changed view where multi-line comments are highlighted. The interviewer checks that you sort first, handle adjacency (lines 3 and 4 should join) as well as overlap, and talk through edge cases cleanly.

What the Interviewer Expects
  • Asks whether touching ranges like (1,3) and (4,6) should merge
  • Sorts by start line before a single linear pass
  • Extends the current block with max(end) rather than overwriting it
  • Handles empty input and single-line ranges
  • States O(n log n) time and O(n) output space
Key Topics to Cover
intervals
sorting
greedy merge
edge cases
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
  • Ranges arrive one at a time as reviewers comment live. How do you keep the merged set up to date?
  • How would you also report how many comments fall into each merged block?
  • What changes if a range can span a deleted hunk that no longer exists in the new file?
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

Input: list of (start, end) with start <= end, 1-indexed lines, inclusive. Output: minimal sorted list of disjoint ranges covering the same lines. Because lines are integers, ranges that are adjac...

High-Level Design

Sort by start. Walk the list keeping the last merged range. If the next range starts at or before last.end + 1, extend last.end to the larger end; otherwise open a new block. Sorting guarantees no...


Submit Your Answer
Markdown supported