AO
Back

Merge Overlapping Collinear 2D Line Segments

Also reported as Merge 2D Line Segments · Merge Segments

CodingOnsiteLast reported October 2026Medium Frequency

Problem Overview

The round: reported six times, at phone screens and onsite coding rounds alike. You get 2D line segments, each a list of two or more collinear points, and merge the segments that lie on the same line and overlap, keeping every point. Candidates describe it as merge intervals plus geometry, with the time going to the corner cases: vertical lines, horizontal lines, and points that are not listed from start to end. One 45-minute screen had no follow-up. One graded part.

Part 1 — merge overlapping collinear segments

def merge(segments: list[list[tuple[float, float]]], merge_touching: bool = True) -> list[list[tuple[float, float]]]:
  • Each segment is a list of two or more points (x, y), all on one line. Two segments merge when they lie on the same line and overlap; a chain of overlaps merges into one segment.
  • A merged segment holds every point of its inputs once, sorted along the line (by x, or by y for a vertical line).
  • A segment that merges with nothing is returned exactly as given, point order included.
  • Output order here: lines in the order they first appear in the input, and on each line the segments from one end to the other.
  • merge_touching: whether two segments that only share an endpoint merge. The reports do not say; here they do by default.
  • Fewer than two distinct points, or points that are not collinear, raise ValueError.

The reported example (one report adds that the points inside a segment are sorted but the segments are not):

Input:  [[(1, 1), (2, 2), (4, 4)], [(2, 1), (4, 2)], [(3, 3), (6, 6)], [(7, 7), (8, 8)]]
Output: [[(1, 1), (2, 2), (3, 3), (4, 4), (6, 6)], [(7, 7), (8, 8)], [(2, 1), (4, 2)]]

Another report's version lists a segment's points backwards ([(4, 2), (2, 1)]), so do not assume the first point is the start.

Notes: one report gives the signature in C++ as merge(vector<vector<pair<double, double>>>); this Python port is ours. The touching rule, the output order, keeping unmerged segments as given, the sort direction of merged segments and the ValueError contract are ours. Coordinates are compared exactly (as fractions), so 0.1 + 0.2 style rounding in the input is not forgiven; ask whether a tolerance is wanted.

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Group by (slope, intercept), then merge intervalswhat candidates describe
Cross and dot products: check parallel, check distance to the line, project endpointsone reply's planning-team approach
Direction vector plus a parameter t along the line instead of x or y—

What Reports Emphasize

Common mistakes: Infinite slope for vertical lines; Merging vertical segments by x, which they all share; Assuming the first point is the start; Losing the round to slope and intercept arithmetic before reaching the merge; Not handling slope 0.

Edge cases to test: Vertical lines at the same and at different x; Horizontal lines; Parallel lines with the same slope; A segment listed backwards; Segments that only touch; A chain of overlaps; A segment inside another

Practice

Write your own against 9 test cases, or read the worked solution — approach, complexity, and code that runs.
Did this match your interview?

If you have had this round, tell us how close this page was. It is how we keep the pages accurate.

Prepping for Applied Intuition with friends?

Send them this page. It is free to read, no account needed.

More Applied Intuition Questions

Free preview

Every question in the Applied Intuition catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 22 Applied Intuition questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Applied Intuition · Coding · Reported 6× across candidate reports
Is this helpful?