KatInterview

469. Convex Polygon

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

A polygon is convex if all its interior angles are less than or equal to 180 degrees. A key property that follows from this is that when traversing the vertices sequentially (either clockwise or counter-clockwise), all turns made at the vertices are in the same direction. For instance, in a counter-clockwise traversal of a convex polygon, every turn is a "left turn". If we find even one "right turn", the polygon is not convex (it's concave or complex). Collinear points, which form a 180-degree angle, are permissible in a convex polygon.

Solution

Convex Polygon in Python

class Solution:
    def isConvex(self, points: List[List[int]]) -> bool:
        n = len(points)
        # orientation: 0 = not set, 1 = positive (CCW), -1 = negative (CW)
        orientation = 0

        for i in range(n):
            # Get three consecutive points: p1, p2, p3.
            # p1 is points[i], p2 is points[(i+1)%n], p3 is points[(i+2)%n]
            x1, y1 = points[i]
            x2, y2 = points[(i + 1) % n]
            x3, y3 = points[(i + 2) % n]

            # Calculate the 2D cross product of vectors (p2-p1) and (p3-p2).
            # The formula is (x2-x1)*(y3-y2) - (y2-y1)*(x3-x2).
            cross_product = (x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2)

            # If cross_product is not 0, it indicates a turn.
            if cross_product != 0:
                if orientation == 0:
                    # This is the first turn we've seen, set the polygon's orientation.
                    orientation = 1 if cross_product > 0 else -1
                else:
                    # Check if the current turn's direction is different from the established orientation.
                    if (cross_product > 0 and orientation == -1) or \
                       (cross_product < 0 and orientation == 1):
                        # Found a turn in the opposite direction, so it's not convex.
                        return False
            # If cross_product is 0, points are collinear, which is fine for a convex polygon.

        # If we get through all points without finding opposite turns, the polygon is convex.
        return True

Convex Polygon in Java

class Solution {
    public boolean isConvex(List<List<Integer>> points) {
        int n = points.size();
        // orientation: 0 = not set, 1 = positive (CCW), -1 = negative (CW)
        long orientation = 0;

        for (int i = 0; i < n; i++) {
            // Get three consecutive points: p1, p2, p3.
            // p1 is points.get(i), p2 is points.get((i+1)%n), p3 is points.get((i+2)%n)
            long x1 = points.get(i).get(0);
            long y1 = points.get(i).get(1);
            long x2 = points.get((i + 1) % n).get(0);
            long y2 = points.get((i + 1) % n).get(1);
            long x3 = points.get((i + 2) % n).get(0);
            long y3 = points.get((i + 2) % n).get(1);

            // Calculate the 2D cross product of vectors (p2-p1) and (p3-p2).
            // The formula is (x2-x1)*(y3-y2) - (y2-y1)*(x3-x2).
            long crossProduct = (x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2);

            // If crossProduct is not 0, it indicates a turn.
            if (crossProduct != 0) {
                if (orientation == 0) {
                    // This is the first turn we've seen, set the polygon's orientation.
                    orientation = crossProduct > 0 ? 1 : -1;
                } else {
                    // Check if the current turn's direction is different from the established orientation.
                    if ((crossProduct > 0 && orientation == -1) ||
                        (crossProduct < 0 && orientation == 1)) {
                        // Found a turn in the opposite direction, so it's not convex.
                        return false;
                    }
                }
            }
            // If crossProduct is 0, points are collinear, which is fine for a convex polygon.
        }

        // If we get through all points without finding opposite turns, the polygon is convex.
        return true;
    }
}

Convex Polygon in C++

class Solution {
public:
    bool isConvex(vector<vector<int>>& points) {
        long long n = points.size();
        // orientation: 0 = not set, 1 = positive (CCW), -1 = negative (CW)
        long long orientation = 0;

        for (int i = 0; i < n; ++i) {
            // Get three consecutive points: p1, p2, p3.
            // p1 is points[i], p2 is points[(i+1)%n], p3 is points[(i+2)%n]
            long long x1 = points[i][0];
            long long y1 = points[i][1];
            long long x2 = points[(i + 1) % n][0];
            long long y2 = points[(i + 1) % n][1];
            long long x3 = points[(i + 2) % n][0];
            long long y3 = points[(i + 2) % n][1];

            // Calculate the 2D cross product of vectors (p2-p1) and (p3-p2).
            // The formula is (x2-x1)*(y3-y2) - (y2-y1)*(x3-x2).
            long long cross_product = (x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2);

            // If cross_product is not 0, it indicates a turn.
            if (cross_product != 0) {
                if (orientation == 0) {
                    // This is the first turn we've seen, set the polygon's orientation.
                    orientation = cross_product > 0 ? 1 : -1;
                } else {
                    // Check if the current turn's direction is different from the established orientation.
                    if ((cross_product > 0 && orientation == -1) || 
                        (cross_product < 0 && orientation == 1)) {
                        // Found a turn in the opposite direction, so it's not convex.
                        return false;
                    }
                }
            }
            // If cross_product is 0, points are collinear, which is fine for a convex polygon.
        }
        
        // If we get through all points without finding opposite turns, the polygon is convex.
        return true;
    }
};

Convex Polygon in TypeScript

function isConvex(points: number[][]): boolean {
    const n = points.length;
    // orientation: 0 = not set, 1 = positive (CCW), -1 = negative (CW)
    let orientation: number = 0;

    for (let i = 0; i < n; i++) {
        // Get three consecutive points: p1, p2, p3.
        // p1 is points[i], p2 is points[(i+1)%n], p3 is points[(i+2)%n]
        const x1 = points[i][0];
        const y1 = points[i][1];
        const x2 = points[(i + 1) % n][0];
        const y2 = points[(i + 1) % n][1];
        const x3 = points[(i + 2) % n][0];
        const y3 = points[(i + 2) % n][1];

        // Calculate the 2D cross product of vectors (p2-p1) and (p3-p2).
        // The formula is (x2-x1)*(y3-y2) - (y2-y1)*(x3-x2).
        const crossProduct = (x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2);

        // If crossProduct is not 0, it indicates a turn.
        if (crossProduct !== 0) {
            if (orientation === 0) {
                // This is the first turn we've seen, set the polygon's orientation.
                orientation = crossProduct > 0 ? 1 : -1;
            } else {
                // Check if the current turn's direction is different from the established orientation.
                if ((crossProduct > 0 && orientation === -1) ||
                    (crossProduct < 0 && orientation === 1)) {
                    // Found a turn in the opposite direction, so it's not convex.
                    return false;
                }
            }
        }
        // If crossProduct is 0, points are collinear, which is fine for a convex polygon.
    }

    // If we get through all points without finding opposite turns, the polygon is convex.
    return true;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.