Amazon | Phone screen | UnionFind

Anonymous User
442
Nov 05, 2024

Assume there are multiple countries on a planet,
represented by a list of pairs of countries, e.g., <A, B>,
indicating that countries A and B are allies.

Design Requirements:
Design a data structure that can efficiently handle the following operations:

  1. Initialize the data structure with a list of pairs of countries, each pair representing an alliance (e.g., <A, B>).
  2. Check if two countries <M, N> are allies, either directly or indirectly through a chain of alliances.
  3. Add a new alliance between any two countries <X, Y>, updating the data structure to reflect this change.
  4. Return the number of distinct groups where all countries in a group are connected through alliances.
Comments (1)