Question bank › Sorting
Dsa Sorting Easy

City Distance Sorting

You are given a list of cities represented by their names and their Euclidean coordinates (x, y). Your task is to sort these cities by their distance from the origin (0, 0) in ascending order. In case of a tie in distance, sort them alphabetically by city name. Input: The first line contains an integer N (1 ≤ N ≤ 100) — the number of cities. The next N lines each contain a string (city name, 1 ≤ len(name) ≤ 100) followed by two integers (x and y, -1000 ≤ x, y ≤ 1000). Output: Print the sorted list of cities in the format [[city1, x1, y1], [city2, x2, y2], ...]. Example: Input: 3 CityA 1 2 CityB 2 1 CityC 1 1 Output: [[CityC, 1, 1], [CityA, 1, 2], [CityB, 2, 1]]

Key concepts

sortingcoordinatesgeospatial

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.