-
Notifications
You must be signed in to change notification settings - Fork 243
Searching Ariel's Treasures
Unit 8 Session 2 Standard (Click for link to problem statements)
Unit 8 Session 2 Advanced (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10-15 mins
- 🛠️ Topics: Trees, Binary Search Trees, Search
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 an item from Ariel's collection.
- What operation needs to be performed?
- The function needs to check if a specific item is present in the BST.
- What should be returned?
- The function should return
True
if the item is found, otherwiseFalse
.
- The function should return
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:
- BST Search: Use the properties of the BST to efficiently locate the target item by navigating left or right based on comparisons.
- Iterative Search: Implement the search iteratively to avoid the overhead of recursion.
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.
- Forgetting to return
False
if the loop exits without finding the treasure. - Misinterpreting the BST properties and incorrectly traversing the tree.
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.
-
Time Complexity:
O(H)
whereH
is the height of the tree. For a balanced BST,H
isO(log N)
, so the search operation isO(log N)
. -
Space Complexity:
O(1)
because the search is performed iteratively with constant space usage.