Unit 8 Session 2 Standard (Click for link to problem statements)
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?
min_size
.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`.
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:
min_size
.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
min_size
.min_size
.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
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`
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(H)
where H
is the height of the tree.
min_size
. In a balanced BST, H
is O(log N)
.O(1)