← Questions

Largest group of mutual strangers in an acquaintance forest

0
MicrosoftCoding

A coding interview question asked at Microsoft. It tests graphs, tree dp and independent set.

You are given n people numbered 0 to n - 1 and a list connections, where each pair [a, b] means a and b know each other (the relation is symmetric).

The acquaintances contain no cycle: you can never start at a person, follow a chain of distinct acquaintances, and come back to the start. So every friend group is a tree, and some people may know nobody.

Implement:

def largest_stranger_group(n: int, connections: list[list[int]]) -> list[int]

Return a largest possible group of people in which no two members know each other, as a sorted list. If several groups tie, return any of them. Raise an error if the input breaks the rules (a cycle, a repeated pair, a person paired with themselves, or an id out of range).

Constraints

  • 0 <= n <= 10^5
  • 0 <= len(connections) <= n - 1
  • Aim for linear time. Chains can be as long as n, so avoid deep recursion.

Example 1

n = 6
connections = [[0, 1], [0, 2], [0, 3], [0, 4], [0, 5]]
output: [1, 2, 3, 4, 5]

Person 0 knows everyone else. Leaving 0 out lets the other five form a group of strangers.

Example 2

n = 7
connections = [[0, 1], [1, 2], [3, 4], [4, 5]]
output: [0, 2, 3, 5, 6]   (size 5)

Two chains of three people each give two strangers (the ends), and person 6 knows nobody, so they always join.

Clarifying questions

  • Are [a, b] and [b, a] both a "repeated pair"?
  • When several groups tie, is any of them accepted?
  • For n = 0, should we return []?
  • Must the input validation run before any work, or can it fail partway through?

What a strong answer covers

  • Recognizes the problem as a maximum independent set, which is easy on a forest.
  • Validates the input with union-find: an edge whose two ends are already connected is a cycle. It also rejects self-pairs, repeated pairs and ids out of range.
  • Uses either a tree DP (best size with the node taken and with it left out, combined bottom-up) or a greedy that always takes a leaf and removes its neighbour. It explains why taking a leaf is never worse.
  • Processes nodes in an iterative order (BFS order, reversed) to avoid deep recursion. That is O(n) time and memory.
  • Rebuilds the chosen set from the DP choices, and returns it sorted. Isolated people are always included.
  • Tests a star, a chain, isolated nodes, n = 0, a single edge, and each invalid input.

Follow-ups

  • Each person has a weight: maximize the total weight. The greedy breaks; the DP still works.
  • Count how many maximum groups exist.
  • The graph may contain cycles: in general this is NP-hard. What is still possible for bipartite graphs?
  • Return the smallest set of people who between them know everyone.

Where it's asked

What it tests

graphs · tree dp · independent set

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding