# Calculate the average distance between cities

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/1195/average-distance-between-cities
Source observed: 2026-11-04T21:42:51.028Z
Last checked: 2026-11-04T21:42:51.028+00:00
Indexed: 2026-11-05T03:48:09.295+00:00

---

# Calculate the average distance between cities

We provide this medium-difficulty data structures and algorithms coding question for data scientist interview preparation. The question concerns a table of distances between city pairs recorded by different car GPS systems.

## Task

Given a table with these columns:

- `origin`: the origin city in the pair
- `destination`: the destination city in the pair
- `distance`: the distance between `origin` and `destination`

Write a function that calculates the average distance for each pair of cities and returns a new table with `city_pair` and `average_distance`.

### Required output

| Column | Type and requirements |
|---|---|
| `city_pair` | `varchar`; put the two city names in alphabetical order and separate them with a hyphen, such as `CityA-CityB` |
| `average_distance` | `float`, rounded to 2 decimal places |

Order the results by ascending `average_distance`.

## Example

### Input

| origin | destination | distance |
|---|---|---:|
| CityA | CityB | 100 |
| CityA | CityB | 110 |
| CityB | CityA | 130 |

Treat the rows for `CityA`–`CityB` and `CityB`–`CityA` as the same city pair. The average is:

```text
(100 + 110 + 130) / 3 = 113.33
```

### Expected output

| city_pair | average_distance |
|---|---:|
| CityA-CityB | 113.33 |

The page includes an editor with Python selected, but no solution is shown here. Running code requires [sign-up](https://www.aced.io/signup?src=lesson). The detailed company statistics for this question are available through [Upgrade](https://www.aced.io/upgrade?src=questions).

## Interview details

The question is associated with:

- **Role:** [Data Scientist](https://www.aced.io/questions?role=data-science)
- **Companies:** [BCG Gamma](https://www.aced.io/questions?company=bcg-gamma) and [Waymo](https://www.aced.io/questions?company=waymo)
- **Categories:** [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms), [Coding](https://www.aced.io/questions?type=coding), and [Estimation](https://www.aced.io/questions?type=estimation)
- **Interview activity:** 3 interviews; last asked a month ago

## Interview experience

One shared experience is associated with this question: [BCG Gamma · Data Scientist interview](https://www.aced.io/experiences/bcg-gamma-data-scientist-interview-5d8528). The contributor wrote:

> “Online assesment was challenging due to time pressure even though the questions are not so difficult but hard to manage syntax properly without gwtting any help.”

## Related question

- [Find the shortest distance between two points.](https://www.aced.io/questions/3219/find-shortest-distance-between-points)

## Related courses

### [Data Science Interview Prep](https://www.aced.io/courses/data-science)

We offer preparation for data science roles at Google, Amazon, Microsoft, Meta, Apple, and other companies through mock interviews, frameworks, and advice from senior candidates. Topics include statistics, experimentation, coding, SQL, machine learning, and behavioral interviews.

![Data Science Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/3U4C5C0OkDR0UeGch4AmlE/7ad489430b676a088cb973307fe77884/Data_Science_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

### [SQL Interviews](https://www.aced.io/courses/sql-interviews)

We help candidates prepare for SQL interviews with syntax basics, aggregations, and window functions. The course covers manipulating data, writing complex queries, and analyzing information from diverse datasets.

![SQL Interviews](https://images.ctfassets.net/x78yjrjc11pq/62mN54S54HpLIOmyG8qbnq/54deaf5e397d0807d71c0e27e21e5ee3/sql_illustration_2.jpg?w=800&h=800&fm=webp&q=75)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1195%2Faverage-distance-between-cities&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1195%2Faverage-distance-between-cities). Opening a form does not execute a tool.