Magicsheet logo

Smallest Common Region

Medium
84.1%
Updated 6/1/2025

Smallest Common Region

What is this problem about?

The "Smallest Common Region" interview question is essentially a "Lowest Common Ancestor" (LCA) problem framed in the context of geographical regions. You are given a list of regions where the first element is the parent and the subsequent elements are child regions. You need to find the smallest region that contains two specific target regions. This "Smallest Common Region coding problem" represents data as a tree where each region is a node and its parents are ancestors.

Why is this asked in interviews?

Airbnb and TikTok ask this to test your ability to model hierarchical data. It evaluates your knowledge of tree traversal and your ability to work with parent-child relationships using hash tables. It's a practical variation of a classic graph algorithm, demonstrating how you can apply standard computer science concepts to real-world data like location hierarchies or organizational charts.

Algorithmic pattern used

This problem follows the "Hash Table and Tree/Graph interview pattern".

  1. Build a map where each child region points to its parent region. This effectively allows you to "climb up" the tree from any leaf node.
  2. To find the smallest common region for region1 and region2:
    • Trace the path from region1 to the root, storing every ancestor in a hash set.
    • Trace the path from region2 to the root. The first ancestor of region2 that is already in the hash set is the smallest common region.

Example explanation

Regions: [["Earth", "USA", "Canada"], ["USA", "New York", "California"], ["New York", "NYC", "Albany"]] Find common region for "NYC" and "California".

  1. Parent Map: {USA: Earth, Canada: Earth, New York: USA, California: USA, NYC: New York, Albany: New York}
  2. Path for "NYC": NYC -> New York -> USA -> Earth. Ancestor Set: {NYC, New York, USA, Earth}
  3. Path for "California": California -> USA.
  4. "USA" is the first element in California's path that is also in NYC's ancestor set. Result: "USA".

Common mistakes candidates make

A common mistake is trying to build a child-pointing tree (parent to children) and using DFS/BFS, which is more complex than simply mapping children to parents. Another error is not handling the case where one region is an ancestor of the other. Some candidates also forget that the list of regions can be quite long, so efficient lookups using a hash map are essential.

Interview preparation tip

When you need to find a common ancestor in a general tree (where you only know parent links), the "trace and set" method is the most intuitive and efficient. Always consider if you can simplify a tree problem by looking "up" (child to parent) instead of "down" (parent to child). This is a frequent shortcut in "Smallest Common Region interview question" discussions.

Similar Questions