← Questions

Intersection of two line segments

0
Applied IntuitionCodingLast asked Feb 2026

A coding interview question asked in Applied Intuition's AI-assisted build round, last asked Feb 2026. It tests geometry, math and edge cases.

Given two line segments in the plane, AB and CD, by their endpoints, decide whether the closed segments intersect. As a follow-up, return where: the single point, or the overlapping piece when they lie on one line.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • segments_intersect(a, b, c, d) -> bool — each point is a tuple (x, y) of integers. Touching at an endpoint counts as intersecting.

Example

segments_intersect((0,0), (2,2), (0,2), (2,0))  ->  True     # cross at (1, 1)
segments_intersect((0,0), (0,2), (1,0), (1,2))  ->  False    # parallel vertical
segments_intersect((0,0), (2,0), (1,0), (3,0))  ->  True     # collinear, overlap from (1,0) to (2,0)
segments_intersect((0,0), (1,1), (1,1), (3,0))  ->  True     # touch at an endpoint
segments_intersect((0,0), (1,0), (2,0), (3,0))  ->  False    # collinear, no overlap

Constraints

  • Coordinates can be large; avoid floating-point error in the yes/no answer.
  • Handle vertical and horizontal segments and segments that are a single point.

Clarifying questions

  • Are coordinates integers or floats, and how large?
  • Does touching at an endpoint count?
  • For the follow-up, how should a collinear overlap be returned?
  • Can a segment have both endpoints equal?

What a strong answer covers

  • The orientation test (sign of a cross product) for the four point triples; the general case is when C and D lie on opposite sides of AB and A and B on opposite sides of CD.
  • Special cases when an orientation is zero: check that the point lies within the other segment's bounding box.
  • No slopes, so vertical segments need no special case; integer arithmetic keeps the answer exact.
  • For the point: solve the parametric equations, returning exact fractions or floats only at the end; for collinear overlap, the max of the starts and min of the ends along the line.
  • Tests for each case in the example plus degenerate point segments.

Follow-ups

  • Return the intersection point, or the overlap segment.
  • Given n segments, report whether any two intersect faster than O(n²) (sweep line).
  • Inputs are floats. How do you choose an epsilon, and where can it still go wrong?

Where it's asked

  • Applied Intuition · AI-assisted build · Last asked Feb 2026120 min · Build the designed system, or a small web app, in a prepared environment over two to three hours · Claude Code or a provided Claude model expected

What it tests

geometry · math · edge cases

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding