Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Binary Search Trees : Session 2

Caren
June 29, 2018

Binary Search Trees : Session 2

Caren

June 29, 2018
Tweet

More Decks by Caren

Other Decks in Education

Transcript

  1. 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
  2. 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 finish test
  3. 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 overflow
  4. Kth Smallest Element in Binary Tree Given a binary tree,

    write a function to find 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
  5. Kth Smallest Element in Binary Search Tree Given a binary

    search tree, write a function to find 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
  6. Kth Smallest Element in BST Given a binary search tree,

    write a function kthSmallest to find 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
  7. Kth Smallest Element in BST Given a binary search tree,

    write a function kthSmallest to find 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?
  8. Kth Smallest Element in BST Given a binary search tree,

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

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

    write a function kthSmallest to find 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;
 }
  11. Kth Smallest Element in BST Given a binary search tree,

    write a function kthSmallest to find 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);
 }
 }
  12. Kth Smallest Element in BST Given a binary search tree,

    write a function kthSmallest to find the kth smallest element in it. What if the BST is modified (we add or delete nodes) often and we need to find 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)

  13. Kth Smallest Element in BST Given a binary search tree,

    write a function kthSmallest to find the kth smallest element in it. What if the BST is modified (we add or delete nodes) often and we need to find 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 ??
  14. Kth Smallest Element in BST Given a binary search tree,

    write a function kthSmallest to find the kth smallest element in it. BST with Rank 10 5 7 1 4 3 1 1
  15. 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
  16. 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
  17. 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
  18. Mock Interviewing Use codepad for the mock interview
 https://codepad.remoteinterview.io/ Goal

    is to have working code with tests by the end of the session Feel free to move on to the other question before we make the announcement Leftover time? Try tackling a post assessment question together
  19. Mock Interviewing First of all, kudos on doing these difficult

    problems after a long day of work! Now it’ll just be so much easier when you do them when you’re not so tired. Writing test cases before starting. It’s a great way to both think through the problem and have test cases to run afterwards! 
 Omar did a great job of this! A lot of people were doing really well talking though their thought process as an interviewee and having their partner give them suggestions along the way
 Great job Rhythm, Aleksandra, Emma, Roshan, Anish, and Qiuxuan
  20. Mock Interviewing During planning time, try to come up with

    2-3 hints for your problem Before starting implementation, it’s always good to discuss about space and run time complexity. Before running you code or saying ‘okay this is my solution’ on a whiteboard, it’s always good to find your own bugs before the interviewer points them out.