Lowest Region Containing Two Named Regions

Quick Overview

Lowest Region Containing Two Named Regions. Each regions list starts with a parent region followed by its directly contained regions.

Lowest Region Containing Two Named Regions

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each regions list starts with a parent region followed by its directly contained regions. Containment is transitive, and every region contains itself. The source hierarchy is a tree with a region containing all others and at most one direct parent per region. Return the smallest region containing both distinct region1 and region2. Such a region exists. ```cpp class Solution { public: string findSmallestRegion(vector<vector<string>>& regions, string region1, string region2); }; ``` ### Examples **Example 1:** ```text Input: regions = [["World","West","East"],["West","Hill","Lake"]], region1 = "Hill", region2 = "Lake" Output: "West" Explanation: West is the first common ancestor of the two child regions. ``` **Example 2:** ```text Input: regions = [["World","West","East"],["West","Hill","Lake"]], region1 = "West", region2 = "East" Output: "World" Explanation: Neither queried region contains the other; World contains both. ``` ### Constraints - There are 2 to 10000 region lists, each with 2 to 20 names. - All supplied and queried names have length 1 to 20 and use English letters. - region1 and region2 are different. - A region containing all others exists, each region has at most one direct parent, and the smallest common region exists. ### Contract considerations Region names are case-sensitive English-letter strings under the original constraints; no raw TreeNode adapter is introduced. ```hint Read the boundary condition Can either queried region itself be returned, and does the order of the supplied region lists matter? ``` ### Boundary reminders Region names are case-sensitive English-letter strings under the original constraints; no raw TreeNode adapter is introduced.

Overview: Lowest Region Containing Two Named Regions. Each regions list starts with a parent region followed by its directly contained regions.

Airbnb logo
Airbnb
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each regions list starts with a parent region followed by its directly contained regions. Containment is transitive, and every region contains itself. The source hierarchy is a tree with a region containing all others and at most one direct parent per region. Return the smallest region containing both distinct region1 and region2. Such a region exists.

class Solution {
public:
    string findSmallestRegion(vector<vector<string>>& regions, string region1, string region2);
};

Examples

Example 1:

Input: regions = [["World","West","East"],["West","Hill","Lake"]], region1 = "Hill", region2 = "Lake"
Output: "West"
Explanation: West is the first common ancestor of the two child regions.

Example 2:

Input: regions = [["World","West","East"],["West","Hill","Lake"]], region1 = "West", region2 = "East"
Output: "World"
Explanation: Neither queried region contains the other; World contains both.

Constraints

  • There are 2 to 10000 region lists, each with 2 to 20 names.
  • All supplied and queried names have length 1 to 20 and use English letters.
  • region1 and region2 are different.
  • A region containing all others exists, each region has at most one direct parent, and the smallest common region exists.

Contract considerations

Region names are case-sensitive English-letter strings under the original constraints; no raw TreeNode adapter is introduced.

Boundary reminders

Region names are case-sensitive English-letter strings under the original constraints; no raw TreeNode adapter is introduced.