def merge(segments, merge_touching=True):
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]]]:
(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.x, or by y for a vertical line).merge_touching: whether two segments that only share an endpoint merge. The reports do not say; here they do by default.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.
Reported at 45-minute phone screens and at onsite coding rounds. One screen left about 35 minutes of coding after introductions and questions. Another candidate, who passed the screen, wrote the merge-interval version in about 40 minutes and got no follow-up. Another spent a long time discussing with the interviewer how to compute the slope and did not finish. An onsite candidate could not remember how to compute slope and intercept, got a reminder from the interviewer, and felt the round went fine. One candidate failed and listed vertical lines and point order as the corner cases. A reply suggests it is asked by planning and controls teams and solves it with cross and dot products: a parallel check, a distance-to-line check, then projecting endpoints onto the other segment.
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
Alternative approaches: Group by (slope, intercept), then merge intervals (what candidates describe); Cross and dot products: check parallel, check distance to the line, project endpoints (one reply's planning-team approach); Direction vector plus a parameter t along the line instead of x or y
Also reported as Merge 2D Line Segments · Merge Segments
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]]]:
(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.x, or by y for a vertical line).merge_touching: whether two segments that only share an endpoint merge. The reports do not say; here they do by default.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 | Notes |
|---|---|
| Group by (slope, intercept), then merge intervals | what candidates describe |
| Cross and dot products: check parallel, check distance to the line, project endpoints | one reply's planning-team approach |
| Direction vector plus a parameter t along the line instead of x or y | — |
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
If you have had this round, tell us how close this page was. It is how we keep the pages accurate.
Send them this page. It is free to read, no account needed.
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.