Closest Points

Upasana | June 21, 2019 | 1 min read | 17 views

In this problem, you are given a list of x, y values that represent points in an x, y coordinate system. Process the list of x, y coordinates to determine which two are the closest. In each test case, there will be only one pair of points that are the "closest". The distance (d) between two points (x1, y1) and (x2, y2) is given by the following formula:


Consider the three points listed below.

24 12 25 54 33 13

The three points are (24, 12), (25, 54), and (33, 13). For output you will need to identify the two closest points and the distance between them. For the three points above the results are:

Closest 2 points are (24, 12) and (33, 13). The distance is 9.055385138137417

def calculate_dist_btwn_points(p1,p2):
    dist = ((p2[0]-p1[0])**2 + (p2[1]-p2[1])**2)**0.5
    return dist

def get_closest_pts_dist(list_of_num):
    tp = []
    for i,j in zip(list_of_num[::2],list_of_num[1:][::2]):
    for i,j in enumerate(tp):
        # print(j)
    return tps[dists.index(max(dists))], max(dists)

print(get_closest_pts_dist([24, 12, 25, 54, 33, 13]))

([(25, 54), (33, 13)], 8.0)

Top articles in this category:
  1. Flask Interview Questions
  2. Google Data Scientist interview questions with answers
  3. Top 100 interview questions on Data Science & Machine Learning
  4. Python coding challenges for interviews
  5. AWS Lambda Interview Questions for Developers
  6. How to design a customer satisfaction survey
  7. When using Gaussian mixture model, how do you know it is applicable

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.