TIP102 Unit 9 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?
HAPPY CASE
Input:
descriptions1 = [
["Chocolate Chip", "Peanut Butter", 1],
["Chocolate Chip", "Oatmeal Raisin", 0],
["Peanut Butter", "Sugar", 1]
]
Output:
['Chocolate Chip', 'Peanut Butter', 'Oatmeal Raisin', 'Sugar']
Explanation:
The tree structure:
Chocolate Chip
/ \
Peanut Butter Oatmeal Raisin
/
Sugar
EDGE CASE
Input:
descriptions2 = [
["Ginger Snap", "Snickerdoodle", 0],
["Ginger Snap", "Shortbread", 1]
]
Output:
['Ginger Snap', 'Shortbread', 'Snickerdoodle']
Explanation:
The tree structure:
Ginger Snap
/ \
Shortbread Snickerdoodle
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 Tree Construction problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
1) Initialize a dictionary `nodes` to store TreeNode objects by their flavor.
2) Initialize a set `child_set` to keep track of all child nodes.
3) Iterate over each `description` in `descriptions`:
- If `parent` is not in `nodes`, create a new TreeNode for `parent` and add it to `nodes`.
- If `child` is not in `nodes`, create a new TreeNode for `child` and add it to `nodes`.
- Add `child` to `child_set`.
- If `is_left == 1`, set `child` as the left child of `parent`.
- If `is_left == 0`, set `child` as the right child of `parent`.
4) Identify the root node by finding the node in `nodes` that is not in `child_set`.
5) Return the root node.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def build_cookie_tree(descriptions):
nodes = {} # To store nodes by their flavor
child_set = set() # To keep track of all child nodes
# Step 1: Create nodes and establish parent-child relationships
for parent, child, is_left in descriptions:
if parent not in nodes:
nodes[parent] = TreeNode(parent)
if child not in nodes:
nodes[child] = TreeNode(child)
child_set.add(child)
if is_left == 1:
nodes[parent].left = nodes[child]
else:
nodes[parent].right = nodes[child]
# Step 2: Find the root (the one node not in the child_set)
root = None
for parent in nodes:
if parent not in child_set:
root = nodes[parent]
break
return root
# Example Usage:
descriptions1 = [
["Chocolate Chip", "Peanut Butter", 1],
["Chocolate Chip", "Oatmeal Raisin", 0],
["Peanut Butter", "Sugar", 1]
]
descriptions2 = [
["Ginger Snap", "Snickerdoodle", 0],
["Ginger Snap", "Shortbread", 1]
]
print_tree(build_cookie_tree(descriptions1))
print_tree(build_cookie_tree(descriptions2))
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`descriptions1 = [
["Chocolate Chip", "Peanut Butter", 1],
["Chocolate Chip", "Oatmeal Raisin", 0],
["Peanut Butter", "Sugar", 1]
]`
- Execution:
- Construct the tree by creating nodes and linking them according to the descriptions.
- Identify "Chocolate Chip" as the root node.
- Output:
['Chocolate Chip', 'Peanut Butter', 'Oatmeal Raisin', 'Sugar']
- Example 2:
- Input:
`descriptions2 = [
["Ginger Snap", "Snickerdoodle", 0],
["Ginger Snap", "Shortbread", 1]
]`
- Execution:
- Construct the tree by creating nodes and linking them according to the descriptions.
- Identify "Ginger Snap" as the root node.
- Output:
['Ginger Snap', 'Shortbread', 'Snickerdoodle']
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
where N
is the number of descriptions.
O(N)
where N
is the number of nodes in the tree.
N
nodes in the nodes
dictionary and N
children in the child_set
.
~~~