37 · Design Data Structures

#2013 Detect Squares

Mediumsolve on LeetCode ↗

frequently reported atGoogleAmazon

the problem

In plain words

Points arrive one at a time on a grid. add(point) stores the point [x, y]; adding the same point again stores another copy, and copies count as different points. count(point) returns how many ways you can choose three stored points that make, together with the query point, a square with sides parallel to the axes and an area above zero.

in add([3,10]), add([11,2]), add([3,2]), count([11,10]), count([14,8]), add([11,2]), count([11,10])

out null, null, null, 1, 0, null, 2

With (11,10), the points (3,10), (3,2) and (11,2) make a square of side 8. A second copy of (11,2) gives a second choice for that corner.

  • 0 ≤ x, y ≤ 1000
  • At most 3000 calls to add and count in total

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #149
      Max Points on a Line
      Hardin Math & Numbers
    2. #1
      Two Sum
      Easyin Hashing
    3. #380