Coding questions · Amazon
Identify Unique Unordered City Pairs in Flight Log
This question evaluates competency in data manipulation, deduplication of symmetric relationships, and handling unordered pairs within relational or tabular datasets using SQL or Python/Pandas.
FLIGHTS
+----------------+---------------+
| departure_city | arrival_city |
+----------------+---------------+
| NYC | LAX |
| LAX | NYC |
| ORD | ATL |
+----------------+---------------+
Scenario
You work with an airline flight log. Each row records a flight’s origin and destination city. Management wants to see all distinct city pairs regardless of flight direction (NYC–LAX is the same as LAX–NYC).
Question
Write a SQL query (or equivalent Python/Pandas snippet) that returns the unique unordered city pairs from the FLIGHTS table, treating (A,B) and (B,A) as the same route combination.
Hints
Normalize the order of the two cities—e.g., use LEAST/GREATEST or sort values—then GROUP BY the normalized pair.
Unique Unordered Flight City Pairs
Table FLIGHTS
| column | type | constraints |
|---|---|---|
| departure_city | VARCHAR(3) | |
| arrival_city | VARCHAR(3) |
Sample data:
| arrival_city | departure_city |
|---|---|
| LAX | NYC |
| NYC | LAX |
| ATL | ORD |
Expected output
Each unordered pair of cities appears once, with the two cities normalized to a consistent order (e.g., alphabetical within the pair).
| city_a | city_b |
|---|---|
| ATL | ORD |
| LAX | NYC |
Hints
- Normalize the pair order using LEAST and GREATEST (or equivalent CASE logic).
- Use DISTINCT (or GROUP BY) on the normalized pair to remove direction duplicates.