Amazon | Phone screen | UnionFind

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-11-05Added By Anonymous
Source: Link

Company: Amazon
Role: unknown
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

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