Coordinates as immutable records
Python data engineering interview problem. Difficulty: beginner. Pattern: Searching. About 10 minutes. Free to practice.
A warehouse stores pickup coordinates. Dispatch wants the stored point closest to a reference location. Coordinates should be treated as immutable records, not loose tuples you accidentally mutate.
A location service stores points as fixed (x, y) pairs (tuples) and needs the stored point closest to a reference point. Return the point in points that is closest to ref. If two points are equally close, return the first one in the list. If the list is empty, return None. Write closest_coordinate(points, ref). With points [(0, 0), (2, 2)] and reference (1, 1), both are the same distance away, so the first, (0, 0), is returned. With one point [(5, 5)] the answer is (5, 5). The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Compare distances from each point to ref.
- On a tie, return the earlier point.
- Return None for an empty list.
- Return the point as a tuple.
Constraints
- Each point and ref are pairs of numbers.
- You may compare squared distances, because the square root does not change the order.
Examples
Input: closest_coordinate([(0, 0), (2, 2)], (1, 1)) Output: (0, 0) This input follows the stated rules and produces this output.
Topics: lakebench, python, tuples.
More Python interview questions · All interview problems · Learn data engineering
Coordinates as immutable records
Interview-style drill: Given a list of (lat, lon) tuples, return the pair closest to a reference point.
Given stored points and a reference point, return the closest coordinate record.