# Degrees of Friendship — Coding Question

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/3628/degrees-of-friendship
Source observed: 2026-11-05T00:29:27.239Z
Last checked: 2026-11-05T00:29:27.239+00:00
Indexed: 2026-11-06T05:25:59.548+00:00

---

# Degrees of Friendship

**Difficulty:** Easy  
**Role:** [Software Engineer](https://www.aced.io/questions?role=swe)  
**Company:** [Google](https://www.aced.io/questions?company=google)  
**Categories:** [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms), [Coding](https://www.aced.io/questions?type=coding)  
**Reported last asked:** a year ago

We provide this coding interview question through Aced, formerly Exponent. The question asks you to calculate the minimum number of friendship connections between two users in a social network—their “friend distance,” similar to a degree of connection on LinkedIn.

## Problem

We are building a new social network where users are friends with one another. To make better friend recommendations, write a function `friend_distance(friends, userA, userB)` that returns the minimum distance between two users.

The input, `friends`, is a 2D array. Each entry `(A, B)` records the friendship status between user A and user B:

- Each user’s friendships are represented by a row.
- Each column represents a user.
- If `friends[0][1]` is `1`, user 0 is friends with user 1.
- If `friends[1][2]` is `1`, user 1 is friends with user 2.

Return `-1` when there are no connections between the two users.

## Python

```python
def friend_distance(friends, userA, userB):
    # Your code here...
```

## Example

In the example below, users 0 and 1 are direct friends, as shown by `(0, 1)` and `(1, 0)` being `1`. Users 1 and 2 are also direct friends, as shown by `(1, 2)` and `(2, 1)` being `1`.

Therefore:

- The distance between users 0 and 1 is `1`.
- The distance between users 1 and 2 is `1`.
- Users 0 and 2 are not direct friends, but both connect through user 1, so their distance is `2`.

```python
friends = [
    [0, 1, 0],
    [1, 0, 1],
    [0, 1, 0]
]

friend_distance(friends, 0, 1)  # => 1
friend_distance(friends, 1, 2)  # => 1
friend_distance(friends, 0, 2)  # => 2
```

## Practice interface

The question includes separate **Question**, **Solution**, and **Community** tabs. Four hints and three challenges are available as expandable sections, and the page provides an editor with **Output**, **Test Results**, and **Console** panels. Running code requires [signing up](https://www.aced.io/signup?src=lesson).

The page also offers an [Upgrade](https://www.aced.io/upgrade?src=questions) option for detailed company statistics.

## Related courses

### [Software Engineering Interview Prep](https://www.aced.io/courses/software-engineering)

![Software Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/4VkuYewBHUFIVUS5oJDW5h/02e833439b1289f25f38f13f81cbdca3/adobe_stock_optimized.jpeg?w=800&h=800&fm=webp&q=75)

Our software engineering interview course helps candidates prepare for roles at Google, Amazon, Microsoft, Meta, Apple, and other companies. It includes mock interviews, frameworks, and advice from senior candidates, with practice covering data structures, algorithms, system design, people management, behavioral interviews, and more.

### [System Design Interviews](https://www.aced.io/courses/system-design-interviews)

![System Design Interviews](https://images.ctfassets.net/x78yjrjc11pq/78x7miFZFjBCrM5GqftrMJ/2f6d26e632c4e7265c49f6a180227edb/system_design_track_compressed_26ac98a46e__281_29_5be2280f31.jpeg?w=800&h=800&fm=webp&q=75)

Our system design course covers current system design questions across product, infrastructure, and AI domains. It includes in-depth video examples, written breakdowns, reference patterns, and mock interview videos featuring senior engineers and managers.

[View all questions](https://www.aced.io/questions)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3628%2Fdegrees-of-friendship&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%2F3628%2Fdegrees-of-friendship). Opening a form does not execute a tool.