Number of Boomerangs

Algorithms
Medium
Apple
37.1K views

Given $n$ points in the plane, return the number of 'boomerangs'. A boomerang is a triplet of points $(i, j, k)$ such that the distance between $i$ and $j$ is equal to the distance between $i$ and $k$. Use a HashMap for counting distances.

Why Interviewers Ask This

Apple interviewers ask this to evaluate your ability to optimize brute-force solutions using hash maps for constant-time lookups. They specifically test if you can recognize geometric patterns, handle edge cases like duplicate points or floating-point precision, and efficiently calculate combinatorial permutations without redundant calculations.

How to Answer This Question

1. Clarify the problem by defining a boomerang as an ordered triplet where the center point is equidistant from two others, noting that order matters for (j, k). 2. Propose a brute-force O(n^3) approach first to establish a baseline, then immediately pivot to an optimized O(n^2) solution using a HashMap. 3. Explain the core logic: iterate through each point acting as the 'center', calculate distances to all other points, and store frequency counts in a map. 4. Detail the calculation formula: for any distance with count 'c', the number of valid permutations is c * (c - 1), adding this to your total result. 5. Discuss complexity analysis, emphasizing space trade-offs, and mention handling potential floating-point issues by using squared Euclidean distance to avoid precision errors.

Key Points to Cover

  • Recognize that squared Euclidean distance prevents floating-point precision errors
  • Apply the permutation formula n * (n-1) rather than combinations when order matters
  • Demonstrate clear transition from O(n^3) brute force to O(n^2) hash map optimization
  • Explicitly state that the center point must be distinct from the two outer points
  • Explain space complexity trade-offs between the input array and the hash map

Sample Answer

To solve the Number of Boomerangs problem efficiently, I would first clarify that we are looking for ordered triplets (i, j, k) where the distance from i to j equals the distance from i to k. A naive solution checking all triplets would take O(n^3) time, which is inefficient for large datasets typical in Apple's performance-critical applications. Instead, I propose an O(n^2) approach using a HashMap. The strategy involves iterating through each point in the array, treating it as the central vertex 'i'. For every other point 'j', I calculate the squared Euclidean distance to 'i' and store its frequency in a hash map. Using squared distance avoids floating-point inaccuracies associated with square roots. Once the map is populated for a specific center 'i', I iterate through the frequency values. If a specific distance appears 'c' times, it means there are 'c' candidates for 'j' and 'k'. Since the order of j and k matters, the number of valid boomerangs for this distance is c multiplied by (c minus 1). I sum these products for all distances to get the total count for that center, then repeat for all n points. This ensures we capture all unique permutations while maintaining optimal time complexity.

Common Mistakes to Avoid

  • Using standard combinations instead of permutations, ignoring that (j,k) and (k,j) are distinct boomerangs
  • Calculating actual square roots for distances, introducing unnecessary floating-point rounding errors
  • Failing to reset the hash map for each new center point, leading to incorrect aggregated counts
  • Overlooking edge cases where multiple points share the exact same coordinates

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 54 Apple questions