Number of Boomerangs
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
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
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.