The Erect the Fence interview question is a classic geometry problem known as finding the Convex Hull. You are given an array of points representing the locations of trees in a garden. You need to find the coordinates of the trees that should be used as fence posts to enclose all the trees with the minimum amount of fence. The fence must be a convex polygon.
Companies like Google and Amazon ask the Erect the Fence coding problem to test a candidate's knowledge of geometry interview pattern and computational geometry. It evaluates your ability to work with vectors, cross products, and sorting in 2D space. It’s a challenging problem that requires a systematic approach to handle collinear points and winding orders.
The most common algorithms for this are Monotone Chain or Graham Scan.
Suppose you have points: (1,1), (2,2), (2,0), (2,4), (3,3), (4,2).
(2,0), (4,2), (2,4), (1,1).Memorize the 2D cross product formula: (x2-x1)*(y3-y2) - (y2-y1)*(x3-x2). A positive result means a left turn, negative means a right turn, and zero means the points are collinear.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Erect the Fence II | Hard | Solve | |
| Maximum Number of Darts Inside of a Circular Dartboard | Hard | Solve | |
| Self Crossing | Hard | Solve | |
| Convex Polygon | Medium | Solve | |
| Queries on Number of Points Inside a Circle | Medium | Solve |