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 queryI was able to explain the approach and determine the time complexity as O(n * log n) using two hash maps and a priority queue. It took me around 35 minutes to optimize the solution to this point. However, the interviewer didn’t let me write any code and seemed a bit inflexible.
In the last 10 minutes, he suddenly asked me to think of a binary search-based approach, which left me confused because I wasn’t sure how binary search would apply to this problem. Before I could work through it, time was up.
I felt quite frustrated — the interviewer was rude and didn’t seem willing to listen to alternative approaches. Sometimes, it feels like they just memorize one solution and aren't open to others. Honestly, if we gave them the same problem in an interview setting, they might not even be able to solve it themselves.
I just wanted to ask — for someone who hasn’t seen this problem before, is it realistically solvable within 45 minutes?


