cswork / libraryOpen practice workspace

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

columntypeconstraints
departure_cityVARCHAR(3)
arrival_cityVARCHAR(3)

Sample data:

arrival_citydeparture_city
LAXNYC
NYCLAX
ATLORD

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_acity_b
ATLORD
LAXNYC

Hints

  1. Normalize the pair order using LEAST and GREATEST (or equivalent CASE logic).
  2. Use DISTINCT (or GROUP BY) on the normalized pair to remove direction duplicates.