Merge Collinear Point Segments
Updated
Problem statement
Each input segment is a list of at least two different integer points that lie on one straight line. The segment covers the part of the line between its two farthest points, including both of them.
Merge segments that lie on the same infinite line and whose covered parts overlap or touch. Keep merging until no more segments can join. Each group of joined segments (a single segment counts as a group) becomes one output entry. The entry lists the group's distinct points, without repeats, in order along one fixed direction of the line.
Order the output like this. Lines come in the order they first appear in the input. When one line has several separate groups, list them by increasing position along that line.
This works the same for every slope, including horizontal and vertical lines.
Function
mergeCollinearSegments(segments: int[][][]) → int[][][]Examples
Example 1
segments = [[[1,1],[2,2],[4,4]],[[2,1],[4,2]],[[3,3],[6,6]],[[7,7],[8,8]]]return = [[[1,1],[2,2],[3,3],[4,4],[6,6]],[[7,7],[8,8]],[[2,1],[4,2]]]The first and third segments both lie on the line y=x and overlap, so they merge into one group of five distinct points. The fourth segment is also on y=x but does not touch that group, so it stays a separate group and comes right after it. The second segment lies on a different line, with slope one half, so it stays alone and comes last.
Example 2
segments = [[[-2,3],[0,3],[2,3]],[[2,3],[5,3]],[[1,-1],[1,2]]]return = [[[-2,3],[0,3],[2,3],[5,3]],[[1,-1],[1,2]]]The first two segments lie on the same horizontal line and share an endpoint, so they touch and merge into one group. The third segment is vertical, on a different line, so it stays alone and comes second.
Constraints
1 <= segments.length <= 5000.- Each segment contains at least two distinct points and all of its points are collinear.
- The total number of listed points is at most
20000. - Coordinates are integers in
[-10^6,10^6].
More Applied Intuition interview questions
- KV Store with Nested TransactionsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Parse Nested Message TypesPHONE SCREEN · Seen Nov 2026
- Resolve Variable Equations with Dependency ErrorsPHONE SCREEN · Seen Nov 2026
- Locate Vehicles with Square QueriesPHONE SCREEN · Seen Nov 2026
- File-Backed Dense Game of LifePHONE SCREEN · Seen Oct 2026
- Compilation Order with Topological SortPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Merge Overlapping Closed IntervalsPHONE SCREEN · Seen Sep 2026
- Ring Buffer OperationsPHONE SCREEN · Seen Aug 2026
- Out-of-Order TLV File ReassemblyPHONE SCREEN · Seen Aug 2026
- Hash Map with Separate ChainingONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Horizontal Line Bisecting Total Square AreaGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Lab and Consultation Appointment MatchingAssort Health · PHONE SCREENAssort Health interview questions
- Merchant Capacity Interval MergeDoorDash · PHONE SCREENDoorDash interview questions
- Free Time for Every Meeting RoomScale AI · PHONE SCREENScale AI interview questions
- Worker Management with Promotions and Double-Paid TimeInstacart · ONSITE INTERVIEWInstacart interview questions
- Worker Management (Promotion Track), Part 4: Full-Shift Double PayAirbnb · OAAirbnb interview questions
- Worker Management, Part 4: Double-Paid IntervalsAnthropic · OAAnthropic interview questions
- Peak Concurrent Calls by UTC DayHubSpot · OA
- Unique Active Pins by Time SegmentPinterest · ONSITE INTERVIEWPinterest interview questions