Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array points where points[i] = [xi, yi] is the coordinates of the ith point on a 2D plane. Multiple points can have the same coordinates.
You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at (xj, yj) with a radius of rj.
For each query queries[j], compute the number of points inside the jth circle. Points on the border of the circle are considered inside.
Return an array answer, where answer[j] is the answer to the jth query.
Example 1:
Input: points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]] Output: [3,2,2] Explanation: The points and circles are shown above. queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.
Example 2:
Input: points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]] Output: [2,3,2,4] Explanation: The points and circles are shown above. queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.
Constraints:
1 <= points.length <= 500points[i].length == 20 <= xi, yi <= 5001 <= queries.length <= 500queries[j].length == 30 <= xj, yj <= 5001 <= rj <= 500Follow up: Could you find the answer for each query in better complexity than O(n)?
Problem summary: You are given an array points where points[i] = [xi, yi] is the coordinates of the ith point on a 2D plane. Multiple points can have the same coordinates. You are also given an array queries where queries[j] = [xj, yj, rj] describes a circle centered at (xj, yj) with a radius of rj. For each query queries[j], compute the number of points inside the jth circle. Points on the border of the circle are considered inside. Return an array answer, where answer[j] is the answer to the jth query.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[1,3],[3,3],[5,3],[2,2]] [[2,3,1],[4,3,1],[1,1,2]]
[[1,1],[2,2],[3,3],[4,4],[5,5]] [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
count-lattice-points-inside-a-circle)count-number-of-rectangles-containing-each-point)check-if-the-rectangle-corner-is-reachable)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1828: Queries on Number of Points Inside a Circle
class Solution {
public int[] countPoints(int[][] points, int[][] queries) {
int m = queries.length;
int[] ans = new int[m];
for (int k = 0; k < m; ++k) {
int x = queries[k][0], y = queries[k][1], r = queries[k][2];
for (var p : points) {
int i = p[0], j = p[1];
int dx = i - x, dy = j - y;
if (dx * dx + dy * dy <= r * r) {
++ans[k];
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1828: Queries on Number of Points Inside a Circle
func countPoints(points [][]int, queries [][]int) (ans []int) {
for _, q := range queries {
x, y, r := q[0], q[1], q[2]
cnt := 0
for _, p := range points {
i, j := p[0], p[1]
dx, dy := i-x, j-y
if dx*dx+dy*dy <= r*r {
cnt++
}
}
ans = append(ans, cnt)
}
return
}
# Accepted solution for LeetCode #1828: Queries on Number of Points Inside a Circle
class Solution:
def countPoints(
self, points: List[List[int]], queries: List[List[int]]
) -> List[int]:
ans = []
for x, y, r in queries:
cnt = 0
for i, j in points:
dx, dy = i - x, j - y
cnt += dx * dx + dy * dy <= r * r
ans.append(cnt)
return ans
// Accepted solution for LeetCode #1828: Queries on Number of Points Inside a Circle
impl Solution {
pub fn count_points(points: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {
queries
.iter()
.map(|v| {
let cx = v[0];
let cy = v[1];
let r = v[2].pow(2);
let mut count = 0;
for p in points.iter() {
if (p[0] - cx).pow(2) + (p[1] - cy).pow(2) <= r {
count += 1;
}
}
count
})
.collect()
}
}
// Accepted solution for LeetCode #1828: Queries on Number of Points Inside a Circle
function countPoints(points: number[][], queries: number[][]): number[] {
return queries.map(([cx, cy, r]) => {
let res = 0;
for (const [px, py] of points) {
if (Math.sqrt((cx - px) ** 2 + (cy - py) ** 2) <= r) {
res++;
}
}
return res;
});
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.