Dataford
Interview Guides
Upgrade
All questions/Coding/Convex Hull Including Boundary Points

Convex Hull Including Boundary Points

Medium
Coding
ArraysGraphsSortingMath

Problem

Problem

At DoorDash, you are given GPS points for a delivery region. Implement a function that returns the convex hull boundary of these points, including all points that lie on the boundary edges (not just the corner vertices).

Formal Specification

  • Input: points: list[list[int]] where each point is [x, y] (may contain duplicates).
  • Output: list[list[int]] of unique boundary points in counterclockwise (CCW) order, starting from the leftmost-lowest point.

A point must be included if it lies on the perimeter of the convex hull (including collinear points along edges). Points strictly inside the hull must be excluded.

Examples

Example 1

  • Input: points = [[0,0],[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
  • Output: [[0,0],[2,0],[4,2],[2,4]]
  • Explanation: The hull corners are (0,0),(2,0),(4,2),(2,4); (1,1),(2,2),(3,3) are inside.

Example 2

  • Input: points = [[0,0],[1,0],[2,0],[3,0]]
  • Output: [[0,0],[1,0],[2,0],[3,0]]
  • Explanation: All points are collinear and lie on the hull boundary, so all are returned.

Constraints

  • 1 <= len(points) <= 10^4
  • -10^9 <= x, y <= 10^9
  • points may contain duplicates
  • Return unique boundary points in CCW order starting from the leftmost-lowest point

Examples

Example 1
Inputpoints = [[0,0],[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]Output[[0,0],[2,0],[4,2],[2,4]]WhyOnly the outer quadrilateral corners are on the hull; the other points are strictly inside.
Example 2
Inputpoints = [[0,0],[2,0],[2,2],[0,2],[1,0],[2,1],[1,2],[0,1]]Output[[0,0],[1,0],[2,0],[2,1],[2,2],[1,2],[0,2],[0,1]]WhyAll mid-edge points lie on the rectangle boundary and must be included in CCW order.

Constraints

  • 1 <= len(points) <= 10^4
  • -10^9 <= x, y <= 10^9
  • points may contain duplicates
  • Return unique boundary points in counterclockwise order starting from the leftmost-lowest point

Function Signature

def convex_hull(points: list[list[int]]) -> list[list[int]]:

Problem

Problem

At DoorDash, you are given GPS points for a delivery region. Implement a function that returns the convex hull boundary of these points, including all points that lie on the boundary edges (not just the corner vertices).

Formal Specification

  • Input: points: list[list[int]] where each point is [x, y] (may contain duplicates).
  • Output: list[list[int]] of unique boundary points in counterclockwise (CCW) order, starting from the leftmost-lowest point.

A point must be included if it lies on the perimeter of the convex hull (including collinear points along edges). Points strictly inside the hull must be excluded.

Examples

Example 1

  • Input: points = [[0,0],[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
  • Output: [[0,0],[2,0],[4,2],[2,4]]
  • Explanation: The hull corners are (0,0),(2,0),(4,2),(2,4); (1,1),(2,2),(3,3) are inside.

Example 2

  • Input: points = [[0,0],[1,0],[2,0],[3,0]]
  • Output: [[0,0],[1,0],[2,0],[3,0]]
  • Explanation: All points are collinear and lie on the hull boundary, so all are returned.

Constraints

  • 1 <= len(points) <= 10^4
  • -10^9 <= x, y <= 10^9
  • points may contain duplicates
  • Return unique boundary points in CCW order starting from the leftmost-lowest point

Examples

Example 1
Inputpoints = [[0,0],[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]Output[[0,0],[2,0],[4,2],[2,4]]WhyOnly the outer quadrilateral corners are on the hull; the other points are strictly inside.
Example 2
Inputpoints = [[0,0],[2,0],[2,2],[0,2],[1,0],[2,1],[1,2],[0,1]]Output[[0,0],[1,0],[2,0],[2,1],[2,2],[1,2],[0,2],[0,1]]WhyAll mid-edge points lie on the rectangle boundary and must be included in CCW order.

Constraints

  • 1 <= len(points) <= 10^4
  • -10^9 <= x, y <= 10^9
  • points may contain duplicates
  • Return unique boundary points in counterclockwise order starting from the leftmost-lowest point

Function Signature

def convex_hull(points: list[list[int]]) -> list[list[int]]:
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.