DoorDash | Phone Screen

Anonymous User
3518
Jun 02, 2022
Jun 02, 2022

"""
A number of cities are arranged on a graph that has been divided up like an ordinary Cartesian plane. Each city is located at an integral (x, y) coordinate intersection. City names and locations are given in the form of three arrays: c, x, and y, which are aligned by the index to provide the city name (c[i]), and its coordinates, (x[i], y[i]). Determine the name of the nearest city that shares either an x or a y coordinate with the queried city. If no other cities share an x or y coordinate, return 'NONE'. If two cities have the same distance to the queried city, q[i], consider the one with an lexographically shorter name (i.e. 'ab' < 'aba' < 'abb') as the closest choice. The distance is the Manhattan distance, the absolute difference in x plus the absolute difference in y.

Example:
Input: n = 3; c = ['c1', 'c2', 'c3']; x = [3, 2, 1] ; y = [3, 2, 3]; q = ['c1', 'c2', 'c3']
Expected Answer: ['c3', 'NONE', 'c1']

Explanation: The three points at (x[i], y[i]) are (3, 3), (2, 2) and (1, 3) represent the coordinates of the cities on the graph. The nearest city to c1 is c3, which shares a y value (distance = (3-1) + (3-3) = 2). City c2 does not have a nearest city as none share an x or y with c2, so this query returns 'NONE'. A query of c3 returns c1 based on the first calculation. The return array after all queries are complete is ['c3', 'NONE', 'c1'].

Function Description

closest_straight_city has the following parameter(s):
    string c[n]: an array of strings that represent the names of each city[i]
    int x[n]: the x coordinates of each city[i]
    int y[n]: the y coordinates of each city[i]
    string q[m]: the names of each city to query
Returns:
    string[m]: an array of m strings where the index of i element denotes the return value of the index of i query

"""

Comments (7)