Caren
June 29, 2018
90

Binary Search Trees : Session 2

June 29, 2018

Transcript

pairs
3. Insights from Hiring an Intern Over 100 applications in 2

weeks! Resumes  - I spent about 30 seconds - 1 minute on each one  - Any GPA less than 3.9 didn’t seem impressive  - Bolded words and terms were really helpful  - Small achievements like “Placed 1st in National Olympiad” or “Won 1st place at X hackathon” really stood out  - Don’t send generic cover letters Having a portfolio website stood out
4. Insights from Hiring an Intern Sent out ~35 HackerRank challenges

- 9 people got 100%  - Code quality mattered  well named variables  comments or well named methods  helper functions for less giant blocks of codde  unnecessary lines?  - Time took to ﬁnish test
5. Insights from Hiring an Intern Onsite   - talk through

thought process  - be able to talk about why you’re interested in the position  - think about what you want to ask when the interviewer gives you the chance Don’t  - straight up copy from stack overﬂow
6. Kth Smallest Element in Binary Tree Given a binary tree,

write a function to ﬁnd the kth smallest element in it. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 / 1 Output: 3
7. Kth Smallest Element in Binary Search Tree Given a binary

search tree, write a function to ﬁnd the kth smallest element in it. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 Output: 3
8. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. Solution 1: - do an inorder traversal - store visited notes in an array / stack - get the kth element of the array or pop from stack until we’ve found the kth element after a full traversal
9. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. Solution 1: - do an inorder traversal - store visited notes in an array / stack - get the kth element of the array or pop from stack until we’ve found the kth element after a full traversal What’s the run time and space complexity of this solution?
10. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. What if the BST is modiﬁed (we add or delete nodes) often and we need to ﬁnd the kth smallest frequently? How can we optimize our solution for this case?
11. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. What if the BST is modiﬁed (we add or delete nodes) often and we need to ﬁnd the kth smallest frequently? How can we optimize our solution for this case? What if we could modify the structure of the nodes?
12. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. Generating rank of nodes:  class TreeNodeWithRank {  int val;  int count;  TreeNodeWithCount left;  TreeNodeWithCount right;  TreeNodeWithCount(int x) {val = x; count = 1;};  } private TreeNodeWithRank buildTreeWithRank(TreeNode root) {  if (root == null) return null;  TreeNodeWithRank rootWithRank = new TreeNodeWithRank(root.val);  rootWithRank.left = buildTreeWithRank(root.left);  rootWithRank.right = buildTreeWithRank(root.right);  if (rootWithRank.left != null) rootWithRank.count += rootWithRank.left.count;  if (rootWithRank.right != null) rootWithRank.count += rootWithRank.right.count;  return rootWithRank;  }
13. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. Generating rank of nodes: private TreeNodeWithRank buildTreeWithRank(TreeNode root) {  if (root == null) return null;  TreeNodeWithRank rootWithRank = new TreeNodeWithRank(root.val);  rootWithRank.left = buildTreeWithRank(root.left);  rootWithRank.right = buildTreeWithRank(root.right);  if (rootWithRank.left != null) rootWithRank.count += rootWithRank.left.count;  if (rootWithRank.right != null) rootWithRank.count += rootWithRank.right.count;  return rootWithRank;  } private int kthSmallest(TreeNodeWithRank rootWithRank, int k) {  if (k <= 0 || k > rootWithRank.count) return -1;  if (rootWithRank.left != null) {  if (rootWithRank.left.count >= k) return kthSmallest(rootWithRank.left, k);  if (rootWithRank.left.count == k-1) return rootWithRank.val;  return kthSmallest(rootWithRank.right, k-1-rootWithRank.left.count);  } else {  if (k == 1) return rootWithRank.val;  return kthSmallest(rootWithRank.right, k-1);  }  }
14. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. What if the BST is modiﬁed (we add or delete nodes) often and we need to ﬁnd the kth smallest frequently? How can we optimize our solution for this case? What if we could modify the structure of the nodes? We can let each node track it’s “rank” (the number of nodes in its subtrees + 1)
15. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. What if the BST is modiﬁed (we add or delete nodes) often and we need to ﬁnd the kth smallest frequently? How can we optimize our solution for this case? What if we could modify the structure of the nodes? We can let each node track it’s “rank” (the number of nodes in its subtrees + 1)  Why +1 ??
16. Kth Smallest Element in BST Given a binary search tree,

write a function kthSmallest to ﬁnd the kth smallest element in it. BST with Rank 10 5 7 1 4 3 1 1
17. Mock Interviewing 15 minutes : Prep  Review the problem you’re

going to ask the other person and understand the different solutions 40 minutes : First question 40 minutes : Second question We’ll send out an announcement when  1) 15 minute prep time is over  2) Your pair should move from question 1 to question 2  3) We should all regroup and come back from the breakout room
18. Mock Interviewing Interviewee should:  Plan out solution  Implement solution  Analysis

run time and space complexity Interviewer should:  Help interviewee get unstuck  Try to think of cases where the solution might not work
19. Mock Interviewing Reminders:  When a TA pops in for a

session, we expect:  DO: Have an ongoing conversation  DO: Pseudocode and example input / outputs before implementation  DO: Practice running the code and debugging live  DO: Wrap up with a time/ space complexity analysis if you run out of time DON’T: look at the solution during the interview  DON’T: using leetcode to test the code Questions? Use the Zoom “raise your hand” feature