PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Applied

Merge overlapping 2D line segments

Last updated: Mar 29, 2026

Quick Overview

This question evaluates computational geometry and algorithmic skills, specifically collinearity detection, geometric segment intersection, merging of overlapping segments, sorting of points along a line, and deduplication of vertices.

  • medium
  • Applied
  • Coding & Algorithms
  • Software Engineer

Merge overlapping 2D line segments

Company: Applied

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a list of 2D line segments. Each segment is represented as a **list of 2D points** `[(x1,y1), (x2,y2), ...]` that all lie on the same straight line. ### Definitions - Within a segment, the points are already **sorted along the segment** (from one end to the other). - Different segments are **not** sorted relative to each other. - Two segments should be **merged** if: 1. They are **collinear** (lie on the same infinite line; i.e., same slope and offset), and 2. Their **geometric segments intersect** (they share at least one point in common, including touching at endpoints). When merging multiple segments, the result for that merged group should contain **all distinct points that appear in any of the original segments in the group**, sorted along the line. ### Task Return a list of merged segments (each as a sorted list of points). The order of merged segments in the output does not matter. ### Example **Input** ``` [ [(1, 1), (2, 2), (4, 4)], [(2, 1), (4, 2)], [(3, 3), (6, 6)], [(7, 7), (8, 8)] ] ``` **Output (one valid ordering)** ``` [ [(1, 1), (2, 2), (3, 3), (4, 4), (6, 6)], [(7, 7), (8, 8)], [(2, 1), (4, 2)] ] ``` ### Notes / Assumptions - Coordinates are integers. - Each input segment contains at least 2 points. - You should remove duplicate points within a merged segment.

Quick Answer: This question evaluates computational geometry and algorithmic skills, specifically collinearity detection, geometric segment intersection, merging of overlapping segments, sorting of points along a line, and deduplication of vertices.

Related Interview Questions

  • Merge Overlapping Collinear Segments - Applied (hard)
  • Implement a Fixed-Capacity Deque - Applied (medium)
  • Implement Nested Transactional Key-Value Store - Applied (hard)
  • Find intersection of two line segments - Applied (easy)
  • Find first and last occurrence in sorted array - Applied (medium)
Applied logo
Applied
Mar 1, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

You are given a list of 2D line segments. Each segment is represented as a list of 2D points [(x1,y1), (x2,y2), ...] that all lie on the same straight line.

Definitions

  • Within a segment, the points are already sorted along the segment (from one end to the other).
  • Different segments are not sorted relative to each other.
  • Two segments should be merged if:
    1. They are collinear (lie on the same infinite line; i.e., same slope and offset), and
    2. Their geometric segments intersect (they share at least one point in common, including touching at endpoints).

When merging multiple segments, the result for that merged group should contain all distinct points that appear in any of the original segments in the group, sorted along the line.

Task

Return a list of merged segments (each as a sorted list of points). The order of merged segments in the output does not matter.

Example

Input

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

Output (one valid ordering)

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

Notes / Assumptions

  • Coordinates are integers.
  • Each input segment contains at least 2 points.
  • You should remove duplicate points within a merged segment.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Applied•More Software Engineer•Applied Software Engineer•Applied Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.