Max Points on a Line
Given an array of points, find the maximum number of points that lie on the same straight line. This requires using slope and floating-point comparison carefully.
Why Interviewers Ask This
Google asks this problem to evaluate a candidate's ability to handle geometric edge cases and numerical precision issues. They specifically test whether you can move beyond brute-force solutions to optimize time complexity while managing floating-point inaccuracies inherent in slope calculations. It reveals your depth of understanding regarding hash maps, mathematical representations of lines, and robust error handling.
How to Answer This Question
Key Points to Cover
- Explicitly rejecting floating-point division to prevent precision errors
- Using Greatest Common Divisor (GCD) to normalize slope representations as integer pairs
- Implementing O(n^2) time complexity by iterating through every point as an anchor
- Dedicated logic for handling vertical lines and duplicate coordinates
- Demonstrating awareness of Google's focus on numerical stability and edge-case robustness
Sample Answer
Common Mistakes to Avoid
- Relying on double-precision floats for slopes, which leads to incorrect equality checks due to rounding errors
- Failing to account for duplicate points in the input array, resulting in undercounted intersections
- Ignoring vertical lines where the denominator becomes zero, causing division by zero exceptions
- Attempting to store full line equations rather than just slopes relative to a specific anchor point
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.