PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • easy
  • LinkedIn
  • Coding & Algorithms
  • Machine Learning Engineer

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.

Return the Euclidean distance from point P to segment AB, rounded to 6 decimal places.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ((1,1), (0,0), (2,0))

Expected Output: 1.0

Explanation: Projection falls on segment.

Input: ((3,0), (0,0), (2,0))

Expected Output: 1.0

Explanation: Closest to endpoint.

Input: ((1,1), (0,0), (0,0))

Expected Output: 1.414214

Explanation: Degenerate segment.

Hints

  1. Choose a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 2026

Loading coding console...

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
  • AI Coding 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.

Related Coding Questions

  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Design O(1) Randomized Multiset - LinkedIn (easy)
  • Process Mutable Matrix Sum Queries - LinkedIn (medium)
  • Design a Randomized Multiset - LinkedIn (medium)
  • Can You Place N Objects? - LinkedIn (medium)