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?- A. 5
- B. 3
- C. 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
- Problem: What is the time complexity of the Hopcroft-Karp algorithm for maximum matching on V vertices and E edges?
- Problem: Which technique gives the answer to 'minimum number of platforms' by sorting events?
- Problem: Which of these is the standard O(n log n) algorithm for the longest increasing subsequence?
- Problem: What is the standard complexity of computing all pairs of shortest paths in a graph with V vertices using repeated squaring of…
- Problem: Which result says that every planar graph is 5-colourable?
- Problem: What is the purpose of the 'lowest common multiple of the array' technique in competitive programming?