Problem
Ask
Submissions

Problem: Self Crossing

Hard
40 min
Explore how to analyze a sequence of moves on a grid to detect if the path crosses itself. Learn to apply mathematical and geometric problem-solving techniques to identify intersections efficiently and write code that returns true if any crossing occurs. This lesson strengthens your ability to solve spatial path problems commonly presented in coding interviews.

Statement

You are given an array of integers, distance, where each element represents the length of a move you will make on an X-Y plane. You start at the origin, which is point (0,0)(0, 0), and move according to the array. Specifically, you move distance[0] meters north, distance[1] meters west, distance[2] meters south, distance[3] meters east, and continue this pattern in a counterclockwise direction. Each step follows the sequence—north, west, south, east—repeating as long as there are remaining distances in the array.

Your task is to determine whether this path crosses itself at any point. This means checking whether you revisit any previously visited position (including the origin or any other point) at any step. Return TRUE if the path intersects itself, and FALSE otherwise.

Constraints:

  • 1 \leq distance.length \leq 10310^3

  • 1 \leq distance[i] \leq 10310^3

Problem
Ask
Submissions

Problem: Self Crossing

Hard
40 min
Explore how to analyze a sequence of moves on a grid to detect if the path crosses itself. Learn to apply mathematical and geometric problem-solving techniques to identify intersections efficiently and write code that returns true if any crossing occurs. This lesson strengthens your ability to solve spatial path problems commonly presented in coding interviews.

Statement

You are given an array of integers, distance, where each element represents the length of a move you will make on an X-Y plane. You start at the origin, which is point (0,0)(0, 0), and move according to the array. Specifically, you move distance[0] meters north, distance[1] meters west, distance[2] meters south, distance[3] meters east, and continue this pattern in a counterclockwise direction. Each step follows the sequence—north, west, south, east—repeating as long as there are remaining distances in the array.

Your task is to determine whether this path crosses itself at any point. This means checking whether you revisit any previously visited position (including the origin or any other point) at any step. Return TRUE if the path intersects itself, and FALSE otherwise.

Constraints:

  • 1 \leq distance.length \leq 10310^3

  • 1 \leq distance[i] \leq 10310^3