Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: What is the maximum number of points on a line among (0,0), (1,1), (2,2), (3,5), (4,4) (collinear with the diagonal)? Run the code.

Problem: What is the maximum number of points on a line among (0,0), (1,1), (2,2), (3,5), (4,4) (collinear with the diagonal)? Run the code.
pts = [(0, 0), (1, 1), (2, 2), (3, 5), (4, 4)]
best = 0
for a in pts:
    for b in pts:
        if a != b:
            cnt = sum(1 for c in pts if (b[0] - a[0]) * (c[1] - a[1]) == (b[1] - a[1]) * (c[0] - a[0]))
            best = max(best, cnt)
print(best)
What is printed?
  1. A. 5
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: C. 4

The points (0,0), (1,1), (2,2) and (4,4) lie on the same line y = x, giving 4 collinear points.

More Coding Interview Problems questions

Exams that ask this topic