Codepath

Smallest Pearl Above Minimum Size

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

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15-20 mins
  • 🛠️ Topics: Trees, Binary Search Trees, Inorder Successor

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 Search Tree (BST) where each node represents a pearl with a specific size.
  • What operation needs to be performed?
    • The function needs to find the smallest pearl size greater than a given min_size.
  • What should be returned?
    • The function should return the value of the smallest pearl greater than min_size, or None if no such pearl exists.
HAPPY CASE
Input: pearls = Pearl(3, Pearl(1, None, Pearl(2)), Pearl(5, Pearl(4), Pearl(8))), min_size = 3
Output: 5
Explanation: The smallest pearl greater than 3 is 5.

EDGE CASE
Input: pearls = Pearl(3, Pearl(1, None, Pearl(2)), Pearl(5, Pearl(4), Pearl(8))), min_size = 8
Output: None
Explanation: There is no pearl larger than 8, so the output is `None`.

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 Binary Search Tree (BST) problems, we want to consider the following approaches:

  • BST Search: Use the properties of the BST to efficiently locate the smallest pearl larger than min_size.
  • Inorder Successor: The inorder successor in a BST is the smallest node that is larger than the given node.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the BST to find the smallest pearl greater than min_size. If the current node's value is greater than min_size, it's a potential candidate, and we move to the left subtree to find a smaller valid pearl. If the current node's value is less than or equal to min_size, we move to the right subtree.

1) Initialize a variable `candidate` to `None` to store the potential smallest pearl larger than `min_size`.
2) Set `current` to the root of the tree (`pearls`).
3) While `current` is not `None`:
    - If `current.val` is greater than `min_size`, update `candidate` to `current`.
    - Move to the left child to see if there is a smaller valid pearl.
    - If `current.val` is less than or equal to `min_size`, move to the right child.
4) After the loop, return `candidate.val` if `candidate` is not `None`, otherwise return `None`.

⚠️ Common Mistakes

  • Not correctly identifying the smallest pearl larger than min_size.
  • Incorrectly handling edge cases where no valid pearl exists above min_size.

4: I-mplement

Implement the code to solve the algorithm.

class Pearl:
    def __init__(self, size, left=None, right=None):
        self.val = size
        self.left = left
        self.right = right

def pick_pearl(pearls, min_size):
    candidate = None
    current = pearls
    
    while current:
        if current.val > min_size:
            candidate = current  # Potential candidate for smallest larger pearl
            current = current.left  # Check if there's a smaller valid pearl
        else:
            current = current.right  # Move to the right subtree
    
    return candidate.val if candidate else None

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: `pearls = Pearl(3, Pearl(1, None, Pearl(2)), Pearl(5, Pearl(4), Pearl(8)))`, `min_size = 3`
    - Execution: 
        - Start at root (3).
        - 3 is equal to `min_size`, move to the right child (5).
        - 5 > 3, update `candidate` to 5, move to the left child (4).
        - 4 > 3, update `candidate` to 4, left child is `None`.
        - Return `candidate` value, which is 4.
    - Output: `4`
- Example 2:
    - Input: `pearls = Pearl(3, Pearl(1, None, Pearl(2)), Pearl(5, Pearl(4), Pearl(8)))`, `min_size = 8`
    - Execution: 
        - Start at root (3).
        - 3 < 8, move to the right child (5).
        - 5 < 8, move to the right child (8).
        - 8 = `min_size`, move to the right child which is `None`.
        - No candidate found, return `None`.
    - Output: `None`

6: E-valuate

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

Time Complexity:

  • Time Complexity: O(H) where H is the height of the tree.
    • Explanation: In the worst case, the algorithm might have to traverse the height of the tree to find the smallest pearl larger than min_size. In a balanced BST, H is O(log N).

Space Complexity:

  • Space Complexity: O(1)
    • Explanation: The space usage is constant, as no additional data structures are used beyond a few pointers.
Fork me on GitHub