Skip to main content

Convex Polygon - Solution & Explanation

MediumPremiumFree on FleetCodeArrayMathGeometry5 min readAsked at: Google
Practice this problem

Problem Statement

You are given an array of points on the X-Y plane points where points[i] = [xi, yi]. The points form a polygon when joined sequentially.

Return true if this polygon is convex and false otherwise.

You may assume the polygon formed by given points is always a simple polygon. In other words, we ensure that exactly two edges intersect at each vertex and that edges otherwise don't intersect each other.

 

Example 1:

Input: points = [[0,0],[0,5],[5,5],[5,0]]
Output: true

Example 2:

Input: points = [[0,0],[0,10],[10,10],[10,0],[5,5]]
Output: false

 

Constraints:

  • 3 <= points.length <= 104
  • points[i].length == 2
  • -104 <= xi, yi <= 104
  • All the given points are unique.

Approach Overview

Problem Overview: You are given points of a polygon in order. The task is to determine whether the polygon is convex. A polygon is convex if every internal angle is less than 180°, which means the direction of turning between consecutive edges never changes sign.

Approach 1: Brute Force Orientation Check (O(n^3) time, O(1) space)

A direct but inefficient approach examines every combination of three vertices and checks the orientation using the cross product. For three points A, B, C, compute the cross product of vectors AB and BC. The sign tells whether the turn is clockwise or counterclockwise. If the polygon is convex, all triples formed from adjacent edges should maintain a consistent turning direction. The brute force variant redundantly recomputes orientations across many triples, leading to O(n^3) checks. It mainly helps build intuition about orientation and the geometric definition of convexity.

This approach relies heavily on basic math and geometry operations. However, repeated comparisons across many triplets make it impractical for larger inputs.

Approach 2: Cross Product Sign Consistency (O(n) time, O(1) space)

The optimal solution walks through the polygon once and checks the orientation of each consecutive triplet of vertices. For points A = points[i], B = points[(i+1) % n], and C = points[(i+2) % n], compute the cross product: (Bx - Ax) * (Cy - By) - (By - Ay) * (Cx - Bx). The sign indicates the direction of the turn. A convex polygon must turn either always clockwise or always counterclockwise as you traverse the edges.

During iteration, track the first non‑zero cross product sign. For each subsequent triplet, compute the cross product again and compare the sign. If any cross product has the opposite sign, the polygon has a reflex angle and is therefore not convex. Collinear points produce a cross product of zero and can simply be skipped. The modulo operation ensures the last vertices wrap around to the beginning of the polygon.

This single pass approach uses constant memory and performs only simple arithmetic operations on coordinates stored in an array. The key insight is that convexity depends only on consistent orientation of consecutive edges, not on global comparisons.

Recommended for interviews: The O(n) cross product sign consistency approach is the expected solution. Interviewers want to see that you understand orientation tests and can apply them while iterating around the polygon once. Mentioning the brute force orientation idea demonstrates geometric reasoning, but implementing the single-pass check shows you know how to translate that idea into an optimal algorithm.

Solution

Code

Python

Java

C++

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Orientation ChecksO(n^3)O(1)Useful for understanding geometric orientation and validating convexity conceptually
Cross Product Sign Consistency (Single Pass)O(n)O(1)Best general solution for checking convex polygons efficiently

Video Solution

469. Convex Polygon (Leetcode Medium) • Programming Live with Larry • 271 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Convex Polygon easy or hard?
Convex Polygon is generally classified as a medium difficulty problem. The implementation is short, but it requires understanding geometric orientation and cross products. Candidates unfamiliar with computational geometry may find the reasoning step challenging.
How to solve Convex Polygon in O(n)?
Traverse the polygon and compute the cross product for every consecutive triple of vertices. Record the first non-zero cross product sign. Continue scanning and ensure every subsequent non-zero cross product has the same sign. If a sign flip occurs, the polygon is not convex.
What is the best approach for Convex Polygon?
The best approach is checking cross product sign consistency while iterating through consecutive triplets of vertices. Compute the orientation for each pair of adjacent edges and ensure all non-zero cross products have the same sign. This confirms the polygon always turns in the same direction, which guarantees convexity. The algorithm runs in O(n) time and O(1) space.
What data structure is used in Convex Polygon?
The input is typically stored in an array of coordinate pairs representing polygon vertices. The algorithm iterates through this array while performing geometric cross product calculations. No additional data structures are required beyond simple variables.
What is the time complexity of Convex Polygon?
The optimal solution runs in O(n) time because each vertex is processed once while evaluating cross products of consecutive edges. Space complexity is O(1) since only a few variables are used to track orientation signs. Brute force orientation checks can take up to O(n^3) time.
Convex Polygon Python or Java solution approach?
Both Python and Java implementations follow the same logic: iterate through points, compute cross products for three consecutive vertices, track the orientation sign, and ensure it never changes. The implementation uses basic arithmetic operations and a loop over the points array.
Is Convex Polygon asked at Google, Amazon, or Meta?
Convex polygon checks appear in interviews that test computational geometry fundamentals. Similar orientation and cross-product problems have been reported in interviews at companies like Google, Amazon, and Meta, especially for roles involving algorithmic problem solving.

Ready to solve this problem?

Practice Convex Polygon with our built-in code editor and test cases.

Practice on FleetCode