Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. Detailed feedback on refdash website:
  2. Coding summary (3.5/4.0):
  3. Very solid coding skills. The candidate was able to independently code 3 solutions in one hour. They paid especial attention to detail and made sure to not miss any edge case. They tested their code which is very appreciated.
  4.  
  5. During the first problem (Longest Palindromic Substring) their solution was a little bit messy but they were able to simplify it once pointed out.
  6.  
  7. Algorithm and Data Structures summary (3.6/4.0):
  8. The candidate showed an outstanding understanding of data structures and algorithms. They asked very relevant clarification questions and were able to walk through the three problems, proposing various solutions and correctly articulating their trade offs. The candidate accurately described the time and space complexity for every approach. They even went into describing how to solve the last problem using a variation of a Segment Tree data structure, called Heavy Light Decomposition. Outstanding!
  9.  
  10. Overall summary (3.5/4.0):
  11. Excellent performance overall. The candidate independently solved 3 problems in a one hour frame. Their code was mostly clean and their communication was clear and consistent through the interview.
  12.  
  13.  
  14. // Interview board starts here
  15. Martin Copes
  16. mcopes73@gmail.com
  17.  
  18. Yelp
  19. Palantir
  20.  
  21.  
  22. 10
  23. 4 5 6
  24. 1 2 7
  25.  
  26. public class SecurityTree{
  27.  
  28. void grantAccess(TreeNode n);
  29. void revokeAccess(TreeNode n);
  30. boolean hasAccess(TreeNode n);
  31. }
  32. grantAccess(10)
  33. revokeAccess(4)
  34. hasAccess(1) <-- false
  35.  
  36.  
  37. Another solution:
  38. Heavy-Light Decomposition
  39. 2->10
  40. segment tree on a path, [1, 4, 10]
  41. max query INT_MIN, 1, 2
  42. ^
  43. O(log (number of nodes) * log(path length))
  44.  
  45. initially all nodes are woth access denied.
  46.  
  47. revokeAccess(4)
  48. grantAccess(10)
  49.  
  50. int currentTime = 3
  51. bool access = 0;
  52. int updateAt;
  53. [1].access=0, updatedAt = INT_MIN
  54. [4].access=0, updatedAt = 1, currentUpdatedTime = 1
  55. [10].access=1, updatedAt = 2, updatedAt > currentUpdatedTime
  56.  
  57. grantAccess(10)
  58. revokeAccess(4)
  59.  
  60. [1].access=0, updatedAt = INT_MIN
  61. [4].access=0, updatedAt = 2
  62. [10].access=1, updatedAt = 1
  63.  
  64. grantAccess O(1)
  65. revokeAccess O(1)
  66. hasAccess O(h)
  67.  
  68. struct TreeNode {
  69. bool hasAccess = false;
  70. TreeNode *parent;
  71. int updatedAt = INT_MIN;
  72. };
  73.  
  74. int currentTime = 0;
  75.  
  76. void grantAccess(TreeNode *root) {
  77. root->hasAccess = true;
  78. updatedAt = currentTime++;
  79. }
  80.  
  81. void revokeAccess(TreeNode *root) {
  82. root->hasAccess = false;
  83. updatedAt = currentTime++;
  84. }
  85.  
  86. bool hasAccess(TreeNode *root) {
  87. int latestUpdate = INT_MIN;
  88. bool access = false;
  89. while(root!=NULL) {
  90. if(root->updatedAt > latestUpdate) {
  91. latestUpdate = root->updatedAt;
  92. access = root->updatedAt;
  93. }
  94. root = root->parent;
  95. }
  96. return access;
  97. }
  98.  
  99. Given a Binary Search Tree that contains positive integers,
  100. return the length of the longest path from root to a leaf
  101. such that the leaf is a dead end.
  102.  
  103. A leaf is considered a dead end
  104. if and only if we are not able to insert any element after that node
  105. without causing a duplicate in the tree.
  106.  
  107.  
  108. 10
  109. 8 15
  110. 9
  111.  
  112. Ans ---> 3
  113.  
  114.  
  115. 10
  116. 8 15
  117. 5 9
  118. 1
  119.  
  120. node, lowerbound, upperbound, depth
  121. 10, INT_MIN, INT_MAX, 1
  122. 8, INT_MIN, 10, 2
  123. 5, INT_MIN, 8, 3
  124. 1, INT_MIN, 5, 4
  125.  
  126. 9, 8, 10, 3 <-- max = 3
  127.  
  128. Time Complexity: O(n)
  129. Space Complexity: O(maxiumHeight)
  130.  
  131. int maximumHeight = 0;
  132.  
  133. void findLongestPathHelper(TreeNode *root, int lowerBound, upperBound, int depth) {
  134. /*
  135. 1. Check if it is a leaf, check if it's also a dead-end node
  136. 2. Traverse left
  137. 3. Traverse right
  138. */
  139. if(root == NULL) return;
  140.  
  141. if(root->left == NULL && root->right == NULL) {
  142. // this is a leaf
  143. if(upperBound-lowerBound == 2) {
  144. // this is a dead-end node
  145. maximumHeight = max(maximumHeight, depth);
  146. }
  147. return;
  148. }
  149.  
  150. findLongestPathHelper(root->left, lowerBound, root->value, depth+1);
  151. findLongestPathHelper(root->right, root->value, upperBound, depth+1);
  152. }
  153.  
  154. int findLongestPath(TreeNode *root) {
  155. maximumHeight = 0;
  156. findLongestPathHelper(root, INT_MIN, INT_MAX, 1);
  157. return maximumHeight;
  158. }
  159.  
  160. daadbc
  161.  
  162. dadbc ---> dad
  163. ^
  164. left = i, right = i+1
  165.  
  166.  
  167.  
  168. pair<int,int> expand(string &str, int left, int right) {
  169. // while str[left] == str[right], expand
  170.  
  171. while(left >= 0 && right < str.size() && str[left]==str[right]) {
  172. left--;
  173. right++;
  174. }
  175. return make_pair(left+1, right-1);
  176. }
  177.  
  178. string findLongestPalindrome(string &str) {
  179. if(str.empty()) return str;
  180. /*
  181. 1. for every index i,
  182. 2. set left and right to i // for odd length
  183. 3. or set left to i and right to i+1 // for even length
  184. 4. expand around the center
  185. 5. keep storing the max
  186. */
  187. int longetPalindrome = 0, startIndex, endIndex;
  188.  
  189. for(int i = 0; i < str.size(); i++) {
  190. // odd length
  191. int left = i, right = i;
  192. pair<int, int> palindromeBoundary = expand(str, left, right);
  193. int lengthOfPalindrome = palindromeBoundary.second - palindromeBoundary.first + 1;
  194. if(lengthOfPalindrome > longestPalindrome) {
  195. longestPalindrome = lengthOfPalindrome;
  196. startIndex = palindromeBoundary.left;
  197. endIndex = palindromeBoundary.right;
  198. }
  199.  
  200. // even length
  201. left = i, right = i+1;
  202. palindromeBoundary = expand(str, left, right);
  203. lengthOfPalindrome = palindromeBoundary.second - palindromeBoundary.first + 1;
  204. if(lengthOfPalindrome > longestPalindrome) {
  205. longestPalindrome = lengthOfPalindrome;
  206. startIndex = palindromeBoundary.left;
  207. endIndex = palindromeBoundary.right;
  208. }
  209. }
  210.  
  211. // build the palindrome string
  212. string palindrome;
  213. for(int i = startIndex; i <= endIndex; i++) palindrome += str[i];
  214. return palindrome;
  215. }
  216.  
  217. daadbc
  218. i = 0
  219. left = 0, right = 0
  220. left = -1, right = 1, left = 0, right = 0, return {0, 0}
  221. left = 0, right = 1, return {1, 0}, length = 0-1+1 == 0
  222.  
  223. i = 1
  224. left = 1, right = 1
  225. left = 0, right = 2, left = 1, right = 1 return {1, 1}, length = 1
  226.  
  227. left = 1, right = 2
  228. left = 0, right = 3,
  229. left = -1, right = 4, left = 0, right = 3, return {0, 3}, length = 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement