PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of planar geometry and algorithmic reasoning for identifying collinearity, including handling duplicate points and edge cases in integer coordinates.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Find maximum collinear points

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a list of 2D points with integer coordinates, return the **maximum number of points that lie on the same straight line**. ### Input - `points`: a list of `n` points, where each point is `[x, y]`. ### Output - An integer: the maximum count of points that are collinear. ### Constraints - `1 <= n <= 2000` - Coordinates are integers in a reasonable range (e.g., `[-10^4, 10^4]`). - Duplicate points may exist. ### Notes - Lines can be vertical, horizontal, or any slope. - The result should count all points on the best line, including duplicates.

Quick Answer: This question evaluates understanding of planar geometry and algorithmic reasoning for identifying collinearity, including handling duplicate points and edge cases in integer coordinates.

Given a list of 2D points with integer coordinates, return the maximum number of points that lie on the same straight line. Points may be duplicated, and each duplicate counts as a separate point. Lines can be vertical, horizontal, or have any slope.

Constraints

  • 1 <= len(points) <= 2000
  • -10^4 <= x_i, y_i <= 10^4
  • Duplicate points may exist and must be counted in the result

Examples

Input: ([[1,1],[2,2],[3,3]],)

Expected Output: 3

Explanation: All three points lie on the line y = x.

Input: ([[1,1],[1,1],[2,2],[3,3]],)

Expected Output: 4

Explanation: The duplicate point [1,1] counts separately, and all four points lie on the same line.

Input: ([[2,3],[2,4],[2,5],[0,0]],)

Expected Output: 3

Explanation: The points [2,3], [2,4], and [2,5] lie on the vertical line x = 2.

Input: ([],)

Expected Output: 0

Explanation: There are no points, so the answer is 0.

Input: ([[0,0],[0,0],[0,0]],)

Expected Output: 3

Explanation: All three points are duplicates of the same location, so they all count on the same line.

Input: ([[0,0],[1,1],[1,-1],[2,2],[3,3],[2,-2]],)

Expected Output: 4

Explanation: The points [0,0], [1,1], [2,2], and [3,3] are collinear on y = x.

Hints

  1. Try fixing one point as an anchor and grouping all other points by the line they form with that anchor.
  2. Avoid floating-point slopes. Store each slope as a reduced `(dy, dx)` pair using `gcd`, and use a consistent sign convention. Count duplicate points separately.
Last updated: Jun 5, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)