Magicsheet logo

Maximum Points Inside the Square

Medium
100%
Updated 6/1/2025

Maximum Points Inside the Square

1. What is this problem about?

The Maximum Points Inside the Square interview question is a geometric-inspired array problem. You are given a set of points on a 2D plane, each with an associated character label. You need to find the largest square centered at the origin (0, 0) that contains some points such that no two points inside or on the boundary of the square have the same character label. The goal is to return the maximum number of points that can be included in such a square.

The challenge here is to determine the maximum side length (or half-side length) of the square that satisfies the uniqueness constraint. Since the square is centered at the origin, its "size" is determined by the maximum absolute coordinate (x or y) of the points it contains.

2. Why is this asked in interviews?

This Maximum Points Inside the Square coding problem is asked by companies like HashedIn to evaluate a candidate's ability to simplify geometric constraints. It tests whether you can recognize that a square centered at the origin can be defined by a single value (the distance from the origin to its sides). It also assesses your proficiency in using hash tables for frequency counting and sorting or binary search to find an optimal threshold. It's a great test of clean logic and efficient data management.

3. Algorithmic pattern used

The Array, Hash Table, Sorting, Binary Search, String interview pattern is common for this problem. One effective approach is to sort the points based on their "square distance" from the origin, which is max(abs(x), abs(y)). By iterating through these distances, you can check if adding all points at a certain distance would violate the unique label constraint. Alternatively, you can binary search on the possible side lengths of the square, using a hash set to check for label uniqueness at each step.

4. Example explanation

Points: (1, 1, 'a'), (2, 2, 'b'), (1, -1, 'c'), (3, 3, 'a') Distances:

  • Point 1: max(1, 1) = 1
  • Point 2: max(2, 2) = 2
  • Point 3: max(1, |-1|) = 1
  • Point 4: max(3, 3) = 3

Sorted unique distances: 1, 2, 3.

  • At distance 1: Labels are {'a', 'c'}. Both are unique. Points included: 2.
  • At distance 2: Labels so far are {'a', 'c'} + {'b'}. All unique. Points included: 3.
  • At distance 3: Labels so far are {'a', 'c', 'b'} + {'a'}. Duplicate 'a'! So, the maximum points we can include is 3 (at distance 2).

5. Common mistakes candidates make

A common error in the Maximum Points Inside the Square coding problem is not handling points with the same distance correctly. If multiple points are at the same distance, you must include all of them or none of them to maintain the square's symmetry. Candidates often forget to check for duplicate labels within the same distance group before finalizing the count. Another mistake is using the Euclidean distance instead of the Chebyshev distance (max absolute coordinate), which is what defines a square.

6. Interview preparation tip

When dealing with geometric problems involving squares or circles centered at the origin, always try to reduce the 2D coordinates to a 1D "distance" metric. For squares, this is usually max(|x|, |y|). This transformation allows you to treat the problem as a 1D sorting or searching task, which is much simpler to implement and reason about.

Similar Questions