Nearest Neighbouring City
Updated
Problem statement
A number of cities are placed at distinct integer coordinates on a Cartesian plane. The arrays cities, xCoordinates, and yCoordinates describe each city's name and position.
For every name in queries, find the nearest other city that shares either the same x-coordinate or the same y-coordinate with the queried city. Distance is measured using Manhattan distance: |x1 - x2| + |y1 - y2|.
- If no other city shares an x-coordinate or y-coordinate with the queried city, return
"NONE". - If multiple eligible cities have the same minimum distance, return the lexicographically smallest city name.
Return the answers in the same order as queries.
Function
findNearestCities(numOfCities: int, cities: String[], xCoordinates: int[], yCoordinates: int[], numOfQueries: int, queries: String[]) → String[]Examples
Example 1
numOfCities = 3cities = ["c1", "c2", "c3"]xCoordinates = [3, 2, 1]yCoordinates = [3, 2, 3]numOfQueries = 3queries = ["c1", "c2", "c3"]return = ["c3", "NONE", "c1"]y x 0123401234 c3 c2 c1
The plot places c1 = (3, 3), c2 = (2, 2), and c3 = (1, 3) at their input coordinates. Cities c1 and c3 share y-coordinate 3, while c2 shares neither coordinate with another city.
Example 2
numOfCities = 3cities = ["fastcity", "bigbanana", "xyz"]xCoordinates = [23, 23, 23]yCoordinates = [1, 10, 20]numOfQueries = 3queries = ["fastcity", "bigbanana", "xyz"]return = ["bigbanana", "fastcity", "bigbanana"]y x 05101520250510152025 fastcity (23, 1) bigbanana (23, 10) xyz (23, 20)
All three cities lie on the vertical line x = 23. Their y-coordinates are 1, 10, and 20.
Example 3
numOfCities = 3cities = ["london", "warsaw", "hackerland"]xCoordinates = [1, 10, 20]yCoordinates = [1, 10, 10]numOfQueries = 3queries = ["london", "warsaw", "hackerland"]return = ["NONE", "hackerland", "warsaw"]y x 0510152025051015 london (1, 1) warsaw (10, 10) hackerland (20, 10)
london = (1, 1) is isolated. warsaw = (10, 10) and hackerland = (20, 10) share y-coordinate 10, so they are nearest to each other.
Example 4
numOfCities = 5cities = ["green", "red", "blue", "yellow", "pink"]xCoordinates = [100, 200, 300, 400, 500]yCoordinates = [100, 200, 300, 400, 500]numOfQueries = 5queries = ["green", "red", "blue", "yellow", "pink"]return = ["NONE", "NONE", "NONE", "NONE", "NONE"]y x 01002003004005006000100200300400500600 green red blue yellow pink
Every city lies on the diagonal x = y, but no two distinct cities share an x-coordinate or a y-coordinate. Therefore every query returns "NONE".
Constraints
- 1 ≤ n, m ≤ 10^5
- 1 ≤ x[i], y[i] ≤ 10^9
- 1 ≤ length of q[i] and c[i] ≤ 10
- Each character of all c[i] and q[i] is in the range ascii[a-z, 0-9, -]
- All city name values, c[i], are unique
- All cities have unique coordinates
More Akuna Capital interview questions
- Last Digit of the Ways to Split Traders Between Two OfficesOA · Seen Dec 2026
- Light Bulbs Left On After All Engineers FinishOA · Seen Dec 2026
- Delivery Management SystemOA · Seen Nov 2026
- Count Server ReplacementsOA · Seen Nov 2026
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Remaining Drone PowerOA · Seen Nov 2026
- Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Rolling MetricsOA · Seen Oct 2026
- Vowel Substring GameOA · Seen Sep 2026
Similar questions at other companies
- API Rate Limit Violation DetectionTekion · OATekion interview questions
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Build Sessions from Unordered Events (Part 1)Scale AI · OAScale AI interview questions
- Minimum Distinct Prefix CostChubb · OAChubb interview questions
- Decode Words with Anagram KeysCresta · OACresta interview questions
- Count Redundant File BackupsGoldman Sachs · OAGoldman Sachs interview questions
- Top Mutual-Friend RecommendationsVisa · OAVisa interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)