From e31b9e4d5f7518bd1f3f407e241072f1d4bb6c2e Mon Sep 17 00:00:00 2001 From: Mari Wahl Date: Tue, 6 Jan 2015 14:14:25 -0500 Subject: [PATCH] 2014 interview problems organized --- .../2014/math_arrays_and_strings/balance.txt | 2 - .../check_2_numbers_array_sum.py | 70 ++++++-------- .../check_if_anagrams.py | 51 +++++----- .../find_if_is_substr.py | 32 ++++--- .../find_largest_sum.py | 46 +++------ .../find_non_repeating_number.py | 51 ++++------ .../math_arrays_and_strings/find_sum_ints.py | 36 ------- .../get_float_rep_bin.py | 6 +- .../interserction_two_arrays.py | 96 ++++++++----------- .../max_subarray_stocks.py | 14 +-- .../math_arrays_and_strings/ransom_note.py | 12 +-- .../math_arrays_and_strings/rev_string.py | 2 +- .../math_arrays_and_strings/reverse_words.py | 6 +- .../reverse_words_sentence.py | 20 ---- 14 files changed, 159 insertions(+), 285 deletions(-) delete mode 100755 src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_sum_ints.py diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/balance.txt b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/balance.txt index 0a66673..27df907 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/balance.txt +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/balance.txt @@ -1,5 +1,3 @@ -#!/usr/bin/env python - __author__ = "bt3" diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_2_numbers_array_sum.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_2_numbers_array_sum.py index d0fb7bf..b567c79 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_2_numbers_array_sum.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_2_numbers_array_sum.py @@ -3,53 +3,43 @@ __author__ = "bt3" """ -Given an integer x and an unsorted array of integers, describe an algorithm to -determine whether two of the numbers add up to x. +Given an integer x and an unsorted array of integers, describe an +algorithm to determine whether two of the numbers add up to x. 1. Using hash tables. -2. Sorting the array and keeping two pointers in the array, one in the beginning and -one in the end. Whenever the sum of the current two integers is less than x, move the -first pointer forwards, and whenever the sum is greater than x, move the second pointer +2. Sorting the array and keeping two pointers in the array, one in +the beginning and one in the end. Whenever the sum of the current +two integers is less than x, move the first pointer forwards, and +whenever the sum is greater than x, move the second pointer backwards. O(nln n). -3. Create a BST with x minus each element in the array. Check whether any element of -the array appears in the BST. It takes O(nlog n) times two. +3. Create a BST with x minus each element in the array. +Check whether any element of the array appears in the BST. +It takes O(nlog n) times two. """ +def check_sum_hash_table(array, k): + ''' + >>> check_sum_hash_table([3, 2, 6, 7, 9, 1], 8) + [(6, 2), (1, 7)] + >>> check_sum_hash_table([5, 2, 6, 7, 9, 1], 4) + [] + >>> + ''' -def check_if_sum(arr, num): - seq = sorted(arr) - p1, p2 = 0, len(arr) - 1 - while p1 < p2 and p2 < len(arr) and p1 >= 0: - sum_here = seq[p1] + seq[p2] - if sum_here == num: - return True, seq[p1], seq[p2] - elif sum_here < num: - p1 += 1 - elif sum_here > num: - p2 -= 1 - return False + from collections import defaultdict + dict = defaultdict() + res = [] -from collections import Counter -def check_if_sum2(arr, num): - d = Counter() - for i in arr: - d[i] += 1 + for i in array: + if k-i in dict: + res.append((i, k-i)) + del dict[k-i] + else: + dict[i] = 1 - for i in arr: - other = num - i - d[i] -= 1 - if d[other] == 1: - return True, other, i - return False + return res - -arr = [3, 1, 13, 7, 2, 10] -num1 = 11 -num2 = 6 - -print(check_if_sum(arr, num1)) -print(check_if_sum(arr, num2)) -print -print(check_if_sum2(arr, num1)) -print(check_if_sum2(arr, num2)) \ No newline at end of file +if __name__ == '__main__': + import doctest + doctest.testmod() \ No newline at end of file diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_if_anagrams.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_if_anagrams.py index c7d94e5..0df881e 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_if_anagrams.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/check_if_anagrams.py @@ -3,16 +3,17 @@ __author__ = "bt3" -""" find whether two words are anagrams. Since sets do not count occurency, and sorting is O(nlogn) -we will use hash tables. We scan the first string and add all the character occurences. Then we -scan the second tring and decrease all the caracther occurences. If all the counts are zero, it is +""" find whether two words are anagrams. Since sets do not count occurrence, +and sorting is O(nlogn) we will use hash tables. We scan the first string +and add all the character occurrences. Then we scan the second trying and +decrease all the character occurrences. If all the counts are zero, it is an anagram""" -import string - +from collections import Counter def verify_two_strings_are_anagrams(str1, str2): - ana_table = {key:0 for key in string.ascii_lowercase} + + ana_table = Counter() for i in str1: ana_table[i] += 1 @@ -20,24 +21,24 @@ def verify_two_strings_are_anagrams(str1, str2): for i in str2: ana_table[i] -= 1 - if len(set(ana_table.values())) < 2: return True - else: return False + if len(set(ana_table.values())) < 2: + return True + else: + return False +''' verify if words are anagrams by comparing a sum of Unicode code +point of the character''' + +def get_unicode_sum(word): + s = 0 + for p in word: + s += ord(p) + return s -''' verify if words are anagrams by comparying hash functions''' - -def hash_func(astring, tablesize): - sump = 0 - for p in astring: - sump += ord(p) - return sump%tablesize - - -def find_anagram_hash_function(word1, word2): - tablesize = 11 - return hash_func(word1, tablesize) == hash_func(word2, tablesize) +def find_anagram_get_unicode(word1, word2): + return get_unicode_sum(word1) == get_unicode_sum(word2) @@ -47,10 +48,8 @@ if __name__ == '__main__': str2 = 'aniram' str3 = 'anfaam' - print verify_two_strings_are_anagrams(str1, str2) - print verify_two_strings_are_anagrams(str1, str3) - print - print find_anagram_hash_function(str1, str2) - print find_anagram_hash_function(str1, str3) - + assert(verify_two_strings_are_anagrams(str1, str2) == True) + assert(verify_two_strings_are_anagrams(str1, str3) == False) + assert(find_anagram_get_unicode(str1, str2) == True) + assert(find_anagram_get_unicode(str1, str3) == False) diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_is_substr.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_is_substr.py index b28cfe7..ff3e2ad 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_is_substr.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_if_is_substr.py @@ -3,25 +3,27 @@ __author__ = "bt3" -def isSubstr(s1, s2): - if s1 in s2 or s2 in s1: return True - return False - - def find_substr(s1, s2): - pl, ps = 0, 0 - if len(s1) > len(s2): - larger, smaller = s1, s2 + + if len(s1) < len(s2): + bs = s2 + ss = s1 else: - larger, smaller = s2, s1 - while ps < len(smaller) and pl < len(larger): - if larger[pl] == smaller[ps]: + bs = s1 + ss = s2 + + ps = 0 + + for c in bs: + + if ss[ps] == c: ps += 1 else: ps = 0 - pl += 1 - if ps == len(smaller): + + if ps == len(ss)-1: return True + return False @@ -30,5 +32,5 @@ if __name__ == '__main__': s1 = 'buffy is a vampire slayer' s2 = 'vampire' s3 = 'angel' - print find_substr(s2, s1) - print find_substr(s3, s1) + assert(find_substr(s2, s1) == True) + assert(find_substr(s3, s1) == False) diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_largest_sum.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_largest_sum.py index a26e787..9b79ac8 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_largest_sum.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_largest_sum.py @@ -4,8 +4,10 @@ __author__ = "bt3" -'''You are given an array of integers (both positive and negative). Find the contiguous -sequence with the largest sum. Return the sum.''' +''' + You are given an array of integers (both positive and negative). + Find the contiguous sequence with the largest sum. +''' def find_largest_sum(array): @@ -17,43 +19,21 @@ def find_largest_sum(array): >>> find_largest_sum([1]) 1 ''' - if not array: return 0 - p1, p2 = 0, 1 - all_sums = set() - sum_ = array[p1] - while p1 < p2 and p2 < len(array): - if sum_ + array[p2] < 0: - all_sums.add(sum_) - p2 += 1 - p1 = p2 - sum_ = 0 - sum_ += array[p2] - p2 += 1 - all_sums.add(sum_) - return max(all_sums) + sum_ = 0 + sum_here = 0 + for i in array: + sum_here += i -def find_largest_sum_simple(array): - ''' - >>> find_largest_sum_simple([-1, 2, -3, 5, 3, 1, -16, 7, 1, -13, 1]) - 9 - >>> find_largest_sum_simple([]) - 0 - >>> find_largest_sum_simple([1]) - 1 - ''' - max_sum, sum_ = 0, 0 + if sum_here < 0: + sum_here = 0 - for item in array: - sum_ += item - if max_sum < sum_: - max_sum = sum_ - elif sum_ < 0: - sum_ = 0 - return max_sum + if sum_ < sum_here: + sum_ = sum_here + return sum_ diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_non_repeating_number.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_non_repeating_number.py index 3c2d932..8d1dc14 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_non_repeating_number.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_non_repeating_number.py @@ -6,43 +6,28 @@ __author__ = "bt3" from collections import defaultdict -def find_unique_number(arr): - table = defaultdict(int) - total = 0 - for i in arr: - if table[i]: - total -= i +def find_unique_number(array): + ''' + >>> find_unique_number([1, 3, 6, 1, 5, 6, 9, 3, 7]) + [1, 6, 3] + >>> find_unique_number([1, 3, 5, 6, 9, 7]) + [] + ''' + + table = defaultdict() + total = [] + + for i in array: + if i in table: + total.append(i) else: - total += i - table[i] +=1 + table[i] = 1 + return total -def find_unique_number_xor(arr): - xor = 0 - for item in arr: - xor ^= item - return xor - - -def find_unique_char(s): - if len(s) < 2: return True - for i, c in enumerate(s): - for j in s[i+1:]: - if j == c: - return False - return True - if __name__ == '__main__': - arr = [1, 3, 5, 6, 1, 5, 6, 3, 7,] - print(find_unique_number(arr)) - print(find_unique_number_xor(arr)) - s1 = 'abcdefg' - s2 = 'buffy' - s3 = '' - print - print(find_unique_char(s1)) - print(find_unique_char(s2)) - print(find_unique_char(s3)) + import doctest + doctest.testmod() \ No newline at end of file diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_sum_ints.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_sum_ints.py deleted file mode 100755 index 85239bf..0000000 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/find_sum_ints.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python - -__author__ = "bt3" - - - -from collections import defaultdict - -def find_ints_sum(array, sum_): - ''' - >>> find_ints_sum([4, 5, 3, 5, 2, 8, 1], 9) - set([(5, 4), (1, 8)]) - >>> find_ints_sum([1, 2], 5) - set([]) - >>> find_ints_sum([1], 1) - 'Not enough values' - ''' - if len(array)< 2: - return "Not enough values" - - dict = defaultdict() - pairs = set() - - for i in array: - if (sum_ - i) in dict: - pairs.add((i, sum_ -i)) - else: - dict[i] = True - - return pairs - -# if the array was sorted, we could do this with two pointers - -if __name__ == '__main__': - import doctest - doctest.testmod() \ No newline at end of file diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/get_float_rep_bin.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/get_float_rep_bin.py index 5f037dd..156cb77 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/get_float_rep_bin.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/get_float_rep_bin.py @@ -3,9 +3,9 @@ __author__ = "bt3" -''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary - representation. If the Number cannot be represented accurately in binary, with at - most 32 chars, print error: +''' Given a real number between 0 and 1 (eg: 0.72), this method print the + binary representation. If the Number cannot be represented accurately + in binary, with at exit most 32 chars, print error: ''' def get_float_rep(num): diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/interserction_two_arrays.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/interserction_two_arrays.py index 5079030..b0868ef 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/interserction_two_arrays.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/interserction_two_arrays.py @@ -3,77 +3,61 @@ __author__ = "bt3" - -''' using sets ''' - def intersection_two_arrays_sets(seq1, seq2): - ''' find the intersection of two arrays using set proprieties ''' + ''' + >>> intersection_two_arrays_sets([1,2,3,5,7,8], [3,5,6]) + set([3, 5]) + >>> intersection_two_arrays_sets([1,2,7,8], [3,5,6]) + set([]) + ''' + # O(n) set1 = set(seq1) set2 = set(seq2) - return set1.intersection(set2) #same as list(set1 & set2 + + return set1.intersection(set2) #same as list(set1 & set2) +def intersection_two_arrays_On2(seq1, seq2): + ''' + >>> intersection_two_arrays_On2([1,2,3,5,7,8], [3,5,6]) + [3, 5] + >>> intersection_two_arrays_On2([1,2,7,8], [3,5,6]) + [] + ''' -''' using merge sort ''' + final = [] + + for i in seq1: + for j in seq2: + if i == j: + final.append(i) + + return final + + +def intersection_two_arrays_On(seq1, seq2): + ''' + >>> intersection_two_arrays_On([1,2,3,5,7,8], [3,5,6]) + [5, 3] + >>> intersection_two_arrays_On([1,2,7,8], [3,5,6]) + [] + ''' + + final = [] -def intersection_two_arrays_ms(seq1, seq2): - ''' find the intersection of two arrays using merge sort ''' - res = [] while seq1 and seq2: + if seq1[-1] == seq2[-1]: - res.append(seq1.pop()) + final.append(seq1.pop()) seq2.pop() elif seq1[-1] > seq2[-1]: seq1.pop() else: seq2.pop() - res.reverse() - return res - - - -''' using binary search ''' - -def binary_search(seq, key, lo=0, hi=None): - ''' binary search iterative algorithm ''' - hi = hi or len(seq) - while lo < hi: - mid = (hi+lo) // 2 - if seq[mid] == key: - return True - elif key > seq[mid]: - lo = mid + 1 - else: - hi = mid - return None - -def intersection_two_arrays_bs(seq1, seq2): - ''' if A small and B is too large, we can do a binary search on each entry in B ''' - ''' only works if sorted and the small sequence has not larger nmbers!!!''' - if len(seq1) > len(seq2): seq, key = seq1, seq2 - else: seq, key = seq2, seq1 - - intersec = [] - for item in key: - if binary_search(seq, item): - intersec.append(item) - return intersec - - - -def test_intersection_two_arrays(module_name='this module'): - seq1 = [1,2,3,5,7,8] - seq2 = [3,5,6] - - assert(set(intersection_two_arrays_sets(seq1,seq2)) == set([3,5])) - assert(intersection_two_arrays_bs(seq1,seq2) == [3,5]) - assert(intersection_two_arrays_ms(seq1,seq2) == [3,5]) - - s = 'Tests in {name} have {con}!' - print(s.format(name=module_name, con='passed')) + return final if __name__ == '__main__': - test_intersection_two_arrays() - + import doctest + doctest.testmod() diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/max_subarray_stocks.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/max_subarray_stocks.py index f79e87d..61d2957 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/max_subarray_stocks.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/max_subarray_stocks.py @@ -25,13 +25,7 @@ def beating_stock(array): - - - - - - - -array = [7, 2, 3, 6, 5, 8, 5, 3, 4] -print(array) -print("The best profit is %d, buying at %d, selling at %d." %(beating_stock(array))) \ No newline at end of file +if __name__ == '__main__': + array = [7, 2, 3, 6, 5, 8, 5, 3, 4] + print(array) + print("Profit: %d, buying at %d, selling at %d." %(beating_stock(array))) \ No newline at end of file diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/ransom_note.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/ransom_note.py index 14cc1db..75a07f8 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/ransom_note.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/ransom_note.py @@ -24,11 +24,11 @@ def check_if_ransom_note(magazines, note): +if __name__ == '__main__': + magazines1 = "avfegthhgrebvkdsvnijnvyijfdmckdsmovkmmfvskumvl;cdkmioswckofjbkreenyukjemjgnmkmvkmnvdkmvkr g gmvdvmldm vldfkmbldkmlvdkm" + magazines2 = "adfsfa" + note = "you should disobey" -magazines1 = "avfegthhgrebvkdsvnijnvyijfdmckdsmovkmmfvskumvl;cdkmioswckofjbkreenyukjemjgnmkmvkmnvdkmvkr g gmvdvmldm vldfkmbldkmlvdkm" -magazines2 = "adfsfa" -note = "you should disobey" - -print(check_if_ransom_note(magazines1, note)) -print(check_if_ransom_note(magazines2, note)) + print(check_if_ransom_note(magazines1, note)) + print(check_if_ransom_note(magazines2, note)) diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/rev_string.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/rev_string.py index 64b09ce..0079aaf 100644 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/rev_string.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/rev_string.py @@ -12,5 +12,5 @@ def revert(string): if __name__ == '__main__': - string = "Google is fun!" + string = "buffy is fun!" print(revert(string)) \ No newline at end of file diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words.py index 593a6d9..573c368 100644 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words.py @@ -6,8 +6,8 @@ __author__ = "bt3" def invert_word(word): """ - >>> invert_word('stripe is awesome') - 'awesome is stripe' + >>> invert_word('buffy is awesome') + 'awesome is buffy' """ new_word = [] @@ -21,5 +21,3 @@ if __name__ == '__main__': import doctest doctest.testmod() - #word = 'stripe is awesome' - #print invert_word(word) diff --git a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words_sentence.py b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words_sentence.py index 7064468..f3a6183 100755 --- a/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words_sentence.py +++ b/src/extra_interview_problems/glassdoor/2014/math_arrays_and_strings/reverse_words_sentence.py @@ -12,25 +12,6 @@ def reversing_words_setence_py2(s): words.reverse() return ' '.join(words) -def reversing_words_setence_py3(s): - p1 = 0 - word = '' - arr = [] - while p1 < len(s): - if s[p1] != ' ': - word += s[p1] - else: - arr.append(word) - word = '' - p1 += 1 - - arr.append(word) - new = '' - while arr: - new += arr.pop() - new += ' ' - - return new @@ -39,6 +20,5 @@ if __name__ == '__main__': s = "Buffy is a Vampire Slayer" print reversing_words_setence_py(s) print reversing_words_setence_py2(s) - print reversing_words_setence_py3(s)