Prepin
Log in

You have a table displaying different distances between various cities. What is the method to calculate the average distance for each pair of cities?

Approach / Explanation

The interviewer poses this question to gauge your ability to tackle a problem that involves multiple levels of calculation and your comprehension of how to determine averages within a dataset. In this scenario, a table displaying different distances refers to a data structure, likely a matrix or graph, where each city acts as a node and the distances between pairs of cities are recorded. Your objective is to compute the average of these distances. When responding to this question, keep the following essential points in mind: Comprehend the data structure: It's important to identify whether the data is organized as a matrix, graph, or another type of structure. Understand the calculation method: You need to determine the total of all distances as well as the total number of pairs. Clarify the formula: To calculate the average, you divide the total of all distances by the total number of pairs.

Suggested Answer

Assuming that the distances between cities are represented in a 2D array or matrix, where both the rows and columns correspond to the cities and the values in the cells indicate the distances between them, you can tackle this problem in the following manner: First, loop through each cell in the upper triangle of the matrix (excluding the diagonal). The rationale for not examining the entire matrix is that the distances are typically symmetric (meaning the distance from city A to city B is equal to the distance from city B to city A), allowing you to eliminate unnecessary calculations. Next, calculate the total of these distances while keeping track of how many distances you have included. Finally, determine the average by dividing the total sum by the count of distances. In Python, this could be implemented as shown below: def average_distance ( matrix ) : total_sum = 0 count = 0 n = len ( matrix ) for i in range ( n ) : for j in range ( i + 1 , n ) : total_sum += matrix [ i ] [ j ] count += 1 return total_sum / count if count != 0 else 0

Alternative Answer

When the information is organized in a graph data structure, you can explore the graph using either the depth-first search or breadth-first search algorithms to determine all distances. The fundamental concept remains unchanged: compute the total distance and total count, followed by calculating the average. Alternatively, you can depict the data structure as an adjacency list and use a similar method to that of the matrix. Here’s how it can be implemented in Python: def average_distance ( graph ) : total_sum = 0 count = 0 for city in graph : for _ , distance in graph [ city ] : total_sum += distance count += 1 return total_sum / count if count != 0 else 0 This function simplifies the task of calculating an average by summing all the edges in the adjacency list and then dividing by the total number of distances considered.

Question Details

Difficulty & Category

Medium
data structures

Companies

Waymo

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.