Nearest Points

There are N points arranged on a graph. Each point is in the format of an integer (x, y) coordinate intersection.

Write an Algorithm to find out the nearest point that shares either an x or an y coordinate with the queried point. If such points, return null(python None/java null). If two points have the same distance to the queried point, return the one with alphabetically smaller name.

Here use the Euclidean distance to calculate the distance of two points.

Input:

numOfPoints, int

pointNames, String[]

xCoordinates, int[]

yCoordinates, int[]

numOfQueries, int

queries, String[], the name of the queried points.

(1 < length of queries[i] and points[i] <= 10)

Output:

Return a list of strings representing the name of the nearest point of the queried point.

Example:

Input:

numOfCities = 3

cities = ["p1", "p2","p3"]

xCoordinates = [3, 2, 1]

yCoordinates = [3, 2, 3]

numOfQueries = 3

queries = ["p1", "p2", "p3"]

Output:

[“p3”, null , “p1”]


More Amazon Online Assessment Questions 2020 Fall



Get one-to-one training from Google Facebook engineers

Top-notch Professionals

Learn from Facebook and Google senior engineers interviewed 100+ candidates.
Most recent interview questions and system design topics gathered from aonecode alumnus.
One-to-one online classes. Get feedbacks from real interviewers.

Customized Private Class

Already a coding expert? - Advance straight to hard interview topics of your interest.
New to the ground? - Develop basic coding skills with your own designated mentor.
Days before interview? - Focus on most important problems in target company question bank.