Erect the Fence

HardMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the positions of trees in a garden as trees[i] = [x, y]. A fence of minimum total length must be built so that every tree lies inside it or on it; that fence is the boundary of the convex hull of the trees. Return the coordinates of every tree that lies **on the fence**. That includes the corner trees and also any tree lying on a straight fence segment between two corners. Trees may be returned in **any order**. All positions are distinct. If all trees are on one line, every tree is on the fence.

Examples

Input: trees = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
Output: [[1,1],[2,0],[2,4],[3,3],[4,2]]
Only [2,2] is strictly inside the fence; the other five trees are on it.
Input: trees = [[1,2],[2,2],[4,2]]
Output: [[1,2],[2,2],[4,2]]
The trees are collinear, so the fence runs along the line and touches all of them.

Constraints

  • 1 <= trees.length <= 3000
  • trees[i].length == 2
  • 0 <= x, y <= 100
  • All positions are unique

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems