I started with the slope-intercept approach and immediately ran into the vertical segment wall.
Start by clarifying the problem and edge cases, then propose a robust orientation-based method using cross products to handle all cases including collinear overlaps. Structure your answer by first explaining the algorithm, then walking through edge cases, and finally discussing how to return intersection points or overlap segments.
Pro tip: Mention that you would use exact arithmetic or epsilon comparisons to avoid floating-point errors, and that you would write unit tests for all edge cases before coding.
Ask about input format, expected output for different intersection types, and whether segments are inclusive of endpoints. List edge cases: vertical, horizontal, collinear, touching endpoints, overlapping.
Use orientation tests (cross products) to determine if the segments straddle each other. For collinear cases, check bounding box overlap to detect overlap or touching.
If proper intersection, compute the intersection point using line equations. If collinear overlap, compute the overlapping segment endpoints by projecting onto the dominant axis.
Use exact arithmetic if possible, or epsilon-based comparisons. Write clear code with helper functions for orientation and point-on-segment checks.
Walk through test cases: non-intersecting, proper intersection, endpoint touch, collinear disjoint, collinear overlap, vertical segments. Verify outputs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.