Codepath

Counting Room Clusters

TIP102 Unit 9 Session 2 Standard (Click for link to problem statements)

TIP102 Unit 9 Session 2 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 25-30 mins
  • 🛠️ Topics: Trees, DFS, Cluster Counting

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is the structure of the tree?
    • The tree is a binary tree where each node represents a room in the hotel, and each node value represents the theme of the room.
  • What operation needs to be performed?
    • The function needs to count the number of distinct clusters of connected rooms that have the same theme.
  • What should be returned?
    • The function should return the number of distinct clusters.
HAPPY CASE
Input: 
    themes = ["👻", "👻", "🧛🏾", "👻", "🧛🏾", None, "🧛🏾"]
    hotel = build_tree(themes)
Output: 
    3
Explanation: 
    There are three distinct clusters: one cluster of "👻" and two clusters of "🧛🏾".

EDGE CASE
Input: 
    themes = ["👻", "👻", "👻", "👻", "👻"]
    hotel = build_tree(themes)
Output: 
    1
Explanation: 
    There is only one distinct cluster, as all rooms have the same theme.

2: M-atch

Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Cluster Counting in Trees problems, we want to consider the following approaches:

  • Depth-First Search (DFS): DFS is suitable for exploring all connected components (clusters) in a tree structure.
  • Tree Traversal: Traversal methods can help systematically explore all nodes and identify distinct clusters.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use DFS to traverse the tree and count distinct clusters. A new cluster is identified when a node has a different theme from its parent. The DFS function will recursively check the left and right subtrees to accumulate the total number of clusters.

1) Define a DFS function `dfs(node, parent_val)`:
    - If `node` is `None`, return `0` (no clusters).
    - Check if the current node starts a new cluster:
        - If the node's value differs from its parent's value, it is the start of a new cluster.
    - Recursively count clusters in the left and right subtrees:
        - Pass the current node's value as the parent value to the child nodes.
    - Return the total number of clusters found.

2) In the main function `count_clusters(hotel)`:
    - Start DFS with the root node. The root itself is always the start of a new cluster.
    - Return the result from the DFS function.

⚠️ Common Mistakes

  • Forgetting to properly handle the base case where the node is None.
  • Not correctly identifying the start of a new cluster.

4: I-mplement

Implement the code to solve the algorithm.

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def count_clusters(hotel):
    def dfs(node, parent_val):
        if node is None:
            return 0
        
        # Check if this node starts a new cluster
        cluster_count = 1 if node.val != parent_val else 0
        
        # Recursively count clusters in left and right subtrees
        cluster_count += dfs(node.left, node.val)
        cluster_count += dfs(node.right, node.val)
        
        return cluster_count
    
    # Start DFS with root node; root itself is always a new cluster
    return dfs(hotel, None)
    
# Example Usage:
themes = ["👻", "👻", "🧛🏾", "👻", "🧛🏾", None, "🧛🏾"]
hotel = build_tree(themes)

print(count_clusters(hotel))  # 3

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

- Example 1:
    - Input: 
        `themes = ["👻", "👻", "🧛🏾", "👻", "🧛🏾", None, "🧛🏾"]`
        `hotel = build_tree(themes)`
    - Execution: 
        - Perform DFS to traverse the tree.
        - Identify distinct clusters by comparing node values to parent values.
    - Output: 
        3
- Example 2:
    - Input: 
        `themes = ["👻", "👻", "👻", "👻", "👻"]`
        `hotel = build_tree(themes)`
    - Execution: 
        - Perform DFS to traverse the tree.
        - Identify distinct clusters by comparing node values to parent values.
    - Output: 
        1

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Time Complexity:

  • Time Complexity: O(N) where N is the number of nodes in the tree.
    • Explanation: Each node is visited exactly once during the DFS traversal.

Space Complexity:

  • Space Complexity: O(H) where H is the height of the tree.
    • Explanation: The recursion stack will use space proportional to the height of the tree. In a balanced tree, H is O(log N), but in the worst case (skewed tree), it could be O(N). ~~~
Fork me on GitHub