From 25dc9663ad90861a3de2e1f08502124ab5fca8b3 Mon Sep 17 00:00:00 2001 From: louietyj Date: Sat, 21 Oct 2017 01:20:43 +0800 Subject: [PATCH 1/4] Add min-heap (#68) * Min-heap implementation * Heap test cases --- utilities/python/heap.py | 84 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 utilities/python/heap.py diff --git a/utilities/python/heap.py b/utilities/python/heap.py new file mode 100644 index 00000000..4bbb926f --- /dev/null +++ b/utilities/python/heap.py @@ -0,0 +1,84 @@ +# Implements a min-heap. For max-heap, simply reverse all comparison orders. +# +# Note on alternate subroutine namings (used in some textbooks): +# - _bubble_up = siftdown +# - _bubble_down = siftup + +def _bubble_up(heap, i): + while i > 0: + parent_i = (i - 1) // 2 + if heap[i] < heap[parent_i]: + heap[i], heap[parent_i] = heap[parent_i], heap[i] + i = parent_i + continue + break + +def _bubble_down(heap, i): + startpos = i + newitem = heap[i] + left_i = 2 * i + 1 + while left_i < len(heap): + # Pick the smaller of the L and R children + right_i = left_i + 1 + if right_i < len(heap) and not heap[left_i] < heap[right_i]: + child_i = right_i + else: + child_i = left_i + + # Break if heap invariant satisfied + if heap[i] < heap[child_i]: + break + + # Move the smaller child up. + heap[i], heap[child_i] = heap[child_i], heap[i] + i = child_i + left_i = 2 * i + 1 + +def heapify(lst): + for i in reversed(range(len(lst) // 2)): + _bubble_down(lst, i) + +def heappush(heap, item): + heap.append(item) + _bubble_up(heap, len(heap) - 1) + +def heappop(heap): + if len(heap) == 1: + return heap.pop() + min_value = heap[0] + heap[0] = heap[-1] + del heap[-1] + _bubble_down(heap, 0) + return min_value + + + +# Example usage +heap = [3, 2, 1, 0] +heapify(heap) +print('Heap(0, 1, 2, 3):', heap) +heappush(heap, 4) +heappush(heap, 7) +heappush(heap, 6) +heappush(heap, 5) +print('Heap(0, 1, 2, 3, 4, 5, 6, 7):', heap) + +sorted_list = [heappop(heap) for _ in range(8)] +print('Heap-sorted list:', sorted_list) + +# Large test case, for randomized tests +import random + +# Heapify 0 ~ 99 +heap = list(range(100)) +random.shuffle(heap) +heapify(heap) + +# Push 100 ~ 199 in random order +new_elems = list(range(100, 200)) +random.shuffle(new_elems) +for elem in new_elems: + heappush(heap, elem) + +sorted_list = [heappop(heap) for _ in range(200)] +print(sorted_list == sorted(sorted_list)) From d522c4cceb5bf6fafffb569875b140b4ae570eee Mon Sep 17 00:00:00 2001 From: Bingxu Ren Date: Fri, 20 Oct 2017 12:21:40 -0500 Subject: [PATCH 2/4] Add some OOP questions (#67) * added some oop questions * Update oop.md --- algorithms/oop.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/algorithms/oop.md b/algorithms/oop.md index 54c32a4c..0df9cc2d 100644 --- a/algorithms/oop.md +++ b/algorithms/oop.md @@ -2,3 +2,7 @@ Object-Oriented Programming == - How would you design a chess game? What classes and objects would you use? What methods would they have? +- How would you design the data structures for a book keeping system for a library? +- Explain how you would design a HTTP server? Give examples of classes, methods, and interfaces. What are the challenges here? +- Discuss algorithms and data structures for a garbage collector? +- How would you implement an HR system to keep track of employee salaries and benefits? From 97d3d4d7f4bd3330be93326db27d2ffe8f565da5 Mon Sep 17 00:00:00 2001 From: Brandon <3676319+sibeitokgong@users.noreply.github.com> Date: Sat, 21 Oct 2017 01:42:16 +0800 Subject: [PATCH 3/4] Remove link with SSL error (#70) --- preparing/cheatsheet.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/preparing/cheatsheet.md b/preparing/cheatsheet.md index c12bd180..c750f0ff 100644 --- a/preparing/cheatsheet.md +++ b/preparing/cheatsheet.md @@ -101,7 +101,3 @@ For a detailed walkthrough of interview preparation, refer to the ["Preparing fo |-|-| |✅|Record the interview questions and answers down as these can be useful for future reference.| |⚠️|Send a follow up email to your interviewer(s) thanking them for their time and the opportunity to interview with them.| - -###### References - -- [Preparing for a Facebook/Google Software Engineer Interview](https://orrsella.com/2016/05/14/preparing-for-a-facebook-google-software-engineer-interview/) From c16c7fcf4720fddd35fa3184695a69ddbbc81d01 Mon Sep 17 00:00:00 2001 From: Brandon <3676319+sibeitokgong@users.noreply.github.com> Date: Sat, 21 Oct 2017 01:42:29 +0800 Subject: [PATCH 4/4] Remove broken link in front-end/design (#69) --- front-end/design.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/front-end/design.md b/front-end/design.md index 38b58ef2..cef9c972 100644 --- a/front-end/design.md +++ b/front-end/design.md @@ -12,7 +12,3 @@ Talk me through a full stack implementation of an autocomplete widget. A user ca - What does the backend API look like? - What perf considerations are there for complete-as-you-type behavior? Are there any edge cases (for example, if the user types fast and the network is slow)? - How would you design the network stack and backend in support of fast performance: how do your client/server communicate? How is your data stored on the backend? How do these approaches scale to lots of data and lots of clients? - -###### References - -- https://performancejs.com/post/hde6d32/The-Best-Frontend-JavaScript-Interview-Questions-%28written-by-a-Frontend-Engineer%29