Unit 8 Session 2 Standard (Click for link to problem statements)
Unit 8 Session 2 Advanced (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?
True
if the item is found, otherwise False
.HAPPY CASE
Input: grotto = TreeNode("Snarfblat", TreeNode("Gadget", TreeNode("Dinglehopper"), TreeNode("Gizmo")), TreeNode("Whatzit", None, TreeNode("Whozit"))), treasure = "Dinglehopper"
Output: True
Explanation: "Dinglehopper" is present in the tree.
EDGE CASE
Input: grotto = TreeNode("Snarfblat", TreeNode("Gadget", TreeNode("Dinglehopper"), TreeNode("Gizmo")), TreeNode("Whatzit", None, TreeNode("Whozit"))), treasure = "Thingamabob"
Output: False
Explanation: "Thingamabob" is not present in the tree.
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:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the BST, comparing the target treasure with the current node's value, and move left or right accordingly until the target is found or the tree is fully traversed.
1) Initialize `current_node` to the root of the tree (`grotto`).
2) While `current_node` is not `None`:
- If `treasure` is equal to `current_node.val`, return `True`.
- If `treasure` is less than `current_node.val`, move to the left child.
- If `treasure` is greater than `current_node.val`, move to the right child.
3) If the loop exits, return `False` as the treasure was not found in the tree.
⚠️ Common Mistakes
False
if the loop exits without finding the treasure.Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, value, left=None, right=None):
self.val = value
self.left = left
self.right = right
def locate_treasure(grotto, treasure):
current_node = grotto
while current_node:
if treasure == current_node.val:
return True
elif treasure < current_node.val:
current_node = current_node.left
else:
current_node = current_node.right
return False
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input: `grotto = TreeNode("Snarfblat", TreeNode("Gadget", TreeNode("Dinglehopper"), TreeNode("Gizmo")), TreeNode("Whatzit", None, TreeNode("Whozit")))`, `treasure = "Dinglehopper"`
- Execution:
- Start at root "Snarfblat".
- "Dinglehopper" < "Snarfblat", move to the left child "Gadget".
- "Dinglehopper" < "Gadget", move to the left child "Dinglehopper".
- Found "Dinglehopper", return `True`.
- Output: `True`
- Example 2:
- Input: `grotto = TreeNode("Snarfblat", TreeNode("Gadget", TreeNode("Dinglehopper"), TreeNode("Gizmo")), TreeNode("Whatzit", None, TreeNode("Whozit")))`, `treasure = "Thingamabob"`
- Execution:
- Start at root "Snarfblat".
- "Thingamabob" > "Snarfblat", move to the right child "Whatzit".
- "Thingamabob" < "Whatzit", left child is `None`.
- "Thingamabob" not found, return `False`.
- Output: `False`
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the tree.
O(H)
where H
is the height of the tree. For a balanced BST, H
is O(log N)
, so the search operation is O(log N)
.O(1)
because the search is performed iteratively with constant space usage.