KatInterview

335. Self Crossing

HardSolutions 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

We are asked to determine if a path, generated by a sequence of movements on a 2D plane, crosses itself. The path starts at (0,0) and moves North, then West, then South, then East, and so on, with lengths specified by the distance array. The directions rotate counter-clockwise.

Solution

Self Crossing in Python

class Solution:
    def isSelfCrossing(self, distance: List[int]) -> bool:
        n = len(distance)
        if n <= 3:
            return False
        
        d = distance

        for i in range(3, n):
            # Case 1: 4th line crosses 1st line.
            # The path turns inward and crosses.
            if d[i] >= d[i-2] and d[i-1] <= d[i-3]:
                return True
            
            # Case 2: 5th line meets 1st line.
            # This special case happens at i=4.
            if i == 4 and d[3] == d[1] and d[4] + d[0] >= d[2]:
                return True
            
            # Case 3: 6th line crosses 1st line.
            # The path forms a rectangle and the new line crosses into it.
            if i >= 5:
                if (d[i-2] > d[i-4] and
                    d[i-3] > d[i-1] and
                    d[i-1] + d[i-5] >= d[i-3] and
                    d[i] + d[i-4] >= d[i-2]):
                    return True
        
        return False

Self Crossing in Java

class Solution {
    public boolean isSelfCrossing(int[] distance) {
        int n = distance.length;
        if (n <= 3) {
            return false;
        }

        for (int i = 3; i < n; i++) {
            // Case 1: 4th line crosses 1st line.
            // The path turns inward and crosses.
            if (distance[i] >= distance[i - 2] && distance[i - 1] <= distance[i - 3]) {
                return true;
            }

            // Case 2: 5th line meets 1st line.
            // This special case happens at i=4.
            if (i == 4 && distance[3] == distance[1] && distance[4] + distance[0] >= distance[2]) {
                return true;
            }

            // Case 3: 6th line crosses 1st line.
            // The path forms a rectangle and the new line crosses into it.
            if (i >= 5) {
                if (distance[i - 2] > distance[i - 4] &&
                    distance[i - 3] > distance[i - 1] &&
                    distance[i - 1] + distance[i - 5] >= distance[i - 3] &&
                    distance[i] + distance[i - 4] >= distance[i - 2]) {
                    return true;
                }
            }
        }

        return false;
    }
}

Self Crossing in C++

class Solution {
public:
    bool isSelfCrossing(vector<int>& distance) {
        int n = distance.size();
        if (n <= 3) {
            return false;
        }

        for (int i = 3; i < n; ++i) {
            // Case 1: 4th line crosses 1st line.
            // The path turns inward and crosses.
            if (distance[i] >= distance[i - 2] && distance[i - 1] <= distance[i - 3]) {
                return true;
            }

            // Case 2: 5th line meets 1st line.
            // This special case happens at i=4.
            if (i == 4 && distance[3] == distance[1] && distance[4] + distance[0] >= distance[2]) {
                return true;
            }

            // Case 3: 6th line crosses 1st line.
            // The path forms a rectangle and the new line crosses into it.
            if (i >= 5) {
                if (distance[i - 2] > distance[i - 4] && 
                    distance[i - 3] > distance[i - 1] &&
                    distance[i - 1] + distance[i - 5] >= distance[i - 3] &&
                    distance[i] + distance[i - 4] >= distance[i - 2]) {
                    return true;
                }
            }
        }

        return false;
    }
};

Self Crossing in TypeScript

function isSelfCrossing(distance: number[]): boolean {
    const n = distance.length;
    if (n <= 3) {
        return false;
    }

    const d = distance;

    for (let i = 3; i < n; i++) {
        // Case 1: 4th line crosses 1st line.
        // The path turns inward and crosses.
        if (d[i] >= d[i - 2] && d[i - 1] <= d[i - 3]) {
            return true;
        }

        // Case 2: 5th line meets 1st line.
        // This special case happens at i=4.
        if (i === 4 && d[3] === d[1] && d[4] + d[0] >= d[2]) {
            return true;
        }

        // Case 3: 6th line crosses 1st line.
        // The path forms a rectangle and the new line crosses into it.
        if (i >= 5) {
            if (d[i - 2] > d[i - 4] &&
                d[i - 3] > d[i - 1] &&
                d[i - 1] + d[i - 5] >= d[i - 3] &&
                d[i] + d[i - 4] >= d[i - 2]) {
                return true;
            }
        }
    }

    return false;
}

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.