From 9535e57b46064346d5cb75d9651203fe861bc1ec Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 29 Jul 2015 14:03:34 -0700 Subject: [PATCH 01/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index 08ca7eb..e108c29 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -8,3 +8,39 @@ 7. 1. Segment Tree; 2. How to diagnose latency in data center; 8. Print all permutations of 3 pair of parens. ()()(), (()()), (())(),,,. etc ''' + +''' +先是问了project,涉及很具体的算法和dataset处理之类;问了hadoop;然后就是用coderpad考了一题OOD +Represent a deck of playing cards in an object-oriented design. (1) Write a function to output the deck. (2) Write a fn to shuffle the deck. +''' + + +''' +电话combination那道,算法很熟悉了,dfs,不过好长时间没做,边think aloud 边码,还算顺利 +让自己弄几个test case 跑跑,但没有一次bug free,忘了考虑0,1的case ==还有关于unsigned int 的warning +接着聊了聊怎么 code review,加一些comments啊什么的 +''' + + +''' +subsetsI and subsetsII +search in rotated array +sprial matrix + +2 Word Ladder II +以前只写过找所有path然后在dfs出最短path的解,烙印不让用,问有没有更好的方法 +。。。我建了个Graph,用BFS+mp搜,磕磕碰碰写了个大概。。。 +''' + + +''' +1,background +2,project +3,coding problem +给一个String array,找出每个元素的Anagram,按照相同的anagram分为一组,最后输出每一组元素。 +我用了HashMap>来做的,在计算key的时候,需要按字母顺序sort String作为key, +于是follow up就是怎么不用sort,于是我就说可以把每一个string统计一下a,b,c。。。的频率,然后输出一个pattern a1b3c4。 +把这个pattern作为map的key。 + +4,问当你用browser打开www.uber.com的时候,发生了生么。我就用http请求,到dns,到server,再返回,浏览器render之类的讲了一下 +''' From eb8f4b9700e65b190116662cc2ff3851350da8e8 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 29 Jul 2015 15:00:50 -0700 Subject: [PATCH 02/57] Update Letter_Combinations_of_a_Phone_Number.py --- Array/Letter_Combinations_of_a_Phone_Number.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Array/Letter_Combinations_of_a_Phone_Number.py b/Array/Letter_Combinations_of_a_Phone_Number.py index e2067cd..217c4c5 100644 --- a/Array/Letter_Combinations_of_a_Phone_Number.py +++ b/Array/Letter_Combinations_of_a_Phone_Number.py @@ -21,14 +21,15 @@ class Solution: # Iteration def letterCombinations(self, digits): - if len(digits)==0: return [] + if not digits: return [] result = [''] # Note: here we should use [''] not empty [] for digit in digits: ret = [] for comb in result: for char in Solution.dict[digit]: ret.append(comb+char) - result = ret + if ret: # Note: add ret here, we need to consider the case "12" + result = ret return result # Recursion 1 From 5764d29e3554187265d6e4f6de7fc7956b999fbd Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 29 Jul 2015 15:09:54 -0700 Subject: [PATCH 03/57] Update Letter_Combinations_of_a_Phone_Number.py --- .../Letter_Combinations_of_a_Phone_Number.py | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/Array/Letter_Combinations_of_a_Phone_Number.py b/Array/Letter_Combinations_of_a_Phone_Number.py index 217c4c5..835fcec 100644 --- a/Array/Letter_Combinations_of_a_Phone_Number.py +++ b/Array/Letter_Combinations_of_a_Phone_Number.py @@ -9,7 +9,8 @@ class Solution: # @return a list of strings, [s1, s2] - dict = {'1':'', + dict = {'0':'', + '1':'', '2':'abc', '3':'def', '4':'ghi', @@ -32,33 +33,22 @@ def letterCombinations(self, digits): result = ret return result - # Recursion 1 - def letterCombinations_1(self, digits): - if len(digits)==0: return [] - result = [] - self.combination_rec(digits,0,result,'') - return result - - def combination_rec(self,digits,i,result,ret): - if i == len(digits): #注意这里一定要check i == len(digits), check the following recursion solution ! - result.append(ret) - return - for char in Solution.dict[digits[i]]: # Here we do not need to for loop the digits !!! - self.combination_rec(digits,i+1,result,ret+char) - # Recursion 2 + # Recursion 1 def letterCombinations(self, digits): if not digits: return [] result = [] length = len(digits) - self.letter_com_helper(digits, result, '',length) + self.letter_com_helper(digits, result, '', length) return result def letter_com_helper(self, digits, result, substr,length): - if len(digits) == 0 and len(substr) == length: # 这里如果我们传递digits[i+1:] 还需要check len(substr) == length + if len(substr) == length: result.append(substr); return for i, digit in enumerate(digits): + if digit == "1" or digit == '0': # take care the special '1' and '0' case + length -= 1 for char in Solution.dict[digit]: self.letter_com_helper(digits[i+1:], result, substr + char,length) @@ -67,7 +57,21 @@ def letter_com_helper(self, digits, result, substr,length): # Output: ["aa","ab","ac","ba","bb","bc","ca","cb","cc","a","b","c"] # Expected: ["aa","ab","ac","ba","bb","bc","ca","cb","cc"] - # Recursion 2 + # Recursion 2 + def letterCombinations_1(self, digits): + if len(digits)==0: return [] + result = [] + self.combination_rec(digits,0,result,'') + return result + + def combination_rec(self,digits,i,result,ret): + if i == len(digits): #注意这里一定要check i == len(digits), check the following recursion solution ! + result.append(ret) + return + for char in Solution.dict[digits[i]]: # Here we do not need to for loop the digits !!! + self.combination_rec(digits,i+1,result,ret+char) + + # Recursion 3 def letterCombinations_2(self, digits): if digits == "": return [""] From 3144439b8586cc2588e385bdba26ba517d80e226 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 29 Jul 2015 19:30:37 -0700 Subject: [PATCH 04/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index e108c29..efafbdb 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -21,6 +21,55 @@ 接着聊了聊怎么 code review,加一些comments啊什么的 ''' +class Solution: + # @return a list of strings, [s1, s2] + dict = {'0':'', + '1':'', + '2':'abc', + '3':'def', + '4':'ghi', + '5':'jkl', + '6':'mno', + '7':'pqrs', + '8':'tuv', + '9':'wxyz'} + + # Iteration + def letterCombinations(self, digits): + if not digits: return [] + result = [''] # Note: here we should use [''] not empty [] + for digit in digits: + ret = [] + for comb in result: + for char in Solution.dict[digit]: + ret.append(comb+char) + if ret: # Note: add ret here, we need to consider the case "12" + result = ret + return result + + + # Recursion + def letterCombinations(self, digits): + if not digits: return [] + result = [] + length = len(digits) + self.letter_com_helper(digits, result, '', length) + return result + + def letter_com_helper(self, digits, result, substr,length): + if len(substr) == length: + result.append(substr); return + + for i, digit in enumerate(digits): + if digit == "1" or digit == '0': # take care the special '1' and '0' case + length -= 1 + for char in Solution.dict[digit]: + self.letter_com_helper(digits[i+1:], result, substr + char,length) + + # test case: + # Input: "22" + # Output: ["aa","ab","ac","ba","bb","bc","ca","cb","cc","a","b","c"] + # Expected: ["aa","ab","ac","ba","bb","bc","ca","cb","cc"] ''' subsetsI and subsetsII From 9dcad60969ffad75239e354035b4e110e065ea07 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 30 Jul 2015 16:29:29 -0700 Subject: [PATCH 05/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index efafbdb..9ebd1bf 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -93,3 +93,122 @@ def letter_com_helper(self, digits, result, substr,length): 4,问当你用browser打开www.uber.com的时候,发生了生么。我就用http请求,到dns,到server,再返回,浏览器render之类的讲了一下 ''' + +''' +Phone interview +''' + +# given a list of filenames, each file has been sorted +# merge into a big file + +from random import randint +import heapq + +class MyFile(object): + def __init__(self, n): + self.lines = [] + self.i = 0 + self.length = 3 + r = 0 + for i in xrange(n): + pass + # r = randint(r, r + 100) + # self.lines.append(str(r)) + self.lines.append('abc' + str(randint(1,200))) + self.lines.append('efg' + str(randint(1,200))) + self.lines.append('xyz' + str(randint(1,200))) + + # sort by alphabetical order + # compare 1st letter + # if 1st letter is same,compare 2nd, etc. + # content of file: + # acd + # abc + # xyz + # xyy + # sorted: + # abc + # acd + # xyy, + # xyz + # sorted(str, cmp = lambda x: ) + + def nextline(self): + if self.i < self.length: + self.i += 1 + return self.lines[self.i - 1] + else: + return None + +def merge_karray(files): + ''' + @ input files list: [file1, file2, file3...] + @ output: + ''' + if not files: + return None + heap = [] + result = [] + k = len(files) + for i in xrange(k): + ele = files[i].nextline() + if ele: + heap.append((MyLine(ele), i)) + heapq.heapify(heap) + #print "heap: ", heap + + while heap: + element = heapq.heappop(heap) + value, file_index = element[0], element[1] + result.append(value.string) + new_value = files[file_index].nextline() + if new_value: + heapq.heappush(heap, (MyLine(new_value), file_index)) + return result + + +# "abc", "abcd" +class MyLine(object): + def __init__(self, string): + self.string = string + + def __cmp__(self, other): + for i in xrange(min(len(self.string), len(other.string))): + if self.string[i] > other.string[i]: + return 1 + elif self.string[i] < other.string[i]: + return -1 + else: + continue + if len(self.string) > len(other.string): + return 1 + elif len(self.string) < len(other.string): + return -1 + else: + return 0 + # comapre between 2 strings + + + +if __name__ == '__main__': + f1 = MyFile(10) + f2 = MyFile(20) + f3 = MyFile(15) + k = 10 + files = [] + for i in xrange(k): + files.append(MyFile(3)) + str1 = "abcd" + str2 = "abcf" + # test = MyLine(str1) + # __cmp__ + #print MyLine("abcz") < MyLine("abcd") + + # files +# for f in files: +# print '-'*20 +# l = f.nextline() +# while l: +# print l +# l = f.nextline() + print merge_karray(files) From b5f6c6e641149256835b41d257db1eb2402a249b Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 30 Jul 2015 19:03:38 -0700 Subject: [PATCH 06/57] Update Min_Heap_Implement.md --- Algorithm/Min_Heap_Implement.md | 180 ++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) diff --git a/Algorithm/Min_Heap_Implement.md b/Algorithm/Min_Heap_Implement.md index d44b9e5..946ecd4 100644 --- a/Algorithm/Min_Heap_Implement.md +++ b/Algorithm/Min_Heap_Implement.md @@ -114,6 +114,186 @@ heapified : ------------------------------------ ``` + +#### Heap use ```__cmp__``` +##### How could we sort the string "abc" and "adef" (which are sorted in each string)? + +```python +import random +from random import randint +import heapq + +class MyLine(object): + def __init__(self, string): + self.string = string + + def __cmp__(self, other): + for i in xrange(min(len(self.string), len(other.string))): + if self.string[i] > other.string[i]: + return 1 + elif self.string[i] < other.string[i]: + return -1 + else: + continue + if len(self.string) > len(other.string): + return 1 + elif len(self.string) < len(other.string): + return -1 + else: + return 0 + +def merge_karray(files): + ''' + @ input files list: [file1, file2, file3...] + @ output: + ''' + if not files: return None + heap = [] + result = [] + k = len(files) + for i in xrange(k): + ele = files[i] + if ele: + heap.append((MyLine(ele), i)) + heapq.heapify(heap) + + while heap: + element = heapq.heappop(heap) + value, file_index = element[0], element[1] + result.append(value.string) + return result + +if __name__ == '__main__': + lines = [] + for i in xrange(5): + lines.append('abc' + str(randint(1,200))) + lines.append('efg' + str(randint(1,200))) + lines.append('xyz' + str(randint(1,200))) + print merge_karray(lines) + +``` + +##### Uber phone interview +``` +# given a list of filenames, each file has been sorted +# merge into a big file + +from random import randint +import heapq + +class MyFile(object): + def __init__(self, n): + self.lines = [] + self.i = 0 + self.length = 3 + r = 0 + for i in xrange(n): + pass + # r = randint(r, r + 100) + # self.lines.append(str(r)) + self.lines.append('abc' + str(randint(1,200))) + self.lines.append('efg' + str(randint(1,200))) + self.lines.append('xyz' + str(randint(1,200))) + + # sort by alphabetical order + # compare 1st letter + # if 1st letter is same,compare 2nd, etc. + # content of file: + # acd + # abc + # xyz + # xyy + # sorted: + # abc + # acd + # xyy, + # xyz + # sorted(str, cmp = lambda x: ) + + def nextline(self): + if self.i < self.length: + self.i += 1 + return self.lines[self.i - 1] + else: + return None + +def merge_karray(files): + ''' + @ input files list: [file1, file2, file3...] + @ output: + ''' + if not files: + return None + heap = [] + result = [] + k = len(files) + for i in xrange(k): + ele = files[i].nextline() + if ele: + heap.append((MyLine(ele), i)) + heapq.heapify(heap) + #print "heap: ", heap + + while heap: + element = heapq.heappop(heap) + value, file_index = element[0], element[1] + result.append(value.string) + new_value = files[file_index].nextline() + if new_value: + heapq.heappush(heap, (MyLine(new_value), file_index)) + return result + + +# "abc", "abcd" +class MyLine(object): + def __init__(self, string): + self.string = string + + def __cmp__(self, other): + for i in xrange(min(len(self.string), len(other.string))): + if self.string[i] > other.string[i]: + return 1 + elif self.string[i] < other.string[i]: + return -1 + else: + continue + if len(self.string) > len(other.string): + return 1 + elif len(self.string) < len(other.string): + return -1 + else: + return 0 + # comapre between 2 strings + + + +if __name__ == '__main__': + f1 = MyFile(10) + f2 = MyFile(20) + f3 = MyFile(15) + k = 10 + files = [] + for i in xrange(k): + files.append(MyFile(3)) + str1 = "abcd" + str2 = "abcf" + # test = MyLine(str1) + # __cmp__ + #print MyLine("abcz") < MyLine("abcd") + + # files +# for f in files: +# print '-'*20 +# l = f.nextline() +# while l: +# print l +# l = f.nextline() + print merge_karray(files) + + +``` + + * [Merge K sorted linked list](https://github.com/UmassJin/Leetcode/blob/master/Array/Merge_K_Sorted_Lists.py) * [Merge K sorted array](https://github.com/UmassJin/Leetcode/blob/master/Experience/Merge_k_sorted_array.md) * [Good Video about how to make max heap](https://www.youtube.com/watch?v=WsNQuCa_-PU) From 5e1d68076e39b3b25611a5732c036a9ee5749c36 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 30 Jul 2015 19:03:56 -0700 Subject: [PATCH 07/57] Update Min_Heap_Implement.md --- Algorithm/Min_Heap_Implement.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithm/Min_Heap_Implement.md b/Algorithm/Min_Heap_Implement.md index 946ecd4..0ab8194 100644 --- a/Algorithm/Min_Heap_Implement.md +++ b/Algorithm/Min_Heap_Implement.md @@ -174,7 +174,7 @@ if __name__ == '__main__': ``` ##### Uber phone interview -``` +```python # given a list of filenames, each file has been sorted # merge into a big file From fe49f5d7da3e9dc7e7a617b928642dd945a691ea Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 30 Jul 2015 19:04:12 -0700 Subject: [PATCH 08/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" | 3 --- 1 file changed, 3 deletions(-) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index 9ebd1bf..7e5cfe7 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -94,9 +94,6 @@ def letter_com_helper(self, digits, result, substr,length): 4,问当你用browser打开www.uber.com的时候,发生了生么。我就用http请求,到dns,到server,再返回,浏览器render之类的讲了一下 ''' -''' -Phone interview -''' # given a list of filenames, each file has been sorted # merge into a big file From 09e31693561ad122248ce1135f8c2be7803c8bcb Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 30 Jul 2015 19:05:37 -0700 Subject: [PATCH 09/57] Update Min_Heap_Implement.md --- Algorithm/Min_Heap_Implement.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Algorithm/Min_Heap_Implement.md b/Algorithm/Min_Heap_Implement.md index 0ab8194..d9e6c24 100644 --- a/Algorithm/Min_Heap_Implement.md +++ b/Algorithm/Min_Heap_Implement.md @@ -299,3 +299,4 @@ if __name__ == '__main__': * [Good Video about how to make max heap](https://www.youtube.com/watch?v=WsNQuCa_-PU) * [Median in stream data](https://github.com/UmassJin/Leetcode/blob/master/Experience/Median_in_stream_data.py) * [Find K closet points](https://github.com/UmassJin/Leetcode/blob/master/Experience/K_closet_points.py) +* [Python __cmp__ compare](http://stackoverflow.com/questions/11989178/how-to-heapify-by-field-from-custom-objects) From dd3789eaea26028e897bf59693bed3d439c1ad8f Mon Sep 17 00:00:00 2001 From: UmassJin Date: Fri, 31 Jul 2015 11:10:02 -0700 Subject: [PATCH 10/57] Update Todo_list.md --- Todo_list.md | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/Todo_list.md b/Todo_list.md index c62fbe1..8e12f44 100644 --- a/Todo_list.md +++ b/Todo_list.md @@ -21,30 +21,10 @@ * [x] project in the resume - Good Blog to read: * Binary search: http://fihopzz.blogspot.com/2013/07/enter-post-title-here-binary-search-and.html * CC150 中文: http://www.hawstein.com/posts/ctci-solutions-contents.html * 系统面试设计: http://dongxicheng.org/search-engine/system-designing-in-finging-jobs/ * 我的面试总结: http://www.mitbbs.co.nz/article_t/JobHunting/32564237.html#top -, - -Prepare the Google Interview -* Algorithm - * [tech:interview](http://www.fgdsb.com/) & Leetcode - * 几何题: - * [topcoder](http://help.topcoder.com/data-science/competing-in-algorithm-challenges/algorithm-tutorials/) - * [Algorithm Video](https://www.youtube.com/watch?v=Igr6yONkpIQ) - * G家喜欢考各种tree:prefix tree,augmented binary search tree (with rank and select APIs), segment tree,binary index tree (1D and 2D), interval tree, kd tree, quad tree - * Graph: DFS, BFS, A* - * DP - -* System Design - * G家关于设计棋类游戏的AI的题,基本上都可以用MinMax 算法解决: http://neverstopbuilding.com/minimax - * OOD: 设计电梯,设计停车场 - * Distributed System, Clouding Computing - * 三驾马车: GFS, MapReduce, BigTable -* Operating System - * Thread-Safe data structure和 Threading Pool,推荐阅读C ++ concurrency in action的第六章和第九章 http://www.manning.com/williams/ From ed72a0062f9d13840056a211d0eec0f4b75ea150 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Fri, 31 Jul 2015 12:19:39 -0700 Subject: [PATCH 11/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...r\351\235\242\347\273\217\351\233\206\345\220\210.py" | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index 7e5cfe7..ba7c2c8 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -1,7 +1,8 @@ ''' 1. Given a list of words, find whether a new word is anagram of word in list 2. Implement an LRU cache. -3. Using Object Oriented Design principles, design a method to check if a Sudoku board is valid (skeleton code was provided which was initially passed in through a 2-d array). +3. Using Object Oriented Design principles, design a method to check if a Sudoku board is valid +(skeleton code was provided which was initially passed in through a 2-d array). 4. Lots of question related to hash table. 5. Implement boggle 6. Implement LRU cache with get and set operations in constant time O(1). @@ -10,8 +11,10 @@ ''' ''' -先是问了project,涉及很具体的算法和dataset处理之类;问了hadoop;然后就是用coderpad考了一题OOD -Represent a deck of playing cards in an object-oriented design. (1) Write a function to output the deck. (2) Write a fn to shuffle the deck. +先是问了project,涉及很具体的算法和dataset处理之类;问了hadoop; +然后就是用coderpad考了一题OOD +Represent a deck of playing cards in an object-oriented design. +(1) Write a function to output the deck. (2) Write a fn to shuffle the deck. ''' From 4f2cba91caea3a3eb05468db6030a6322a9e62da Mon Sep 17 00:00:00 2001 From: UmassJin Date: Fri, 31 Jul 2015 17:27:39 -0700 Subject: [PATCH 12/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 128 +++++++++++++++--- 1 file changed, 112 insertions(+), 16 deletions(-) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index ba7c2c8..fc7542c 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -8,18 +8,7 @@ 6. Implement LRU cache with get and set operations in constant time O(1). 7. 1. Segment Tree; 2. How to diagnose latency in data center; 8. Print all permutations of 3 pair of parens. ()()(), (()()), (())(),,,. etc -''' - -''' -先是问了project,涉及很具体的算法和dataset处理之类;问了hadoop; -然后就是用coderpad考了一题OOD -Represent a deck of playing cards in an object-oriented design. -(1) Write a function to output the deck. (2) Write a fn to shuffle the deck. -''' - - -''' -电话combination那道,算法很熟悉了,dfs,不过好长时间没做,边think aloud 边码,还算顺利 +9. 电话combination那道,算法很熟悉了,dfs,不过好长时间没做,边think aloud 边码,还算顺利 让自己弄几个test case 跑跑,但没有一次bug free,忘了考虑0,1的case ==还有关于unsigned int 的warning 接着聊了聊怎么 code review,加一些comments啊什么的 ''' @@ -75,13 +64,20 @@ def letter_com_helper(self, digits, result, substr,length): # Expected: ["aa","ab","ac","ba","bb","bc","ca","cb","cc"] ''' -subsetsI and subsetsII -search in rotated array -sprial matrix +10. subsetsI and subsetsII +11. search in rotated array +12. sprial matrix -2 Word Ladder II +13. +Word Ladder II 以前只写过找所有path然后在dfs出最短path的解,烙印不让用,问有没有更好的方法 。。。我建了个Graph,用BFS+mp搜,磕磕碰碰写了个大概。。。 + +14. +给一个tuple list, 按weight random select. 比如给 [(“hello”, 2.34), (“world”, 4.68)…] world出现的概率是hello的两倍 +第二题, reverse string的变种。 只reverse word不reverse punctuation。比如 “this,,,is.a word” -> “word,,,a.is this” + + ''' @@ -97,6 +93,102 @@ def letter_com_helper(self, digits, result, substr,length): 4,问当你用browser打开www.uber.com的时候,发生了生么。我就用http请求,到dns,到server,再返回,浏览器render之类的讲了一下 ''' +---------------------------------------------- + +''' +System Design: + +1. 第三轮:一个漂亮国人妹纸加一个白男。 systemdeisgn,给两个题目,让我选一个,一个是designnetflix另一个是design uber, +我就直接选 了designuber。我跟面thumbtack一样先用了最经典 的3tier 架构大法,然后一点一点改进。 +system design问的很细,问mobile app怎么跟backend通信,问通信是哪些protocal, 传输的信息是什么, +哪里加cache,distributed db怎么弄之类。基本我答的还算顺利。感觉面试官挺满意这轮的。 + +2. 设计excel, 先实现基本功能再优化(比如存图片怎么弄) + +3. 先是问了project,涉及很具体的算法和dataset处理之类;问了hadoop; +然后就是用coderpad考了一题OOD +Represent a deck of playing cards in an object-oriented design. +(1) Write a function to output the deck. (2) Write a fn to shuffle the deck. + +4. +design一个uber eat。说是system design结果我感觉更像product design + +5. +之前也看了好多uber的面经, 本以为会考算法 leetcode, 结果却考了一个ood的题目 + +直接上原题 +Develop an API Rate-limit Throttling Client +要求写一个api, 请求第三方api, 如果一秒内的请求太多, 自己的api就直接忽略掉。 +面试小哥给了个框架 +import time +import datetime + +class GoogleMapsClient(object): +“””3rd party maps client; we CANT EDIT THIS.””” + +def __init__(self): +self.requests_made = 0 + + + +def make_request(self): +self.requests_made += 1 +now = datetime.datetime.now().time() +return “%d – %s – San Francisco” % (self.requests_made, now) + +刚开始对python time的单位不熟悉,有个bug。后来改了 +class MyRequest(object): + +def __init__(self): +self.client = GoogleMapsClient() +self.time = time.time() +self.request_times = 0 + +def make_request(self): + +if (time.time()-self.time)>1: +self.time = time.time() +self.request_times = 0 +else: +self.request_times += 1 +if self.request_times>=10: +time.sleep(1) +return self.client.make_request() + + + +Follow up 是如何让myclient 继续接受request。 可能我也没听明白题意, 小哥直接说time.sleep(1). + +面试题目就这一个, 开始还有介绍自己和简历。求水果啊。。 + + +6. 电面是一个美国小哥,开始聊了聊简历之后给了一道系统设计题,设计一个Service可以输入用户location查询附近的 +公交站台和所有即将到这些站台的公交车。我随便和他扯了一些系统设计的还有优化算法之类的东西,后来让我写一个 +控制访问API频率(Ratelimit)的function, 用了一个Queue写完就结束了。 + +7. 让我设计一个 Netflix, follow up 很多 比如如何限制同一个用户多处登录不能同时看一个资源, +如何实现根据用户的网速调整清晰度,怎么热门推荐等等。 + +8. 第二个人 : 进来直接不闲聊直接让我打开自己电脑开始写一些代码,设计一个 Excel , +每个cell里面是一个String。 一开始想当然说可以直接用二维矩阵存,后来改成list of lists, +最后改成了HashMap。后续还有evaluate一个string相关的问题(给了黑盒evaluate函数, +对每个cell实现evaluate和支持修改)。 + +9. 第三个人 : 纯聊简历,干聊project,面试官没有准备一道题,到最后我就已经是在找话说了。 + + +''' + +----------------------------------------- + +''' +culture fit: +她让我说了下我现在的proj,具体问了proj怎么设计的,为什么,有哪些可以值得改进的。然后问些behavior, +比如你之前 有没有projfail过,为什么之类的。最后问了我whyuber, where’s ur passion之类的。 +''' + + + # given a list of filenames, each file has been sorted # merge into a big file @@ -212,3 +304,7 @@ def __cmp__(self, other): # print l # l = f.nextline() print merge_karray(files) + + + + From aa54bf7431b072398b7a7d90404815d43f2b2d94 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Mon, 3 Aug 2015 18:24:48 -0700 Subject: [PATCH 13/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ber\351\235\242\347\273\217\351\233\206\345\220\210.py" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index fc7542c..5676d5a 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -11,6 +11,13 @@ 9. 电话combination那道,算法很熟悉了,dfs,不过好长时间没做,边think aloud 边码,还算顺利 让自己弄几个test case 跑跑,但没有一次bug free,忘了考虑0,1的case ==还有关于unsigned int 的warning 接着聊了聊怎么 code review,加一些comments啊什么的 + +10. 面试四轮,第一轮是个韩裔小哥,两道题,happy number和anagram。 +第二轮白人,换硬币那题。 +第三轮ABI经理,问了一大顿behavioural question,最后剩10分钟问了八皇后。 +第四轮白人,设计扫雷的游戏,实现其中点一个格子,返回结果(要么失败,要么一片没有雷的区域,list of JSON object)。 +其中第二轮第四轮都是要求现场编译运行的。我用python所以相对比较轻松一点。 + ''' class Solution: From e8e20dc6fd5782db8ca4526521ea36429a40c27c Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 4 Aug 2015 11:28:02 -0700 Subject: [PATCH 14/57] Update Design_Spreadsheet_OOD.md --- Design/OS_concepts/Design_Spreadsheet_OOD.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Design/OS_concepts/Design_Spreadsheet_OOD.md b/Design/OS_concepts/Design_Spreadsheet_OOD.md index abc6972..1940898 100644 --- a/Design/OS_concepts/Design_Spreadsheet_OOD.md +++ b/Design/OS_concepts/Design_Spreadsheet_OOD.md @@ -172,7 +172,7 @@ to do ripple calculation. # mMap structure: # {x1 : {y1 : Cell1}, x2: {y2: Cell2}} -class Sheet: +class Sheet(object): def __init__(self): self.mMap = dict() @@ -197,7 +197,7 @@ class Sheet: else: return None -class Cell: +class Cell(object): def __init__(self, sheet, x, y, value): self.mSheet = sheet self.mPosx = x @@ -209,7 +209,7 @@ class Cell: try: except: -class ExpressionCell(self, sheet, x, y, expression_str): +class ExpressionCell(object): def __init__(self, sheet, x, y, expression_str): self.mSheet = sheet self.mPosx = x From 03df3fa70d0ded3223ab7850d8408cd713814faa Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 4 Aug 2015 11:54:38 -0700 Subject: [PATCH 15/57] Update Design_Spreadsheet_OOD.md --- Design/OS_concepts/Design_Spreadsheet_OOD.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Design/OS_concepts/Design_Spreadsheet_OOD.md b/Design/OS_concepts/Design_Spreadsheet_OOD.md index 1940898..99b3de2 100644 --- a/Design/OS_concepts/Design_Spreadsheet_OOD.md +++ b/Design/OS_concepts/Design_Spreadsheet_OOD.md @@ -207,16 +207,20 @@ class Cell(object): def getDoubleValue(self ): # if input is not integer, will return error try: - except: + +# For example: change the cell (2,3) to the new cell +# old expression in cell(2,3) is (1,1) + (2,2) * (3,3) +# new expression in cell(2,3) is (2,5) + (6,4) +# other cells that depend on (2,3) is (5,6) and (7,8) class ExpressionCell(object): def __init__(self, sheet, x, y, expression_str): self.mSheet = sheet self.mPosx = x self.mPosy = y - self.mExpression = Parser.parse(s, expression_str) - # mInEdges 表示dependent是什么,如果例子中是(0,1) - # mOutEdges 表示这个cell影响谁 + self.mExpression = Parser.parse(sheet, expression_str) + # mInEdges 表示dependent是什么,如果例子中是(1,1),(2,2),(3,3) + # mOutEdges 表示这个cell影响谁 self.mInEdges = OutEdgeExtractor.extract(mExpression) self.updateCurrentValue() From 30cdac66037f7fde3dabb810fb9901888168f807 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 4 Aug 2015 15:54:03 -0700 Subject: [PATCH 16/57] Create Design_elevator.md --- Design/OS_concepts/Design_elevator.md | 102 ++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Design/OS_concepts/Design_elevator.md diff --git a/Design/OS_concepts/Design_elevator.md b/Design/OS_concepts/Design_elevator.md new file mode 100644 index 0000000..88fd89a --- /dev/null +++ b/Design/OS_concepts/Design_elevator.md @@ -0,0 +1,102 @@ +[转](http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=139134&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26sortid%3D311) +我感觉对于OOP设计问题,关键要和面试官进行讨论,弄清情景situation ,who will use it, +在这个situation都是有哪些对象,他们在干什么,他们之间是什么关系,然后一个对象一个对象的 +去分析这个对象应有的属性,和行为。多和面试官讨论,越细致越好!如果可以使用什么单例, +或者factory method 来设计的话,multi threading, 就尽量的添加上这些东西。 + +elevator:. +First ask the interviewer what kind of elevator? there is only one elevator serving that +building or multiple elevators serving the building simultaneously? +this situation is that: there is one elevator serving the building. there are many floors +in the buliding. Maybe there are some users in different floor pressing the button simultaneously. +This results in some requests to RequestProcessCenter for processing. The RequestProcessCenter +figure out the first request that need to be processed in such an algorithm that the distance +between target floor and current floor is shortest. +First describe the whole situation. and check it with your interviewer; +Second sketch out the main classes and methods on the whiteboard; +So we need the following classes: +public class User { +private name; +public pressButton(int toFloor) { + Request req = new Request( toFloor); + RequestProcessCenter center = RequestProcessCenter.getInstance(); + center.addRequest(req); +} +} +public class Request { + private int toFloor; + public Request(int _toFloor) { + toFloor = _toFloor; +}. from: +public getToFloor() { + return toFloor; +} +} +public class Elevator { + public static Elevator instance = null; + private int currentFloor; + public static Elevator( ) { + if (instance == null) { // late loading and eager loading + // connection pool-google 1point3acres + synchronized (Elevator.class) { + instance = new Elevator();. +} +} +return instance; +} +public getInstance() { + if (instance == null) { + synchronized (SingletonDemo.class) { + instance = new Elevator(); +} +} +return instance; +} +public getCurrentFloor() { + return currentFloor; +} +public moveToTargetFloor(int toFloor) { + currentFloor = toFloor; +} +public void moveUp();. +public void moveDown(); +} +public RequestProcessCenter implements runnable { + public LinkedList queue; +public RequestProcessCenter( ) { + queue = new LinkedList( ); +} +public void run() { + while ( true ) { + processRequest( ). +} +} +public void addRequest(Request request) { + queue.add(request); +}. +public void removeRequest(Request request) {. + queue.remove(request); +} +public Request getNextRequest( ) { + Request shortestReq = null; + int shortest = Integer.MAX_VALUE; + int curFloor = Elevator.getInstance( ).getCurrentFloor( );. from: 1point3acres.com/bbs + for (Request item : queue) { + int distance = Math.abs(curFloor - item.getToFloor( ) ); + if (distance < shortest) { + shortest = distance; + shortestReq = item; +} +} +return shortestReq; +} +public void processRequest( ) {. + Request req = getNextRequest( ); +if (req != null) { + int toFloor = req.getToFloor( );. + Elevator.getInstance.moveToTargetFloor( toFloor); + queue.remove(req); +}. + +} +} From c85e4ed27eccd6754d4c421983e3f932763b1cfd Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 4 Aug 2015 15:54:19 -0700 Subject: [PATCH 17/57] Rename Design_elevator.md to Design_Elevator.md --- Design/OS_concepts/{Design_elevator.md => Design_Elevator.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Design/OS_concepts/{Design_elevator.md => Design_Elevator.md} (100%) diff --git a/Design/OS_concepts/Design_elevator.md b/Design/OS_concepts/Design_Elevator.md similarity index 100% rename from Design/OS_concepts/Design_elevator.md rename to Design/OS_concepts/Design_Elevator.md From 9d359c5455ff6b5fa1a4bec9ddfd2311ece5aea2 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 4 Aug 2015 19:27:50 -0700 Subject: [PATCH 18/57] Update knowledge.md --- knowledge.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/knowledge.md b/knowledge.md index 17e2e4c..dc0d08f 100644 --- a/knowledge.md +++ b/knowledge.md @@ -420,6 +420,8 @@ Content-Encoding:gzip * They use the "code" on a Secure Sockets Layer (SSL), sometimes called Transport Layer Security (TLS) to send the information back and forth +#### [Conditional GET Request](https://ruturajv.wordpress.com/2005/12/27/conditional-get-request/) +#### [HTTP Made Really Easy](http://www.jmarshall.com/easy/http/) ## DNS #### [How DNS works](http://www.verisigninc.com/en_US/domain-names/online/how-dns-works/index.xhtml) From 84d801c6ea6641338dd17a52298ffcd4a9aaa4b0 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 4 Aug 2015 19:28:56 -0700 Subject: [PATCH 19/57] Update knowledge.md --- knowledge.md | 1 + 1 file changed, 1 insertion(+) diff --git a/knowledge.md b/knowledge.md index dc0d08f..79db291 100644 --- a/knowledge.md +++ b/knowledge.md @@ -422,6 +422,7 @@ Content-Encoding:gzip #### [Conditional GET Request](https://ruturajv.wordpress.com/2005/12/27/conditional-get-request/) #### [HTTP Made Really Easy](http://www.jmarshall.com/easy/http/) +#### [Facebook Caching Exercise](https://code.facebook.com/posts/964122680272229/web-performance-cache-efficiency-exercise/) ## DNS #### [How DNS works](http://www.verisigninc.com/en_US/domain-names/online/how-dns-works/index.xhtml) From 7dc0750f065cf8b330f222d0d23c405d3a240c23 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 5 Aug 2015 16:24:55 -0700 Subject: [PATCH 20/57] Update about.md --- about.md | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 5 deletions(-) diff --git a/about.md b/about.md index f3411b7..bb8bbda 100644 --- a/about.md +++ b/about.md @@ -11,7 +11,7 @@ b) Alumni Network ####2. 跳槽目的: 1. I have learned a lot and enjoyed working with currect team recently, but I have joined the Cisco for almost three years and participate in design and develop the 2 generation productions in Nexus 7k, right now, we almost focus on the maintaince of the productions, so I want to try the differnt opportunity outside the Cisco. 2. I want to build my experience in a new industry, I’m interested in working at [name of employer] based on the great things I have learned about it. -3. Instead of the network area, I want to learn the distributed systems technology, how to build the large systems...facebook is the leader in this industry, so working here would mean that I would be at the top of my field. +3. Instead of the network area, I want to learn the distributed systems technology, how to build the large systems...*** is the leader in this industry, so working here would mean that I would be at the top of my field. 4. I really believe that the most important thing for any job is to make sure that you’re learning a lot. Whereas at many companies you really learn only about your own team, at Facebook/Google, employees seem to be encouraged to transfer teams, to share knowledge across teams, to do tech talks about their team’s architecture, I feel confident that I can leverage my academic, professional, and “extracurricular” experience with @@ -38,9 +38,112 @@ software development to make an impact on Google. 3. Multi Tenancy is a critical technology in cloud computing. One instance of application can serve multiple customers by sharing the resources. Important technology for N7K to move towards virtualization. Implemented on Vinci architecture with Spine-Leaf topology 4. Vinci provides a next-generation data center architecture that is built on a spine-leaf topology that provides optimal connectivity at Layer-2 and Layer-3 between hosts at the leaf switches in the fabric and between the hosts and the external network. The fabric essentially provides an overlay network that allows the spine nodes to act as transit nodes that only switch on the fabric overlay header. -##### Flanker - - +##### Blogger +1. Based on the Pyramid framework + * Pyramid uses the WSGI protocol to connect an application and a web server together. + * WSGI: Web Server Gateway Interface. This is a Python standard for connecting web applications to web servers, + similar to the concept of Java Servlets. Pyramid requires that your application be served as a WSGI application. + * Pyramid uses the WebOb package as a basis for its request and response object implementations. + * One of the primary jobs of Pyramid is to find and invoke a view callable when a request reaches your application. + View callables are bits of code which do something interesting in response to a request made to your application. + +2. template: + * Pyramid provides templating via the Chameleon and Mako templating libraries, I use the Mako as template in the frontend for HTML, css and javascript. + * Use 960 Grid system: The 960 Grid System is an effort to streamline web development workflow by providing commonly used dimensions + +3. Process: +1). Extract the input file, create the unique random number("randomID") for each session, + create a dictionary for each session to save information of each file,such as filename, + extract_file_path, unique file_ID and time duration of session... + return the extract result to extract_root.mak + +2). check the input file path by user, if the file path does not exists, or user enter + the blank file path, or iParse can not access the path, raise the exception + otherwise, process the user input path. + +3). Create the randomID for each session. Initialize the Tree() and corresponding + dictionary. Open a debug file and record the user input file path, randomID, + file_path after saved in the /blogger/iparse/tmp, file_ID and filename + Note: for the server 8081, random number range is (100001,20000) + for the server 8080, random number range is (1,100000) + +4). Initialize the Tree: + When insert the new children nodes into the tree, first + make the pointer point to it's parent node, then insert the children list, + after insertion, set the pointer to the root node; + + If the parent id is 0,the children id is 0.1,0.2,0.3... + the children id of node 0.1 is 0.1.1, 0.1.2, 0.1.3.... + Node_fromID() function could find the specific node from the input node ID, + using the depth first searching, if exists, return the Node object, if not exists, return NULL + Class Node: + Each node save the file name, id, parent file and children directories + +5). Copy the input file into "/blogger/iparse/tmp" directory, + and save the new path as the file extract + path after copy successfully, check the input file type + (.tar/gz/zip file or directory), using the + corresponding method to extract the input file + +6). +```python +# Two types of user input: If user enter the .tar file path, +# could contain the compressed file and directory +# If user enter the directory file path, should not contain +# the compressed files, restrict to scripts +# or directories only; For the compressed files, saved into +# /blogger/iparse/tmp, for the directory file, use +# the original input path +``` + +7). +```python +# Extract all the subfiles when user click the directory name +# Extract the compressed file or open the directory when user click the link of filename +# Parse the script file and print the result in the screen when user click the script +# filename +# Extract the specific component files when user using the search component box +# +# "component"is the name of search input box. +# "subcomp" is the name of submit button for search input box. +# First check whether user click the directory and prefer to parse all the subfiles, +# if so, extract all the comporessed subfiles, and then parse the subfiles. +# Second, check whether user enter the component name in search box and submit. +# If so, extract all the files under the current file path and then parse all the +# files related with the specific component. +# iParse also give the search hint to users. If user search the components which +# are not exist in database, raise the exception. +# If user use search box and extract the file at the same time, raise the exception. +# Otherwise, print the parse result in the screen. +``` + +8). for the normal path, we could get the dir_ID and transfer into the file path +but one more concern is when user enter into the subdirectory and then return back +to the first one, it my has the issue. +```python +# If user enter into the subdirectory(example:0.5.1.1.1),do not parse the file and +# return back to the former one (for example: 0.5),system only save the +# subdirectory fileID (0.5.1.1.1). At this time, when user use the search box, +# we need to get the fileID (0.5) rather than 0.5.1.1.1. +# So first check the 'process' value in the current page, if it's not NULL, +# get the value 0.5.1,(the value of 'process' in each page only record the +# recent one, such as in the page 0.5, if we extract 0.5.1, in this page, +# the value of 'process' only record '0.5.1' rather then '0.5.1.1.1'), +# then using getParent() get the parent node 0.5. +# +# If user enter into this directory at the first time and use the search bar +# directly just get the file_path from directory +``` + +9). one scenario is, in the input files, if there is already the tree parse include, +we will not ask the user to enter the tree path again + +10). also for each users, has save one file, which has the timestamp, file information and tracking each step +whether pass the parsing script, debug file + +11). create a shell script to delete the files which expired for 3 days + +12). add the time and component filter, the output files are sorted based on the type ####5. Questions Cultural Behavior Interview(this interview also has a shorter coding problem in it @@ -70,7 +173,7 @@ Areas you will need to prepare in advance: submitting.", think deeply and also think broadly. * need to arrange the differnt work schedule -##### Why you want to work at Facebook (from a developer point of view) +##### Why you want to work at *** (from a developer point of view) * I like the company's culture: focus, passion, and fast, focused on building product and services that create a more open and connected world. * I am very interested in the Facebook's products and keep following the news about Facebook, such as the 6-pack, F4, cold storage tech for pictures. But instead of just reading the tech blogs and understanding the open source technology, I wanna participate in the development. * Facebook only has around 10,000 employees, but could reach and service for more than 1 billion people in the world, which is amazing. From 0a6ee454034af1e5063e164658775c0d3e9c9648 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 5 Aug 2015 20:39:17 -0700 Subject: [PATCH 21/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...351\235\242\347\273\217\351\233\206\345\220\210.py" | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index 5676d5a..b6b897a 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -194,6 +194,16 @@ def make_request(self): 比如你之前 有没有projfail过,为什么之类的。最后问了我whyuber, where’s ur passion之类的。 ''' +''' +change the people's traditional life style, +safety is the most important problem +add the mini meter to know how long to the desitination +how to ensure there is no overcharged +language issue +add/set the time for pick up + + +''' From becc0c31ee0ef44f32c1a7642cf5226ce191794b Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 6 Aug 2015 16:31:55 -0700 Subject: [PATCH 22/57] Create The_Skyline_Problem.py --- Array/The_Skyline_Problem.py | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Array/The_Skyline_Problem.py diff --git a/Array/The_Skyline_Problem.py b/Array/The_Skyline_Problem.py new file mode 100644 index 0000000..fad41b1 --- /dev/null +++ b/Array/The_Skyline_Problem.py @@ -0,0 +1,59 @@ +''' +A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. +Now suppose you are given the locations and height of all the buildings as shown on a cityscape +photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B). + +The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x +coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that +0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded +on an absolutely flat surface at height 0. + +For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] . +The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] +that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. +Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, +and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline +contour. + +For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]. + +Notes: + +The number of buildings in any input list is guaranteed to be in the range [0, 10000]. +The input list is already sorted in ascending order by the left x position Li. +The output list must be sorted by the x position. +There must be no consecutive horizontal lines of equal height in the output skyline. +For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be +merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...] +''' + +from heapq import * + +class Solution: + # @param {integer[][]} buildings + # @return {integer[][]} + def getSkyline(self, buildings): + if not buildings: return [] + skyline = [] + i = 0; n = len(buildings) + liveHR = [] + + while i < n or liveHR: + if not liveHR or (i < n and buildings[i][0] <= -liveHR[0][1]): + x = buildings[i][0] + while i < n and buildings[i][0] == x: + heappush(liveHR, (-buildings[i][2], -buildings[i][1])) + i += 1 + else: + x = -liveHR[0][1] + while liveHR and -liveHR[0][1] <= x: + heappop(liveHR) + height = len(liveHR) and -liveHR[0][0] + if not skyline or height != skyline[-1][1]: + skyline += [x, height], + + return skyline + +# https://leetcode.com/discuss/37736/108-ms-17-lines-body-explained +# https://leetcode.com/discuss/37630/my-c-code-using-one-priority-queue-812-ms + From 59dcbe61a03ecd03c6caf7b6aff2344149511334 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 6 Aug 2015 16:57:55 -0700 Subject: [PATCH 23/57] Update The_Skyline_Problem.py --- Array/The_Skyline_Problem.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Array/The_Skyline_Problem.py b/Array/The_Skyline_Problem.py index fad41b1..8cf5ae2 100644 --- a/Array/The_Skyline_Problem.py +++ b/Array/The_Skyline_Problem.py @@ -27,6 +27,19 @@ merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...] ''' +''' +analysis: +The idea is to do line sweep and just process the buildings only at the start and end points. +The key is to use a priority queue to save all the buildings that are still "alive". +The queue is sorted by its height and end time (the larger height first and if equal height, +the one with a bigger end time first). For each iteration, we first find the current process time, +which is either the next new building start time or the end time of the top entry of the live queue. +If the new building start time is larger than the top one end time, then process the one in the queue +first (pop them until it is empty or find the first one that ends after the new building); otherswise, +if the new building starts before the top one ends, then process the new building (just put them in the queue). +After processing, output it to the resulting vector if the height changes. Complexity is the worst case O(NlogN) +''' + from heapq import * class Solution: From 97def36a6ef06df9084343941a8abc7c34b0d83f Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 6 Aug 2015 21:59:07 -0700 Subject: [PATCH 24/57] Update Python_Basic.md --- Python/Python_Basic.md | 46 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/Python/Python_Basic.md b/Python/Python_Basic.md index a1b0485..6c06f07 100644 --- a/Python/Python_Basic.md +++ b/Python/Python_Basic.md @@ -1737,7 +1737,51 @@ The list looks like this now: [ func(func(s1, s2),s3), ... , sn ] #### 14. [Why we do not need to specific type in Python?](http://stackoverflow.com/questions/2489669/function-parameter-types-in-python) -#### 15. Reference +#### 15. [AND operation] (https://docs.python.org/2/reference/expressions.html#boolean-operations) +* In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. +* The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. +* (Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g., not 'foo' yields False, not ''.) +* For example +```python +>>> liveHR = [1,2,3,4,5] +>>> len(liveHR) +5 +>>> len(liveHR) and liveHR[-1] +5 +>>> liveHR[-1] +5 +>>> len(liveHR) and liveHR[1] +2 +>>> len(liveHR) and liveHR[2] +3 +>>> len(liveHR) and liveHR[3] +4 +>>> len(liveHR) and liveHR[0] +1 +>>> s = [] +>>> len(s) +0 +>>> len(s) and liveHR[0] +0 +>>> +>>> len(s) and liveHR[1] +0 +>>> len(s) and liveHR[2] +0 +>>> s = [1] +>>> len(s) and liveHR[2] +3 +>>> len(s) and liveHR[1] +2 +>>> +>>> +>>> liveHR[1] and len(s) +1 +>>> liveHR[3] and len(s) +1 +``` + +#### 16. Reference * [What's new in Python 3](https://docs.python.org/3/whatsnew/3.0.html) * [Tutorial: Python Build-in function](https://docs.python.org/2/library/functions.html#) From a4591925c4dc4e387ec74149c1c2c6ce3202017c Mon Sep 17 00:00:00 2001 From: UmassJin Date: Fri, 7 Aug 2015 09:03:44 -0700 Subject: [PATCH 25/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index b6b897a..4f1d558 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -202,7 +202,9 @@ def make_request(self): language issue add/set the time for pick up - +uber rush +uber essentials +uber eats ''' From 008e7533098b602250630d8a85b37c89c806671c Mon Sep 17 00:00:00 2001 From: UmassJin Date: Fri, 7 Aug 2015 17:47:23 -0700 Subject: [PATCH 26/57] Update Python_Basic.md --- Python/Python_Basic.md | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Python/Python_Basic.md b/Python/Python_Basic.md index 6c06f07..9111e2b 100644 --- a/Python/Python_Basic.md +++ b/Python/Python_Basic.md @@ -12,6 +12,7 @@ ####[11. Print in format](#print-format) ####[12. File Operation](#file-operation) ####[13. Time Complexity](#time-complexity) +####[14. Decorator](#decorator) ------------------------------------------------------------ @@ -2263,3 +2264,54 @@ https://www.youtube.com/watch?v=C4Kc8xzcA68 #### Reference * [Python time complexity wiki](https://wiki.python.org/moin/TimeComplexity) * http://stackoverflow.com/questions/24540975/why-does-key-in-d-keys-finish-in-on-time-while-key-in-d-finishes-in-o1?lq=1 + + + +## Decorator + +```python +>>> def outer1(): +... def inner1(): +... print "inside inner1" +... return inner1() +... +>>> foo = outer1() +inside inner1 +>>> +>>> def outer(): +... x = 1 +... def inner(): +... print x +... return inner() +... +>>> foo = outer() +1 +>>> foo = outer() +1 + +>>> import random +>>> random.random() +0.6539368726060638 +>>> +>>> +>>> def outer(): +... x = random.random() +... def inner(): +... print x +... return inner() +... +>>> outer() +0.0717367602537 +>>> +>>> outer() +0.40143942734 +>>> outer() +0.134812035114 +>>> outer() +0.280289368236 +>>> + +``` + +#### Reference +* [12 步骤搞定Decorator](http://mp.weixin.qq.com/s?__biz=MzA4MjEyNTA5Mw==&mid=209540854&idx=1&sn=0b7a85228126842a493adb1ff44c57bb&scene=5#rd) From 8221330e4000319f84d44f89d988ccc0a21aab92 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Sun, 9 Aug 2015 20:47:20 -0700 Subject: [PATCH 27/57] Update LRU_Cache.py --- Array/LRU_Cache.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Array/LRU_Cache.py b/Array/LRU_Cache.py index 19c12ac..65ab66d 100644 --- a/Array/LRU_Cache.py +++ b/Array/LRU_Cache.py @@ -1,8 +1,10 @@ ''' -Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. +Design and implement a data structure for Least Recently Used (LRU) cache. +It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. -set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. +set(key, value) - Set or insert the value if the key is not already present. +When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. ''' class ListNode: From 8bc5065d79799ce66738181db3f198c1687bc490 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Mon, 10 Aug 2015 20:59:42 -0700 Subject: [PATCH 28/57] =?UTF-8?q?Update=20Uber=E9=9D=A2=E7=BB=8F=E9=9B=86?= =?UTF-8?q?=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" index 4f1d558..d5fcbf2 100644 --- "a/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/Uber\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -326,4 +326,26 @@ def __cmp__(self, other): - +''' +onsite: +1. given the intervals, find the largest interval and the maximum overlap interval +2. culture fit +3. system design, design the msgs, what's app, wechat +4. living code + give the file, which has + > input: + column1, column2 + "a",b + "a,b","c/"f" + > output: + [{ + "column1":"a", + "column2": "b" + }, + { + "column1": "a,b", + "column2": "c"f" + }] + +5. culture fit, and simple coding +''' From 11ebd998293db2144b4e808ed1d6bbdf311f990f Mon Sep 17 00:00:00 2001 From: UmassJin Date: Mon, 10 Aug 2015 23:35:16 -0700 Subject: [PATCH 29/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index d8d6753..1cb0698 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4110,6 +4110,30 @@ def snake_ladder(move, n): How to get how many unival tree in a n-ary tree ''' +''' +130 +''' +import random + +def weighted_choice(seq): + if not seq: + return None + total_value = sum(v for _, v in seq) + random_value = random.randint(0, total_value-1) + print "random_value: ", random_value + pre = 0 + partial_sum = 0 + for value, i in seq: + partial_sum += i + if pre <= random_value < partial_sum: + print "" + return value + pre = partial_sum + return None + + +seq = [("a",3),("b",5),("c",8)] +print weighted_choice(seq) ======================================================================================== @@ -4498,6 +4522,7 @@ def abbreviation_unique(array): time complexity: O(n) space complexity: O(n) + ''' onsite: 1. find the shortest distance for each unlocked cell in matrix (警察那题) From 8aaddbe21c13a8e719ae83096e03812d7ca9b1e6 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 11 Aug 2015 11:14:46 -0700 Subject: [PATCH 30/57] Update Compare_Version_Numbers.py --- Array/Compare_Version_Numbers.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Array/Compare_Version_Numbers.py b/Array/Compare_Version_Numbers.py index 5de2950..4a7fb3c 100644 --- a/Array/Compare_Version_Numbers.py +++ b/Array/Compare_Version_Numbers.py @@ -9,6 +9,13 @@ 0.1 < 1.1 < 1.2 < 13.37 +# https://leetcode.com/discuss/45331/2-4-lines-python-3-different-ways +def compareVersion(self, version1, version2): + v1, v2 = (map(int, v.split('.')) for v in (version1, version2)) + d = len(v2) - len(v1) + return cmp(v1 + [0]*d, v2 + [0]*-d) + + class Solution: # @param version1, a string # @param version2, a string From 137fcfc7b925d3ac7aab2a8ae9fecd2291473844 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 11 Aug 2015 17:47:28 -0700 Subject: [PATCH 31/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 1cb0698..62af40f 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4135,6 +4135,39 @@ def weighted_choice(seq): seq = [("a",3),("b",5),("c",8)] print weighted_choice(seq) + +''' +131. +http://www.mitbbs.com/article_t1/JobHunting/33021975_0_1.html +1.leetcode上原题 number of islands +2.follow up:count rank 2 islands, where a rank 2 island is an island inside +a lake located on a continent. A continent is a piece of land located in +the ocean; the ocean is any body of water that touches the edges of the map. + +Example: +000000000 +000001100 +001111100 +011000100 +001010100 +001000100 +001111100 +000000000 +上面这个例子里应该返回1. + +3.If the input 2d array is too large to fit in memory, how to handle? + +我从第二个follow up开始就回答的磕磕绊绊,最后也没写code,一直在跟面试官讨论 +。后来思路终于讨论出来了,但第二个follow up面试官提示说water的那个dfs和第一 +问里的dfs有什么不同,后来明白他想说water的dfs要考虑对角线情况。第三个follow +up更是不知道怎么回答,瞎扯了一通。 + +请教各位大侠们,第三问该怎么考虑? +''' + + + + ======================================================================================== ''' From 69d797566505a93216448ffe7e59b99e77391940 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 11 Aug 2015 17:54:04 -0700 Subject: [PATCH 32/57] Update Strongly_Connect_Graph.py --- Algorithm/Strongly_Connect_Graph.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Algorithm/Strongly_Connect_Graph.py b/Algorithm/Strongly_Connect_Graph.py index f874c22..c2ecd20 100644 --- a/Algorithm/Strongly_Connect_Graph.py +++ b/Algorithm/Strongly_Connect_Graph.py @@ -1,12 +1,17 @@ # http://www.geeksforgeeks.org/strongly-connected-components/ # http://www.geeksforgeeks.org/connectivity-in-a-directed-graph/ ''' -Given a directed graph, find out whether the graph is strongly connected or not. A directed graph is strongly connected if there is a path between any two pair of vertices. For example, following is a strongly connected graph. -`对于无向图来说判断连通非常简单,只需要做一次搜索,然后判断路径中是否经过了每个节点即可。但是有向图不可以这么做,比如上图,从节点0开始搜索可以经过所有节点,但是很明显它不是strongly connected。 -Naive的方法是,对每一个节点做一次DFS,如果存在一个节点在DFS中没有经过每一个节点,那么则不是强连通图。这个算法复杂度是O(V*(V+E))。 +Given a directed graph, find out whether the graph is strongly connected or not. +A directed graph is strongly connected if there is a path between any two pair of vertices. +For example, following is a strongly connected graph. +`对于无向图来说判断连通非常简单,只需要做一次搜索,然后判断路径中是否经过了每个节点即可。 +但是有向图不可以这么做,比如上图,从节点0开始搜索可以经过所有节点,但是很明显它不是strongly connected。 +Naive的方法是,对每一个节点做一次DFS,如果存在一个节点在DFS中没有经过每一个节点,那么则不是强连通图。 +这个算法复杂度是O(V*(V+E))。 或者用Floyd Warshall算法来找出任意两个节点的最短路径。复杂度是O(v3)。 -一个更好的办法是强连通分量算法Strongly Connected Components (SCC) algorithm。我们可以用O(V+E)时间复杂度找出一个图中所有的SCC。如果SCC只有一个,那么这个图就是强连通图。 +一个更好的办法是强连通分量算法Strongly Connected Components (SCC) algorithm。 +我们可以用O(V+E)时间复杂度找出一个图中所有的SCC。如果SCC只有一个,那么这个图就是强连通图。 用Kosaraju算法,两个pass做DFS: From 181287ec77821c784a85261309099d6030540324 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 11 Aug 2015 18:39:03 -0700 Subject: [PATCH 33/57] Update Strongly_Connect_Graph.py --- Algorithm/Strongly_Connect_Graph.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Algorithm/Strongly_Connect_Graph.py b/Algorithm/Strongly_Connect_Graph.py index c2ecd20..a763e3b 100644 --- a/Algorithm/Strongly_Connect_Graph.py +++ b/Algorithm/Strongly_Connect_Graph.py @@ -26,7 +26,7 @@ def dfs(node, visited): visited[node] = True for neighbor in node.neighbor: if not visited[neighbor]: - dfs(neighbor) + dfs(neighbor, visited) def strongly_connected_graph(graph): visited = [False for i in xrange(graph.V)] From 30efca8da479b69cf0138a02e95d1817bfc57f62 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 11 Aug 2015 20:37:58 -0700 Subject: [PATCH 34/57] Update Tree.py --- Array/Tree.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Array/Tree.py b/Array/Tree.py index 27250dc..ad4331f 100644 --- a/Array/Tree.py +++ b/Array/Tree.py @@ -36,6 +36,9 @@ */ ''' +# why we can not reconstruct tree based on preorder and postorder +# http://www.cmi.ac.in/~madhavan/courses/programming06/lecture12-21sep2006.txt + class tree_node(object): def __init__(self, value): self.val = value From 77d48febbe6cbdf09b04460265567731348335c1 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 11 Aug 2015 21:29:42 -0700 Subject: [PATCH 35/57] Update Binary Tree Serialization.py --- LintCode/Binary Tree Serialization.py | 53 +++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/LintCode/Binary Tree Serialization.py b/LintCode/Binary Tree Serialization.py index 13075d9..e2b769d 100644 --- a/LintCode/Binary Tree Serialization.py +++ b/LintCode/Binary Tree Serialization.py @@ -1,7 +1,11 @@ ''' -Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'. +Design an algorithm and write code to serialize and deserialize a binary tree. +Writing the tree to a file is called 'serialization' and reading back from the file to +reconstruct the exact same binary tree is 'deserialization'. -There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure. +There is no limit of how you deserialize or serialize a binary tree, +you only need to make sure you can serialize a binary tree to a string and +deserialize this string to the original structure. Have you met this question in a real interview? Yes Example @@ -13,7 +17,6 @@ / \ 15 7 Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input. - You can use other method to do serializaiton and deserialization. ''' @@ -82,6 +85,50 @@ def deserialize_helper(self, data): node.right = self.deserialize_helper(data) return node +''' + +If given Tree is Binary Search Tree? +If the given Binary Tree is Binary Search Tree, we can store it by either storing preorder or postorder traversal. +In case of Binary Search Trees, only preorder or postorder traversal is sufficient to store structure information. +''' + +class TreeNode: + def __init__(self, value=0): + self.value = value + self.left = None + self.right = None + +def construct_BST_from_preorder(preorder): + if not preorder: + return None + size = len(preorder) + root = TreeNode(preorder[0]) + stack = [root] + for value in preorder[1:]: + temp = None + while stack and value > stack[-1].value: + temp = stack.pop() + if temp: + temp.right = TreeNode(value) + stack.append(temp.right) + else: + stack[-1].left = TreeNode(value) + stack.append(stack[-1].left) + return root + + +''' +If given Binary Tree is Complete Tree? +A Binary Tree is complete if all levels are completely filled except possibly the last level and all nodes of +last level are as left as possible (Binary Heaps are complete Binary Tree). For a complete Binary Tree, +level order traversal is sufficient to store the tree. We know that the first node is root, next two nodes +are nodes of next level, next four nodes are nodes of 2nd level and so on. + +If given Binary Tree is Full Tree? +A full Binary is a Binary Tree where every node has either 0 or 2 children. It is easy to serialize such +trees as every internal node has 2 children. We can simply store preorder traversal and store a bit with +every node to indicate whether the node is an internal node or a leaf node. +''' ''' # Google interview: http://www.1point3acres.com/bbs/thread-131485-1-1.html From f038dd933b23f8d528b237ce8e54d70e8ee58b01 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Tue, 11 Aug 2015 22:22:38 -0700 Subject: [PATCH 36/57] Update Tree.py --- Array/Tree.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Array/Tree.py b/Array/Tree.py index ad4331f..c5895ae 100644 --- a/Array/Tree.py +++ b/Array/Tree.py @@ -415,8 +415,7 @@ def hasPathSum(root, sum): if not root: return False sum = sum - root.val if not root.left and not root.right: - if sum == 0: return True - else: return False + return sum == 0 return hasPathSum(root.left, sum) or hasPathSum(root.right, sum) # 这道题容易做错的几点: From 4a2e13d568c1922c6c32b7998cf07bbefdcba3d5 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 09:10:47 -0700 Subject: [PATCH 37/57] Update Tree.py --- Array/Tree.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Array/Tree.py b/Array/Tree.py index c5895ae..fa68605 100644 --- a/Array/Tree.py +++ b/Array/Tree.py @@ -982,5 +982,38 @@ def rightSideView(self, root): } return nullptr; } + + # 42. print out the path from root to the leaf in the binary tree + +def print_path(root): + if not root: + return + result = [] + print_path_helper(root, result, []) + return result + +def print_path_helper(node, result, subpath): + if not node: + return + if not node.left and not node.right: + result.append(subpath[:]+[node.value]) # Note: here we should use the subpath[:], not the subpath directly + else: + print_path_helper(node.left, result, subpath+[node.value]) + print_path_helper(node.right, result, subpath+[node.value]) + +>>> list1 = [1,2,3] +>>> result = [] +>>> result.append(list1) +>>> result +[[1, 2, 3]] +>>> id(result[0]) +4565113024 +>>> id(list1) +4565113024 +>>> list1.append(5) +>>> result +[[1, 2, 3, 5]] + + ``` From e00804dd823a63549f49cc65aeeb9a4b8b4b6d2d Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 10:34:23 -0700 Subject: [PATCH 38/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 62af40f..9ac7a11 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4166,6 +4166,75 @@ def weighted_choice(seq): ''' +''' +132. 给一个binary tree 打印所有的path~~然后问了时间空间复杂度~~就用一般递归做的 +# http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=139354&extra=page%3D1%26filter%3Dsortid%26sortid%3D311&page=1 +''' + # 42. print out the path from root to the leaf in the binary tree + +def print_path(root): + if not root: + return + result = [] + print_path_helper(root, result, []) + return result + +def print_path_helper(node, result, subpath): + if not node: + return + if not node.left and not node.right: + result.append(subpath[:]+[node.value]) # Note: here we should use the subpath[:], not the subpath directly + else: + print_path_helper(node.left, result, subpath+[node.value]) + print_path_helper(node.right, result, subpath+[node.value]) + +# time complexity: O(nlogn) +# space complexity: O(n) + +>>> list1 = [1,2,3] +>>> result = [] +>>> result.append(list1) +>>> result +[[1, 2, 3]] +>>> id(result[0]) +4565113024 +>>> id(list1) +4565113024 +>>> list1.append(5) +>>> result +[[1, 2, 3, 5]] + + +''' +133. +2. good number问题。 一个数如果能用(至少两组)两个立方数相加得到那么就是good number。print小于等于n的所有good number。 + 分析时间复杂度。 + 我先把小于n的所有立方数存起来。然后就变成了2 sum问题了。。。 + +第二题答的是O(n^(4/3)),外层大循环是O(n),里层的存的立方数是O(n^(1/3)),两个相乘。阿三考官嘟囔了半天我也没太听明白他到底什么意思= = +# http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=139354&extra=page%3D1%26filter%3Dsortid%26sortid%3D311&page=1 +''' +import collections + +def happy_number(n): + if n == 0: return 0 + seq = [] + result = [] + for i in xrange(1, n+1): + tmp = i * i * i + if tmp > n: + break + seq.append(tmp) + print "seq: ", seq + table = collections.defaultdict(int) + for i in xrange(len(seq)): + for j in xrange(i+1, len(seq)): + table[seq[i]+seq[j]] += 1 + if table[seq[i]+seq[j]] > 1: + result.append(seq[i] + seq[j]) + print result + +happy_number(500) ======================================================================================== From f45349d25a8461b03cbb745420b7278f5125df9e Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 12:09:04 -0700 Subject: [PATCH 39/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 9ac7a11..0bfb1f8 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4236,6 +4236,63 @@ def happy_number(n): happy_number(500) +''' +134.Implement a function to perform basic string compression using the counts of repeated characters. +For example, the string aabcccccaaa would become a2b1c5a3. +''' +def compress_string(string): + if not string: return None + count = 1; i = 1 + cur = string[0] + result = [] + while i <= len(string): + if i < len(string) and string[i] == cur: + count +=1 + else: + result.append(cur+str(count)) + if i < len(string): + cur = string[i] + count = 1 + i += 1 + return ''.join(result) + +print compress_string("aabccccaaadddddd") + + +''' +135. compress string +For example, the string aabcccccaaa would become a*2b*1c*5a*3. the original string may still have +number and * +''' + +def compress_string(string): + if not string: return None + count = 1; i = 1 + cur = string[0] + result = [] + while i < len(string): + if cur.isdigit(): + result.append("#"+cur) + cur = string[i] + i += 1 + elif cur == "*": + result.append("#"+cur) + cur = string[i] + i += 1 + else: + while i < len(string) and string[i] == cur: + count +=1 + i += 1 + result.append(cur+"*"+str(count)) + if i < len(string): + cur = string[i] + count = 1 + i += 1 + return ''.join(result) + +print compress_string("aa2*5bccc") +print compress_string("aa2*5bcccc3*aaadddddd") + ======================================================================================== From f78ef96490bbe3bdff6fcceb591f9b644b4588a8 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 12:24:29 -0700 Subject: [PATCH 40/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 0bfb1f8..0a5258a 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4294,6 +4294,30 @@ def compress_string(string): print compress_string("aa2*5bcccc3*aaadddddd") +''' +136. +# http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=134847&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311 +Q) Write a program to count the total number of pages reachable from a website. +For example, given "nytimes.com", count the number of pages reachable from there. +You can assume you are given a function to fetch the page and extract the inner links, e.g.: +List fetchPageAndExtractUrls(String url); +''' + + + +''' +137. +# http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=134847&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311 +Q) Given a tiny computer with a 1 MHz CPU and 1 KiB of RAM memory; +no input; +only output is an LED light that says “I am done”. +(1 MHz == 1 million instructions per second) +I load an arbitrary unknown program onto this computer.. +How long do we have to wait in wall-clock time before we can prove the program has an infinite loop? +''' + + + ======================================================================================== ''' From 8bbfb50f31c39e9361c7b076b02ba59eaca4749a Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 14:49:27 -0700 Subject: [PATCH 41/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 0a5258a..d0255f6 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4302,8 +4302,22 @@ def compress_string(string): You can assume you are given a function to fetch the page and extract the inner links, e.g.: List fetchPageAndExtractUrls(String url); ''' +def fetch_pages(url): + visited = {} + total_url = [0] + fetch_helper(url, total_url, visited) + return total_url[0] - +def fetch_helper(url, total_url, visited): + children = fetchPageAndExtractUrls(url) + if not children: + return + for child in children: + if child not in visited: + visited[child] = True + total_url[0] += 1 + fetch_helper(child, total_url, visited) + ''' 137. @@ -4315,7 +4329,16 @@ def compress_string(string): I load an arbitrary unknown program onto this computer.. How long do we have to wait in wall-clock time before we can prove the program has an infinite loop? ''' +因为数据和程序都在内存中,所以,如果在某这两个时间点,内存中的内容处于完全相同的状态, +那么从这两个时间点之后的所有状态也一定会完全相同(除非这是台量子计算机)。 +那么,如果一个程序在开始执行之后,内存先后出现两个完全相同的状态的话,那么这个程序一定是死循环。. +1kilobyte = 2^13 bit, 所以该计算机内存可能存在的不同状态是2^14种。 +因为每次instruction都一定会改变内存的状态(因为但凡有一次不改变,那就已经死循环了), +所以这个计算机最大能执行的不相同操作是2^14次(因为如果程序在2^14次操作中还没能停机, +那第2^14+1次操作一定和之前的2^14次操作中的某一个相同)。 +又因为运行速度是1秒10^6 = 2^20次操作,因此在2^(-6) = 0.016秒内,就能够进行2^14次操作。 +也就是说,如果0.016秒内还没能停下的程序,就永远不会停下了。 ======================================================================================== From 9a46b98fcb51a199eb7e02d8cd12e6195c3d7196 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 19:06:38 -0700 Subject: [PATCH 42/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index d0255f6..25c1d95 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4341,6 +4341,128 @@ def fetch_helper(url, total_url, visited): 也就是说,如果0.016秒内还没能停下的程序,就永远不会停下了。 +''' +138. +# http://www.1point3acres.com/bbs/forum.php?mod=viewthread&tid=138912&extra=page%3D1%26filter%3Dsortid%26sortid%3D311%26searchoption%5B3046%5D%5Bvalue%5D%3D1%26searchoption%5B3046%5D%5Btype%5D%3Dradio%26sortid%3D311 +第二题,一道Math题。就是求float number 的squre root: public float sqrt(float f, int p), +precision是表示小数点后位数(2就要两位一致)。我就先找到result两边的integer标为l , r。 +然后就一阵二分法。问题是, 判断precision和大于一小于一时出错了。然后一阵改。。。。。 +表示很无奈。这种math, corner cases特别多的没准备好。说好的array, string,说好的tree,说好的recursive呢,都没有。。. + +判断数据是否符合精确度,就用两个数都乘以10^p,再取整比较是否相等。比如p=2, f=0.64, curRes = 0.639就不行因为 +都乘以100以后取整是64, 和63不相等。 如果curRes = 0.645就没问题。 + +至于curRes,我就先二分法取到整数范围,比如8开平方根在(2,3)范围,再再(2,3)范围内二分,判断精确度。要注意的是, +(2,3)大于1和(0,1)小于一两种范围二分的时候方向不同。大于一越平方越大,小于一越平方越小 + +精确度要求是小数点平方根res的平方和要求根的数f,小数点之后p位要保持一致。所以比如p=2,如果res^2和f分别是1.50和1.501是可以的, +如果是1.499和1.50就不行了。然而这两个却都符合相差绝对值小于0.01 +''' + +def sqrt_float(num, p): + ''' + @num: float number + @p: precision after dot + ''' + if num <=0: return 0 + l = 0; r = num + if num < 1: r = 1 + + while l <= r: + mid = (l + r) / 2.0 + print "l: %f, r: %f, mid: %f" %(l, r, mid) + if mid * mid > num: + r = mid + elif mid * mid < num: + l = mid + if judge_precision(num, mid*mid, p): + return mid + return None + +def judge_precision(num, res, p): + if int(num*(10**p)) == int(res*(10**p)): + print "res: ", int(res*(10**p)) + return True + return False + +print sqrt_float(1.6, 2) + +[JINZH2-M-20GQ: ~/Desktop/Python_training/Leetcode]: python sqrt_float.py +l: 0.000000, r: 1.600000, mid: 0.800000 +l: 0.800000, r: 1.600000, mid: 1.200000 +l: 1.200000, r: 1.600000, mid: 1.400000 +l: 1.200000, r: 1.400000, mid: 1.300000 +l: 1.200000, r: 1.300000, mid: 1.250000 +l: 1.250000, r: 1.300000, mid: 1.275000 +l: 1.250000, r: 1.275000, mid: 1.262500 +l: 1.262500, r: 1.275000, mid: 1.268750 +res: 160 +1.26875 + + +print sqrt_float(0.556, 2) + +[JINZH2-M-20GQ: ~/Desktop/Python_training/Leetcode]: python sqrt_float.py +l: 0.000000, r: 1.000000, mid: 0.500000 +l: 0.500000, r: 1.000000, mid: 0.750000 +l: 0.500000, r: 0.750000, mid: 0.625000 +l: 0.625000, r: 0.750000, mid: 0.687500 +l: 0.687500, r: 0.750000, mid: 0.718750 +l: 0.718750, r: 0.750000, mid: 0.734375 +l: 0.734375, r: 0.750000, mid: 0.742188 +res: 55 +0.7421875 + +''' +139. +Given an array of N=10^6 int32 and an int32 X, compute the exact number of triples (a, b, c) +of distinct elements of the array so that a + b + c <= X +其实就是和3Sum差不多,不过写完被他问了可能的overflow问题,然后立马改了下就结束了 + +假设X很大,比如是2^31,而数组中的数字是1,2,3...10^6,那么数组中的每一个triplet都满足条件, +最后数出来的个数会是C(3, 10^6) ~= 2^58。这在C++中就必须上long long了。但是如果数组再稍微大一点, +比如到10^7的话,long long就可能hold不住了;所谓的overflow应该就是指这个。所以这个限制非常关键, +如果是面试官一开始主动提出来这个限制的话,我觉得算是比较良心的。 + +当然,三个int32的和会超过int32这也是一个重要的overflow的点。多谢指出。这两个overflow的点都可以通过使用long long进行规避 +''' + +''' +140 +Binary Tree Maximum Path Sum +Can not have the near node in the final path +''' +def maxpathsum_bt(root): + if not root: + return None + result = [-1<<32] + maxpath(root, result) + return result[0] + +def maxpath(node, result): + ''' + return (include node, exclude node) + ''' + if not node: return (0,0) + l = maxpath(node.left, result) + r = maxpath(node.right, result) + + isum = max(0, l[1]) + max(0, r[1]) + node.value + result[0] = max(result[0], isum) + include = node.value + max(0, l[1],r[1]) + + isum = 0 + isum1 = max(0, l[0], l[1]) + isum2 = max(0, r[0], r[1]) + isum = isum1 + isum2 + result[0] = max(result[0], isum) + exclude = max(isum1, isum2) + + return (include, exclude) + + + + ======================================================================================== ''' From af339174e040b1d7218935402da454e1dbd41d34 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 21:37:39 -0700 Subject: [PATCH 43/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 25c1d95..e747056 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4462,6 +4462,68 @@ def maxpath(node, result): +''' +141. +# http://www.mitbbs.com/article_t1/JobHunting/32972467_0_1.html +2.combination +给你一个list of list [[Hello, Hi], [world, girl, boy]] +print: +Hello world +Hello girl +Hello boy +Hi world +Hi girl +Hi boy + +给出了recursive解法,有个地方忘写return了被指出,改正 +followup: how to do it iteratively? + +''' +def combination(lists): + if not lists or not lists[0]: + return None + result = [] + n = len(lists) + combination_helper(lists, result, [], n) + return result + +def combination_helper(lists, result, sublist, length): + if len(sublist) == length: + result.append(sublist) + return + + for i, list_member in enumerate(lists): + for l in list_member: + combination_helper(lists[i+1:], result, sublist+[l], length) + +#lists = [["hello", "world"], ["fight", "cisco", "hi"], ["google","fighting!"]] +#print combination(lists) + +lists1 = [["hello", "world"], ["fight", "cisco", "hi"]] +print combination(lists1) + +def combination2(lists): + if not lists or not lists[0]: + return None + result = [[]] + for list_member in lists: + ret = [] + for comb in result: + for l in list_member: + ret.append(comb+[l]) + #print "ret: ", ret + if ret: + result = ret + return result + +#lists2 = [["hello", "world"], ["fight", "cisco", "hi"], ["google","fighting!"]] +#print combination2(lists2) + +lists3 = [["hello", "world"], ["fight", "cisco", "hi"]] +print combination(lists3) +~ + + ======================================================================================== From dbc985eab31cc4bdf7e7f171af21b4e04290fbe4 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Wed, 12 Aug 2015 21:52:44 -0700 Subject: [PATCH 44/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index e747056..30474fe 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4425,6 +4425,9 @@ def judge_precision(num, res, p): 如果是面试官一开始主动提出来这个限制的话,我觉得算是比较良心的。 当然,三个int32的和会超过int32这也是一个重要的overflow的点。多谢指出。这两个overflow的点都可以通过使用long long进行规避 + +如果限定数组小于等于10^6次,用64位整型就一定可以表示结果;但是如果数组较大,恐怕就要考虑1)用string表示, +2)用一个数组表示,3)抛出异常,根据实际情况选择方案。这些方案都可以跟面试官提 ''' ''' @@ -4524,6 +4527,55 @@ def combination2(lists): ~ +''' +142. +第二题是找Binary Search Tree 里面的出现次数最多的integer..... + +TreeNode pre = null; + int max = 0;. + int val = 0;. + public void commonValue(TreeNode root){ + if(root == null){ + max = Math.max(max, val); + return; + } + + commonValue(root.left);. + + if(pre == null){ + val = 1; + }else if(pre.val == root.val){. + val++; + }else{ + max = Math.max(max, val); + val = 1; + }- + pre = root;. + + commonValue(root.right); + } + +''' + +''' +143. +input:一个字符串可能含有 ‘*’,‘*’可以代表'j' 或'k' +要求打印所有可能的字符串 +''' + +def dfs(s, i): + if len(s) == i: + print s + return + if s[i] == '*': + dfs(s[:i]+'j'+s[i+1:], i+1) + dfs(s[:i]+'k'+s[i+1:], i+1) + else: + dfs(s, i+1) + + +s = "abc*e*d" +print dfs(s,0) ======================================================================================== From 3c02d53570b2e5226bedeeaa38836f8ba046b92d Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 00:04:57 -0700 Subject: [PATCH 45/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 30474fe..098ef9d 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4577,6 +4577,83 @@ def dfs(s, i): s = "abc*e*d" print dfs(s,0) +''' +144. +Find the longest increasing(increasing means one step) sequence in an integer matrix in 4 directions +(up down left right), return the sequence + +直接DFS效率太低,这题主要考DP+记忆化。 +DP方程很明显: +opt[i][j] = max{ opt[i+1][j], opt[i-1][j], opt[i][j+1], opt[i][j-1] } +1 +可以参考POJ原题,1088滑雪。 +''' +def longest_inc(matrix): + if not matrix or not matrix[0]: + return None + m = len(matrix); n = len(matrix[0]) + mem = [[0 for _ in xrange(n)] for _ in xrange(m)] + path = 0; maxpath = 0 + for i in xrange(m): + for j in xrange(n): + path = dfs(mem, matrix, i, j) + if path > maxpath: + maxstart = matrix[i][j] + maxpath = path + print "start: ",maxstart + return maxpath + +def dfs(mem, matrix, i, j): + m = len(matrix); n = len(matrix[0]) + if mem[i][j]: + return mem[i][j] + dirs = [[-1,0],[1,0],[0,-1],[0,1]] + for d in dirs: + newi = i + d[0] + newj = j + d[1] + if newi < 0 or newi >=m or newj < 0 or newj >= n: + continue + if matrix[newi][newj] == (matrix[i][j] + 1): + mem[i][j] = max(mem[i][j], dfs(mem, matrix, newi, newj)) + mem[i][j] += 1 + print mem + return mem[i][j] + +matrix = [ [1,9,8,6], + [7,3,4,5], + [5,2,6,8]] + +print longest_inc(matrix) + +# mem table: +[[1, 1, 2, 1], [1, 4, 3, 2], [1, 5, 1, 1]] +start: 2 +5 + + +''' +145. + +1. longest consecutive numbers +lc原题,但要考虑重复,而且numbers无序, 并且[size=13.3333330154419px]要输出最长的numbers, +example: +1, 2, 3, 4, 6, 7, 8, 9, 10, 11 → 6, 7,8, 9, 10, 11. +11, 10, 9, 8, 7, 6 4, 3, 2,1 ->11, 10, 9, 8, 7, 6 +1, 2, 3, 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5 + +1, 2, 3, 4, 3, 4, 5, 6, 7 -> 3, 4, 5, 6, 7 + +2.第1题的follow-up +numbers变成二叉树,找longest consecutive numbers +example: + 1 + 2 3 + 5 3 + +→ 1, 2 +树的题一向做的不好,感觉和树这种数据结构不来电,花了挺长时间,最后面试官说简化只要求最长的length就好, +因为时间紧迫,随便写了一个递归就交了,不知道有没有bug,但是从面试官反应来看,应该写的不是太没水平~~ +''' + ======================================================================================== ''' From 1381355f569b0f93b741bd4b15eb04a5bb3ed14f Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 11:12:12 -0700 Subject: [PATCH 46/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 61 +++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 098ef9d..8c3d405 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4632,16 +4632,13 @@ def dfs(mem, matrix, i, j): ''' 145. - 1. longest consecutive numbers -lc原题,但要考虑重复,而且numbers无序, 并且[size=13.3333330154419px]要输出最长的numbers, +lc原题,但要考虑重复,而且numbers无序, 并且要输出最长的numbers, example: 1, 2, 3, 4, 6, 7, 8, 9, 10, 11 → 6, 7,8, 9, 10, 11. 11, 10, 9, 8, 7, 6 4, 3, 2,1 ->11, 10, 9, 8, 7, 6 1, 2, 3, 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5 -1, 2, 3, 4, 3, 4, 5, 6, 7 -> 3, 4, 5, 6, 7 - 2.第1题的follow-up numbers变成二叉树,找longest consecutive numbers example: @@ -4654,6 +4651,62 @@ def dfs(mem, matrix, i, j): 因为时间紧迫,随便写了一个递归就交了,不知道有没有bug,但是从面试官反应来看,应该写的不是太没水平~~ ''' + + + +''' +146. +第一轮,给一个字符串数组,找出这样的字符串对(str1,str2),使得1,两个字符串不包含一样的字符,2. 长度乘积最大。 +我一开始对面试官说O(n^2)比较intuitive,然后写完了之后让我改进,后来想了一个按长度先排序, +然后设置两个指针慢慢移动,估计会好很多。 + +给你一个很大的字典。一对词如果不share 任何字母,比如dog, cat不share字母,而dog, boy就share一个o, +则是interesting pair.找出所以interesting pairs中长度乘积最大的pair.输出这个乘积。 +''' + +def compare_str(lists): + ''' + assume the input string only has the lowercase letters + @first could transfer each string into the bit, then compare + each pair of string whether they are the same or not + ''' + if not lists: + return None + n = len(lists) + bit_pattern = [] + lists = sorted(lists, key=len) + for s in lists: + x = 0 + for char in s: + x = x | 1 << ord(char)-ord('a') + bit_pattern.append(x) + print "bit: ", bit_pattern + maxlen = 0 + for i in xrange(n): + j = n - 1 + while i < j: + if not bit_pattern[i] & bit_pattern[j]: + maxlen = max(maxlen, len(lists[i])*len(lists[j])) + print "two lists: %s, %s" %(lists[i], lists[j]) + break + j -= 1 + return maxlen + +lists = ['fighting', 'google', 'hello', 'hi', 'how',''] +lists1 = ['hello','cisc','cisco','wlddd'] +print compare_str(lists) +print compare_str(lists1) + + +''' +147. +将一个数组right rotate k次。要求O(N),in-place +# http://www.geeksforgeeks.org/array-rotation/ +''' + + + + ======================================================================================== ''' From 0528bf56478da9728451de2d4d50e1df7fe64cef Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 12:13:52 -0700 Subject: [PATCH 47/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 8c3d405..718fe29 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4651,6 +4651,45 @@ def dfs(mem, matrix, i, j): 因为时间紧迫,随便写了一个递归就交了,不知道有没有bug,但是从面试官反应来看,应该写的不是太没水平~~ ''' +def longest_consecutive(num): + if not num: + return None + n = len(num) + table = {} + result = 0 + resultlist = [] + for i in num: + left = 0; right = 0 + leftlist = []; rightlist = [] + if i not in table: + if i-1 in table: + left = table[i-1][0] + leftlist = table[i-1][1] + if i+1 in table: + right = table[i+1][0] + rightlist = table[i+1][1] + + isum = left + right + 1 + ilist = leftlist + [i] + rightlist + + table[i] = (isum,ilist) + table[i-left] = (isum,ilist) + table[i+right] = (isum,ilist) + if len(ilist) > len(resultlist): + resultlist = ilist + result = max(isum, result) + print resultlist + return result + + +test = [100, 4, 200, 1, 3, 2, 201, 202, 5] +print longest_consecutive(test) + +# Corner Case +# Input: [0,3,7,2,5,8,4,6,0,1] +# Output: 7 +# Expected: 9 + From 19e146dcf5f93998125b63179c4d475703af43d1 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 14:51:19 -0700 Subject: [PATCH 48/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 718fe29..c1f74c0 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4638,17 +4638,6 @@ def dfs(mem, matrix, i, j): 1, 2, 3, 4, 6, 7, 8, 9, 10, 11 → 6, 7,8, 9, 10, 11. 11, 10, 9, 8, 7, 6 4, 3, 2,1 ->11, 10, 9, 8, 7, 6 1, 2, 3, 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5 - -2.第1题的follow-up -numbers变成二叉树,找longest consecutive numbers -example: - 1 - 2 3 - 5 3 - -→ 1, 2 -树的题一向做的不好,感觉和树这种数据结构不来电,花了挺长时间,最后面试官说简化只要求最长的length就好, -因为时间紧迫,随便写了一个递归就交了,不知道有没有bug,但是从面试官反应来看,应该写的不是太没水平~~ ''' def longest_consecutive(num): @@ -4690,11 +4679,52 @@ def longest_consecutive(num): # Output: 7 # Expected: 9 +''' +147.第1题的follow-up +numbers变成二叉树,找longest consecutive numbers +example: + 1 + 2 3 + 5 3 + +→ 1, 2 +题目简化:连续递增from leaf to root,只考虑单向, +用一个 global update maxlen,每个节点返回(value, MaxLentoThisNode) + +树的题一向做的不好,感觉和树这种数据结构不来电,花了挺长时间,最后面试官说简化只要求最长的length就好, +''' +def longest_consecutive_list(root): + ''' + @only consider the path from root to each leaf + @only consider the increasing path from root + ''' + if not root: + return None + result = [-1<<32] + consecutive_helper(root, result) + return result[0] + +def consecutive_helper(node, result): + if not node: return None + if not node.left and not node.right: + return (node.value, 1) + + l = consecutive_helper(node.left, result) + r = consecutive_helper(node.right, result) + + tmpleft = 0; tmpright = 0 + if l and node.value == (l[0] + 1): + tmpleft = l[1] + 1 + if r and node.value == (r[0] + 1): + tmpright = r[1] + 1 + result[0] = max(tmpleft, tmpright, result[0]) + + return (node.value, max(tmpleft, tmpright)) ''' -146. +148. 第一轮,给一个字符串数组,找出这样的字符串对(str1,str2),使得1,两个字符串不包含一样的字符,2. 长度乘积最大。 我一开始对面试官说O(n^2)比较intuitive,然后写完了之后让我改进,后来想了一个按长度先排序, 然后设置两个指针慢慢移动,估计会好很多。 @@ -4738,13 +4768,17 @@ def compare_str(lists): ''' -147. +149. 将一个数组right rotate k次。要求O(N),in-place # http://www.geeksforgeeks.org/array-rotation/ ''' - +''' +150. +1. 字符串匹配 -> anagram -> 结合起来 不用完全的字符串匹配 只要是anagram就算成功 +也就是有两个字符串s和word word和s的某个子字符串是anagram就行。时间复杂度 空间复杂度 尽量优化 +''' ======================================================================================== From ddcf89d7f113eaa004c4cbef0f5a36ad9fc8692f Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 18:27:50 -0700 Subject: [PATCH 49/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index c1f74c0..7e5a3b1 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4778,8 +4778,44 @@ def compare_str(lists): 150. 1. 字符串匹配 -> anagram -> 结合起来 不用完全的字符串匹配 只要是anagram就算成功 也就是有两个字符串s和word word和s的某个子字符串是anagram就行。时间复杂度 空间复杂度 尽量优化 +# http://www.geeksforgeeks.org/anagram-substring-search-search-permutations/ +# http://www.careercup.com/question?id=5389078581215232 ''' +import collections + +def string_anagram(lists, word): + if not lists or not word or (len(lists) < len(word)): + return False + + m = len(lists); n = len(word) + target = collections.Counter(word) + cur = collections.Counter(lists[:n]) + for i in xrange(n,m): + if target == cur: + return lists[i-n:i] + + cur[lists[i]] += 1 + cur[lists[i-n]] -= 1 + if not cur[lists[i-n]]: + del cur[lists[i-n]] + if target == cur: + return lists[i-n+1:] + return False + + +l = "BACEGADCBA" +w = "ABCD" +print string_anagram(l, w) + +l1 = "cdsgsdgsa" +w1 = "a" +print string_anagram(l1, w1) + +l2 = "abcfedgsfdaf" +w2 = "abcdefg" +print string_anagram(l2, w2) + ======================================================================================== ''' From 4a1feb3e927ee83d0c3345f647c751151e8ec786 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 20:13:25 -0700 Subject: [PATCH 50/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 7e5a3b1..feb1cd9 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -4816,6 +4816,49 @@ def string_anagram(lists, word): w2 = "abcdefg" print string_anagram(l2, w2) + +''' +compare the string using heapq +''' + +import heapq + +class MyLine(object): + def __init__(self, string): + self.string = string + + def __cmp__(self, other): + for i in xrange(min(len(self.string), len(other.string))): + if self.string[i] > other.string[i]: + return 1 + elif self.string[i] < other.string[i]: + return -1 + else: + continue + if len(self.string) > len(other.string): + return 1 + elif len(self.string) < len(other.string): + return -1 + else: + return 0 + +class Solution(object): + def heapq_str(self): + heap = ["hello","world","fighting","cisco"] + heap1 = [] + for s in heap: + heap1.append(MyLine(s)) + heapq.heapify(heap1) + result = [] + while heap1: + element = heapq.heappop(heap1) + result.append(element.string) + + return result + +test = Solution() +print test.heapq_str() + ======================================================================================== ''' From deed3382e28a4dbdc8b98c7d7702713a8be04e2c Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 20:37:59 -0700 Subject: [PATCH 51/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index feb1cd9..664da0b 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -84,35 +84,52 @@ def permutation_number(s): # Best Reference: http://stackoverflow.com/questions/25285792/generate-all-permutations-of-a-list-without-adjacent-equal-elements ''' -3. find h index element in the array, at least n elements in the arrary larger than n, but here is no n+1 elements larger than n+1 +3. find h index element in the array, at least n elements in the array larger than n, but here is no n+1 elements larger than n+1 ''' def find_h_index(arr): n = len(arr) - 1 start = 0; end = n distance = 0 while start <= end: - if end % 2 == 0: - mid = (start + end) / 2 - else: - mid = (start + end) / 2 + 1 - + mid = (start + end + 1) / 2 if arr[mid] >= (n - mid): distance = n - mid + #print "mid: ", mid + #print "distance: ", distance end = mid - 1 - + elif arr[mid] < (n - mid): start = mid + 1 - + return distance -test1 = [0,3,4,7,8,9,10] -test2 = [0,3,4,7,8,9] -test3 = [1] +test1 = [0,3,4,7,8,9,10] -> 4 +test2 = [0,3,4,7,8,9] -> 3 +test3 = [1,2,4,7,8,9] -> 3 +test4 = [1] print find_h_index(test1) print find_h_index(test2) print find_h_index(test3) +[JINZH2-M-20GQ: ~/Desktop/Python_training/Leetcode]: python find_h_index.py +mid: 3 +distance: 3 +mid: 2 +distance: 4 +4 +mid: 3 +distance: 2 +mid: 2 +distance: 3 +3 +mid: 3 +distance: 2 +mid: 2 +distance: 3 +3 + + ''' 具体思路如下,其实就是binary search。 首先根据start和end得出mid,看A[mid]值是否大于数组长度减去mid (假设A.length-mid = distance),如果是那么distance有可能成为h index. 因为数组有序,如果distance小于A[mid], From a4f62c2d67b37ede44dd5a0fff583920bb04db3c Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 20:42:29 -0700 Subject: [PATCH 52/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ogle\351\235\242\347\273\217\351\233\206\345\220\210.py" | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 664da0b..65e3d38 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -84,7 +84,8 @@ def permutation_number(s): # Best Reference: http://stackoverflow.com/questions/25285792/generate-all-permutations-of-a-list-without-adjacent-equal-elements ''' -3. find h index element in the array, at least n elements in the array larger than n, but here is no n+1 elements larger than n+1 +3. find h index element in the array, at least n elements in the array larger than n, +but here is no n+1 elements larger than n+1 ''' def find_h_index(arr): n = len(arr) - 1 @@ -164,7 +165,8 @@ def min_square(n): ''' -第二问本质上是个DP题。思路如下,要求和为n的最小平方数序列,先求出和为1到n-1的最小平方数序列。然后从1到n的平方根之间寻找和为(n-i*i)的数的最短平方序列。1 Date: Thu, 13 Aug 2015 22:01:45 -0700 Subject: [PATCH 53/57] Update Quick_Select.md --- Algorithm/Quick_Select.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Algorithm/Quick_Select.md b/Algorithm/Quick_Select.md index 2e36afb..96b4ed0 100644 --- a/Algorithm/Quick_Select.md +++ b/Algorithm/Quick_Select.md @@ -36,6 +36,9 @@ array = [12, 3, 5, 7, 4, 19, 26] print KthSmallest(array, 0, len(array)-1, 3) ``` +time complexity: +The worst case time complexity of the above solution is still O(n2). In worst case, the randomized function may always pick a corner element. The expected time complexity of above randomized QuickSelect is Θ(n) + #### Reference * [G4G set1](http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array/) * [G4G set2](http://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array-set-2-expected-linear-time/) From 5fa793d1b4ec887add10c6f9b13390bffc588420 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 23:24:32 -0700 Subject: [PATCH 54/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 65e3d38..03f5f20 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -2025,27 +2025,19 @@ def remove_helper(self, cur, val): ''' ''' -# 解法1, time complexity O(n^2) -假设队伍高度是: -数组A: 2 3 1 5 4 - -那么对应的在A之前比他高的人数是: -数组B: 0 0 2 0 1 - -那么首先把高度排序: -数组C: 5 4 3 2 1 - -然后对数组B从后往前扫 -B[4] = 1, 说明 A[4] = C[1] = 4,同时从数组C删除4,此时数组C为:5 3 2 1. -接下来: -B[3] = 0, 说明A[3] = C[0] = 5,同时从数组C删除元素5. C: 3,2, 1 -... -Finally, we could get the result [2,3,1,5,4] +# 解法1, time complexity O(nlogn) +This can be solved using rope data structure. +It's like a binary tree except that each node maintains the number of nodes in the left subtree+1 for itself. Whenever a new number is inserted, if the value is smaller than the node's number it goes to the left otherwise right. When it goes to the right, the value of the node is decreased by the value of the branch node. +Ex Nodes: 6 5 4 3 2 1 +values: 0 0 0 2 2 4 +1. Make 6 as the root of the tree, its value = 1; +2. Insert 5. Value of 5(0) < value of 6(1) so it goes to the left. New value of 6 = 2, value of 5=1; +3. Insert 4, value of 4 < value of 6 so goes to the left; again goes to the left of 5. New values of 4 = 1, value of 5 = 2, value of 6 = 3 +4. Insert 3, goes to the left of 6 but to the right of 5. New values 6 = 4, value of 3 = 1, rest unchanged +5. Insert 2, goes to the left of 6, right of 5 (value of 2 is decreased by value of 5 so its now 0), left of 3. New values of 3 = 2, value of 2 = 1, value of 6 = 5 +6. Insert 1, goes to the left of 6, right of 5, right of 3. +Do an in-order traversal of tree. It is imperative to insert the nodes in decreasing order -这个算法有个问题是,input should be -height: [5, 4, 3, 2, 1] -taller: [0, 1, 0, 0, 2] -so we do not get the input as [0 0 2 0 1], which can not use this algorithm # 解法2, check the discussion in the career cup: Input: From 4ace307bed59ee9c6ee3fea9f85170ad73c141ea Mon Sep 17 00:00:00 2001 From: UmassJin Date: Thu, 13 Aug 2015 23:28:21 -0700 Subject: [PATCH 55/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 03f5f20..778eee9 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -2073,6 +2073,29 @@ def remove_helper(self, cur, val): 15: 1 16: 3 Problem: # http://www.fgdsb.com/2015/01/03/count-numbers/ +''' +void count_number(const vector& arr) { + if(arr.empty()) return; + + int id = 0, step = 1, cur_num = arr[0], cur_count = 0; + while(id < arr.size()) { + cur_count += step; + step *= 2; + if(id + step >= arr.size() || arr[id + step] != cur_num) { + step = 1; + if(id + step < arr.size() && arr[id + step] != cur_num) { + cout << cur_num << ": " << cur_count << endl; + cur_count = 0; + cur_num = arr[id + step]; + } + } + id += step; + } + cout << cur_num << ": " << cur_count << endl << endl; +} + +''' +Count 1 bits Analysis: # https://books.google.com/books?id=iBNKMspIlqEC&pg=PA66#v=onepage&q&f=false 思想是divide and conquer, first seperate the 32 bit into 16 blocks, and each block has 2 bits then check the 1s in each block, if 2 1s, output is 10, if only has 1 1s, output is 01, From 087457691d1981ce079b8fc31994618b36611851 Mon Sep 17 00:00:00 2001 From: UmassJin Date: Fri, 14 Aug 2015 10:56:36 -0700 Subject: [PATCH 56/57] =?UTF-8?q?Update=20google=E9=9D=A2=E7=BB=8F?= =?UTF-8?q?=E9=9B=86=E5=90=88.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\347\273\217\351\233\206\345\220\210.py" | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" index 778eee9..1ec6322 100644 --- "a/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" +++ "b/Experience/google\351\235\242\347\273\217\351\233\206\345\220\210.py" @@ -5289,3 +5289,95 @@ def abbreviation_unique(array): 4. two rows array, find the minimum obstacle need to move if we want to move from the top left to right down do the in-order for the two trees, use the minimum space, less than O(n), maybe O(logn) ''' + + + +Given a list of words as input do two things, build a model which allows you to generate similar but pseudo random words based on the input corpus. + +BuildModel(String[] words); +String GeneratePseudorandomWord(); + +A → C -> E + ->P ->D + ->L + + +root +| +A (value, time, possibility) +| | | +C P L +| | | | +E P D E + | + | P + L | + | P + E + +table: +{A: {P:2, C:1, (L,1), (D,1)}, C: [E], E: {A:1, ‘end’:2}, P: [(P:1)]} +src = [(A:3),(B:2),(E:1)] +des = [(D:1)] +random(0,1) + +Input: +ACE 1/3 +EAE + +E +EA + +⅔ end, ⅓ A + +Output: +APPE +ACE + + +BuildModel(String[] words); +String GeneratePseudorandomWord(); + + +def BuildModel(words): + if not words: + return None + src = collections.defaultdict(int) + chartable = {} + for word in words: + for i, char in enumerate(word): + if i == 0: + scr[char] += 1 + if i + 1 < len(word): + if char not in chartable: + chartable[char] = collections.defaultdict(int) + chartable[char][word[i+1]] += 1 + else: + chartable[char][word[i+1]] += 1 + if i + 1 == len(word): + chartable[char][‘end’] += 1 + + +{A: {P:2, C:1, L:1, D:1}, C: [E], E: {A:1, ‘end’:2}, P: [(P:1)]} + +0.4 +A:0.1, B:0.3, C:0.6 + +import random +def GeneratePseudorandomWord(src, chartable): + randomnum = random.randint(0,1) + srcsum = sum(src.values()) + temp = {} + for item, value in src: + src[item] = operater.truediv(value/srcsum) + temp[src[item]] = item + sorted(temp) + pre = 0 + for pos, item in temp: + cur = pre + pos + if pre <= randomnum < cur: + result.append(item) + break + + + From 3a9847e0c60d887d15eb4b0d4d8ebf51e464df1b Mon Sep 17 00:00:00 2001 From: UmassJin Date: Mon, 11 Jan 2016 19:51:48 -0800 Subject: [PATCH 57/57] Create LICENSE --- LICENSE | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1fa7f95 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2014 Jin Zhu + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.