Compute point-to-segment minimum distance
Company: LinkedIn
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
## Problem
Given a 2D point **P(x, y)** and a line segment with endpoints **A(x1, y1)** and **B(x2, y2)**, compute the **minimum Euclidean distance** from point **P** to the segment **AB**.
### Input
- Real numbers (or integers) representing coordinates of **P**, **A**, and **B**.
### Output
- A single floating-point number: the minimum distance from **P** to segment **AB**.
### Notes / Edge cases
- If the perpendicular projection of **P** onto the infinite line **AB** falls **within** the segment, the answer is the perpendicular distance.
- Otherwise, the answer is `min(dist(P, A), dist(P, B))`.
- Handle degenerate segments where **A == B** (distance is `dist(P, A)`).
### Constraints (assume)
- Coordinates are within a reasonable range (e.g., `[-1e9, 1e9]`).
- Use double precision.
Quick Answer: This question evaluates understanding of computational geometry and numerical robustness, testing the ability to compute Euclidean distances between a point and a line segment using analytic geometry concepts.