The Maximum Number of Visible Points coding problem gives you a list of points (x, y coordinates) and an angle. You are also given your current location (x, y) and a fixed_direction. You can rotate your view up to angle degrees (clockwise or counter-clockwise) from your fixed_direction. You want to find the maximum number of points you can see. Points that are exactly at your location are always visible.
Nuro, Nvidia, Aurora, Amazon, Applied Intuition, Anduril, and Google use this hard problem to test a candidate's understanding of geometry (angles), sorting, and the sliding window technique. It's a classic problem that requires careful handling of angular ranges and circular arrangements.
Geometry + Sorting + Sliding Window:
location. These are always visible. Remove them from the points list for further processing.location relative to the x-axis. Use atan2(y - loc_y, x - loc_x) to get angles in (-PI, PI] or [0, 2*PI). Convert fixed_direction to radians.
atan2(dy, dx) handles all quadrants correctly.[0, 360) degrees or [0, 2*PI) radians for easier processing.[angle1, angle2, ..., angleN] becomes [angle1, ..., angleN, angle1+360, ..., angleN+360].right pointer, the left pointer should advance such that angles[right] - angles[left] <= angle.angle sweep.max_window_size + points_at_origin_count.Consider the fixed_direction: you can rotate up to angle degrees from your fixed_direction. This means your view window is [fixed_direction - angle/2, fixed_direction + angle/2] (if you can split it evenly) or a window of size angle degrees that can be centered anywhere. The problem statement "rotate your view up to angle degrees" implies you can place a view cone of width angle anywhere. This makes the sliding window approach applicable to the sorted angles.
Location = (0,0), Points = [(1,0), (1,1), (-1,0), (0,1)], Angle = 90 degrees.
[0, 45, 90, 180].[0, 45, 90, 180, 360, 405, 450, 540].Sliding Window (window size = 90 degrees):
[0, 45, 90]: 90 - 0 = 90 <= 90. Count = 3.[45, 90]: 90 - 45 = 45 <= 90. Count = 2.[90, 180]: 180 - 90 = 90 <= 90. Count = 2.[180]: Count = 1.[360, 405, 450] (same as [0, 45, 90]): 450 - 360 = 90 <= 90. Count = 3.Max visible points = 3.
atan2 is preferred over atan to correctly handle quadrants. Normalization to a single range (e.g., [0, 360)) is critical.angles[right] - angles[left] <= angle) is correctly applied.For the Array Math Sorting Sliding Window Geometry interview pattern, this problem is an excellent test of handling geometric constraints in an algorithmic context. Master angle calculations, normalization, and the sliding window technique for range-based queries on sorted data, especially for circular arrays.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Moving Stones Until Consecutive II | Medium | Solve | |
| Minimize Manhattan Distances | Hard | Solve | |
| Find the Number of Ways to Place People II | Hard | Solve | |
| Find the Number of Ways to Place People I | Medium | Solve | |
| Minimum Lines to Represent a Line Chart | Medium | Solve |