From ee01cd9446fbfd13bb6b6c60fa8b41e08b71915e Mon Sep 17 00:00:00 2001 From: Mari Wahl Date: Mon, 5 Jan 2015 22:16:56 -0500 Subject: [PATCH] simple examples, glassdoor --- .../advanced/lru_cache.py | 15 ++++++ .../glassdoor/2015/anagram_sorting.py | 11 ++++ .../glassdoor/2015/array_3_numbers.py | 33 ++++++++++++ .../glassdoor/2015/binary_search.py | 53 +++++++++++++++++++ .../glassdoor/2015/bucket.py | 19 +++++++ .../glassdoor/2015/comb.py | 11 ++++ .../glassdoor/2015/fib.py | 13 +++++ .../glassdoor/2015/long_string.py | 12 +++++ .../glassdoor/2015/longest_str.py | 11 ++++ .../glassdoor/2015/merge.py | 44 +++++++++++++++ .../glassdoor/2015/overlapping.py | 32 +++++++++++ .../glassdoor/2015/pairs.py | 41 ++++++++++++++ .../glassdoor/2015/palindrome.py | 31 +++++++++++ .../glassdoor/2015/permutations.py | 20 +++++++ .../glassdoor/2015/primes.py | 11 ++++ .../glassdoor/2015/rec_bt.py | 12 +++++ .../glassdoor/2015/smallest_bst.py | 11 ++++ .../glassdoor/2015/sum_str.py | 38 +++++++++++++ .../glassdoor/2015/tree.py | 53 +++++++++++++++++++ .../glassdoor/2015/trie.py | 12 +++++ .../trees_paths/binary_search_tree.py | 1 - .../trees_paths/binary_tree.py | 1 - .../trees_paths/check_whether_bst.py | 3 +- .../trees_paths/lowest_common_ancestor.py | 1 - .../trees_paths/transversal_BST_ancestor.py | 1 - 25 files changed, 484 insertions(+), 6 deletions(-) create mode 100644 src/extra_interview_problems/advanced/lru_cache.py create mode 100644 src/extra_interview_problems/glassdoor/2015/anagram_sorting.py create mode 100644 src/extra_interview_problems/glassdoor/2015/array_3_numbers.py create mode 100644 src/extra_interview_problems/glassdoor/2015/binary_search.py create mode 100644 src/extra_interview_problems/glassdoor/2015/bucket.py create mode 100644 src/extra_interview_problems/glassdoor/2015/comb.py create mode 100644 src/extra_interview_problems/glassdoor/2015/fib.py create mode 100644 src/extra_interview_problems/glassdoor/2015/long_string.py create mode 100644 src/extra_interview_problems/glassdoor/2015/longest_str.py create mode 100644 src/extra_interview_problems/glassdoor/2015/merge.py create mode 100644 src/extra_interview_problems/glassdoor/2015/overlapping.py create mode 100644 src/extra_interview_problems/glassdoor/2015/pairs.py create mode 100644 src/extra_interview_problems/glassdoor/2015/palindrome.py create mode 100644 src/extra_interview_problems/glassdoor/2015/permutations.py create mode 100644 src/extra_interview_problems/glassdoor/2015/primes.py create mode 100644 src/extra_interview_problems/glassdoor/2015/rec_bt.py create mode 100644 src/extra_interview_problems/glassdoor/2015/smallest_bst.py create mode 100644 src/extra_interview_problems/glassdoor/2015/sum_str.py create mode 100644 src/extra_interview_problems/glassdoor/2015/tree.py create mode 100644 src/extra_interview_problems/glassdoor/2015/trie.py diff --git a/src/extra_interview_problems/advanced/lru_cache.py b/src/extra_interview_problems/advanced/lru_cache.py new file mode 100644 index 0000000..5938ac0 --- /dev/null +++ b/src/extra_interview_problems/advanced/lru_cache.py @@ -0,0 +1,15 @@ +#!/bin/python3 + +from functools import lru_cache + + +@lru_cache(maxsize=20) +def fib(n): + if n < 2: + return n + return fib(n-1) + fib(n-2) + + +if __name__ == '__main__': + print([fib(n) for n in range(10)]) + print(fib.cache_info()) diff --git a/src/extra_interview_problems/glassdoor/2015/anagram_sorting.py b/src/extra_interview_problems/glassdoor/2015/anagram_sorting.py new file mode 100644 index 0000000..3beefd7 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/anagram_sorting.py @@ -0,0 +1,11 @@ +#!/bin/python + +''' + anagram sorting. huge list that does not fit in memory? +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/array_3_numbers.py b/src/extra_interview_problems/glassdoor/2015/array_3_numbers.py new file mode 100644 index 0000000..fa6face --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/array_3_numbers.py @@ -0,0 +1,33 @@ +#!/bin/python + +''' Determine if an Array of integers contains 3 numbers that sum to 0 ''' + +from collections import defaultdict + +def find_3_number(array): + ''' + >>> find_3_number([1,5,56,11,-3,-12]) + 1 11 -12 + True + >>> find_3_number([] ) + False + ''' + hash_ = defaultdict() + for i in array: + hash_[i] = 1 + + for i, x in enumerate(array): + for y in array[i+1:]: + if -(x+y) in hash_: + print x, y, -(x+y) + return True + + return False + + + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/binary_search.py b/src/extra_interview_problems/glassdoor/2015/binary_search.py new file mode 100644 index 0000000..10d29a4 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/binary_search.py @@ -0,0 +1,53 @@ +#!/bin/python + + +def binary_search_rec(array, item, lo=0, hi = None): + ''' + >>> binary_search_rec([2,3,5,6,8,10,15,23], 15) + (True, 6) + >>> binary_search_rec([2,3,5,6,8,10,15,23], 4) + False + ''' + hi = hi or len(array) + if hi < lo : + return False + + mid = (hi + lo)//2 + + if array[mid] == item: + return True, mid + elif array[mid] < item: + return binary_search_rec(array, item, mid + 1, hi) + else: + return binary_search_rec(array[:mid], item, lo, mid -1) + + + +def binary_search_iter(array, item): + ''' + >>> binary_search_iter([2,3,5,6,8,10,15,23], 15) + (True, 6) + >>> binary_search_iter([2,3,5,6,8,10,15,23], 4) + False + ''' + hi = len(array) + lo = 0 + + while lo < hi: + mid = (hi+lo)//2 + if array[mid] == item: + return True, mid + elif array[mid] > item: + hi = mid + else: + lo = mid + 1 + return False + + + + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/bucket.py b/src/extra_interview_problems/glassdoor/2015/bucket.py new file mode 100644 index 0000000..aab792e --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/bucket.py @@ -0,0 +1,19 @@ +#!/bin/python + +''' +Given a telephone directory which is broken in number of +files, how would you find a telephone number in it (bucket sort) + +- Quick Sort is a good choice when you have to sort a lot of elements. + When you are working with smaller collections, Bucket Sort may be a + better choice. +''' + + + + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/comb.py b/src/extra_interview_problems/glassdoor/2015/comb.py new file mode 100644 index 0000000..a43cd5b --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/comb.py @@ -0,0 +1,11 @@ +#!/bin/python + +''' Write code to generate all possible case combinations of a given +lower-cased string. (e.g. "0ab" -> ["0ab", "0aB", "0Ab", "0AB"]) +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/fib.py b/src/extra_interview_problems/glassdoor/2015/fib.py new file mode 100644 index 0000000..540e27d --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/fib.py @@ -0,0 +1,13 @@ +#!/bin/python + +''' +Write a function to find the nth Fibonacci number. +Optimize your code so that the numbers +don't have to be recalculated on consecutive function call +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/long_string.py b/src/extra_interview_problems/glassdoor/2015/long_string.py new file mode 100644 index 0000000..717d802 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/long_string.py @@ -0,0 +1,12 @@ +#!/bin/python + +''' +Given a list of strings, write a function +to calculate the longest common prefix (LCP) of all those strings. +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/longest_str.py b/src/extra_interview_problems/glassdoor/2015/longest_str.py new file mode 100644 index 0000000..d4e7f06 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/longest_str.py @@ -0,0 +1,11 @@ +#!/bin/python + +''' +given a string, find longest string with unique characters +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/merge.py b/src/extra_interview_problems/glassdoor/2015/merge.py new file mode 100644 index 0000000..1c54ea2 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/merge.py @@ -0,0 +1,44 @@ +#!/bin/python + +''' +You have two arrays with N integers in them. Merge those arrays using a +recursive algorithm so that the integers in the final array are sorted. +''' + +def merge_arrays(a1, a2): + ''' + >>> merge_arrays([5, 4, 3], [6, 2, 9]) + [2, 3, 4, 5, 6, 9] + >>> merge_arrays([2, 6], [6, 2]) + [2, 2, 6, 6] + >>> merge_arrays([], []) + [] + ''' + + a1.sort() + a2.sort() + + merge = [] + p1, p2 = 0, 0 + + while p1 < len(a1) and p2 < len(a2): + if a1[p1] <= a2[p2]: + merge.append(a1[p1]) + p1 += 1 + else: + merge.append(a2[p2]) + p2 +=1 + + if a1[p1:]: + merge.extend(a1[p1:]) + + if a2[p2:]: + merge.extend(a2[p2:]) + + return merge + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/overlapping.py b/src/extra_interview_problems/glassdoor/2015/overlapping.py new file mode 100644 index 0000000..9fe6bba --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/overlapping.py @@ -0,0 +1,32 @@ +#!/bin/python + +''' +given an array of intervals, return max number of non-overlapping intervals +''' + +from collections import defaultdict + +def non_overlapping(array): + ''' + >>> non_overlapping([(1,2), (2,5), (1, 6)]) + [[(1, 2), (2, 5)]] + >>> non_overlapping([(1,4), (2,5), (3, 6)]) + [] + ''' + total = [] + + for i, t in enumerate(array): + start = t[0] + end = t[1] + for tt in array[i+1:] : + if end <= tt[0]: + total.append([(start, end), (tt[0], tt[1])]) + + return total + + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/pairs.py b/src/extra_interview_problems/glassdoor/2015/pairs.py new file mode 100644 index 0000000..80bbf93 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/pairs.py @@ -0,0 +1,41 @@ +#!/bin/python + +''' +Given an unsorted array of numbers (that may contain repeated numbers), + * data structure that contains all the pairs w/sum equal to k. + * Do not include pairs that are the same numbers in a different order. +''' + +from collections import Counter + +def sum_pairs(array, k): + ''' + >>> sum_pairs([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 6) + set([(3, 3)]) + >>> sum_pairs([1, 4, 2, 7, 1, 3, 10, 15, 3, 1], 0) + set([]) + ''' + + results = set() + dict = Counter() + + for i in array: + dict[i] += 1 + + for i in array: + if dict[k-i] > 0: + if i == k-i and dict[k-i] > 1: + results.add((i, k-i)) + dict[k-i] -= 2 + elif i == k-i: + results.add((i, k-i)) + dict[k-i] -= 1 + + + return results + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/palindrome.py b/src/extra_interview_problems/glassdoor/2015/palindrome.py new file mode 100644 index 0000000..ab3f5dc --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/palindrome.py @@ -0,0 +1,31 @@ +#!/bin/python + +''' Determine if an Array of integers contains 3 numbers that sum to 0 ''' + +from collections import defaultdict + +def is_palindrome(array): + ''' + >>> is_palindrome('subi no onibus') + True + >>> is_palindrome('helllo there') + False + >>> is_palindrome('h') + True + >>> is_palindrome('') + True + ''' + array = array.strip(' ') + if len(array) < 2: + return True + + if array[0] == array[-1]: + return is_palindrome(array[1:-1]) + else: + return False + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/permutations.py b/src/extra_interview_problems/glassdoor/2015/permutations.py new file mode 100644 index 0000000..0714630 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/permutations.py @@ -0,0 +1,20 @@ +#!/bin/python + +''' Generate all permutations of an alphanumeric string ''' + + def get_permutations(word): + if len(word) < 2: + yield word + return + + for i in range(len(word)): + rest = word[:i] + word[i+1:] + for tail in get_permutations(rest): + yield word[i] + tail + + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/primes.py b/src/extra_interview_problems/glassdoor/2015/primes.py new file mode 100644 index 0000000..5ebafc1 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/primes.py @@ -0,0 +1,11 @@ +#!/bin/python + +''' +find prime factors of a number. +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/rec_bt.py b/src/extra_interview_problems/glassdoor/2015/rec_bt.py new file mode 100644 index 0000000..798c137 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/rec_bt.py @@ -0,0 +1,12 @@ +#!/bin/python + +''' +Reconstruct a binary tree given two sequences of node traversals, +one from inorder and one from postorder traversal. +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/smallest_bst.py b/src/extra_interview_problems/glassdoor/2015/smallest_bst.py new file mode 100644 index 0000000..9d1cf71 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/smallest_bst.py @@ -0,0 +1,11 @@ +#!/bin/python + +''' +find the smallest number in a BST +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/sum_str.py b/src/extra_interview_problems/glassdoor/2015/sum_str.py new file mode 100644 index 0000000..370af13 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/sum_str.py @@ -0,0 +1,38 @@ +#!/bin/python + +''' +find the sum of two integers represented as strings, +return the sum as string, i.e "123" and "456" would return "579" +''' + + +def get_number(s): + count = 1 + num = 0 + p = len(s) -1 + while p >= 0: + num = num + int(s[p])*count + count *= 10 + p -= 1 + return num + + +def sum_string(s1, s2): + ''' + >>> sum_string('10', '5') + '15' + >>> sum_string('0', '1') + '1' + >>> sum_string('123', '456') + '579' + ''' + + n1 = get_number(s1) + n2 = get_number(s2) + return str(n2 + n1) + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/glassdoor/2015/tree.py b/src/extra_interview_problems/glassdoor/2015/tree.py new file mode 100644 index 0000000..a6af0a6 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/tree.py @@ -0,0 +1,53 @@ +#!/bin/python + +''' +Given a tree find out whether is a BST or not +''' + +class Tree(object): + def __init__(self, value): + self.value = value + self.right = None + self.left = None + +def create_tree_bst(): + tree = Tree(4) + tree.right = Tree(6) + tree.left = Tree(2) + tree.right.left = Tree(5) + tree.right.right = Tree(7) + tree.left.left = Tree(1) + tree.left.right = Tree(3) + return tree + +def create_tree_not_bst(): + tree = Tree(4) + tree.right = Tree(6) + tree.left = Tree(2) + tree.right.left = Tree(5) + tree.right.right = Tree(7) + tree.left.left = Tree(3) + tree.left.right = Tree(1) + return tree + + +INFINITY = float("infinity") +NEG_INFINITY = float("-infinity") + +def isBST(tree, minVal=NEG_INFINITY, maxVal=INFINITY): + if not tree: + return True + + if not minVal <= tree.value <= maxVal: + return False + + return isBST(tree.left, minVal, tree.value) and \ + isBST(tree.right, tree.value, maxVal) + + +if __name__ == '__main__': + tree = create_tree_bst() + print isBST(tree) + + tree = create_tree_not_bst() + print isBST(tree) diff --git a/src/extra_interview_problems/glassdoor/2015/trie.py b/src/extra_interview_problems/glassdoor/2015/trie.py new file mode 100644 index 0000000..c8a93c3 --- /dev/null +++ b/src/extra_interview_problems/glassdoor/2015/trie.py @@ -0,0 +1,12 @@ +#!/bin/python + +''' +Implement a trie. (Write the API and code for inserting into a trie). + +''' + + +if __name__ == '__main__': + import doctest + doctest.testmod() + diff --git a/src/extra_interview_problems/trees_paths/binary_search_tree.py b/src/extra_interview_problems/trees_paths/binary_search_tree.py index 0260fa6..f3da045 100755 --- a/src/extra_interview_problems/trees_paths/binary_search_tree.py +++ b/src/extra_interview_problems/trees_paths/binary_search_tree.py @@ -1,7 +1,6 @@ #!/usr/bin/python __author__ = "bt3" -__email__ = "bt33gl@gmail.com" from binary_tree import NodeBT, BinaryTree diff --git a/src/extra_interview_problems/trees_paths/binary_tree.py b/src/extra_interview_problems/trees_paths/binary_tree.py index b7d0177..282a28a 100755 --- a/src/extra_interview_problems/trees_paths/binary_tree.py +++ b/src/extra_interview_problems/trees_paths/binary_tree.py @@ -1,7 +1,6 @@ #!/usr/bin/python __author__ = "bt3" -__email__ = "bt33gl@gmail.com" diff --git a/src/extra_interview_problems/trees_paths/check_whether_bst.py b/src/extra_interview_problems/trees_paths/check_whether_bst.py index c5279d6..7bc3eac 100755 --- a/src/extra_interview_problems/trees_paths/check_whether_bst.py +++ b/src/extra_interview_problems/trees_paths/check_whether_bst.py @@ -1,5 +1,4 @@ __author__ = "bt3" -__email__ = "bt33gl@gmail.com" """ need to keep track of min and max!!!""" @@ -38,7 +37,7 @@ def isBST(bt, mintree=None, maxtree=None): else: right = True - return left or right + return left and right diff --git a/src/extra_interview_problems/trees_paths/lowest_common_ancestor.py b/src/extra_interview_problems/trees_paths/lowest_common_ancestor.py index 7ecd178..bf5f0f2 100755 --- a/src/extra_interview_problems/trees_paths/lowest_common_ancestor.py +++ b/src/extra_interview_problems/trees_paths/lowest_common_ancestor.py @@ -1,7 +1,6 @@ #!/usr/bin/python __author__ = "bt3" -__email__ = "bt33gl@gmail.com" from binary_tree import BinaryTree diff --git a/src/extra_interview_problems/trees_paths/transversal_BST_ancestor.py b/src/extra_interview_problems/trees_paths/transversal_BST_ancestor.py index b137a48..75de17f 100755 --- a/src/extra_interview_problems/trees_paths/transversal_BST_ancestor.py +++ b/src/extra_interview_problems/trees_paths/transversal_BST_ancestor.py @@ -1,7 +1,6 @@ #!/usr/bin/python __author__ = "bt3" -__email__ = "bt33gl@gmail.com" ''' find the lowest ancestor in a BST '''