diff --git a/10-Mock Interviews/01-Large E-Commerce Company/01-On-Site-Question-1/01-On-Site Question 1 - SOLUTION.ipynb b/10-Mock Interviews/01-Large E-Commerce Company/01-On-Site-Question-1/01-On-Site Question 1 - SOLUTION.ipynb deleted file mode 100644 index a1f784f..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/01-On-Site-Question-1/01-On-Site Question 1 - SOLUTION.ipynb +++ /dev/null @@ -1,222 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** You've been given a list of historical stock prices for a single day for Amazon stock. The index of the list represents the timestamp, so the element at index of 0 is the initial price of the stock, the element at index 1 is the next recorded price of the stock for that day, etc. Your task is to write a function that will return the maximum profit possible from the purchase and sale of a single share of Amazon stock on that day. Keep in mind to try to make this as efficient as possible.**\n", - "\n", - "\n", - "For example, if you were given the list of stock prices:\n", - "\n", - "prices = [12,11,15,3,10]\n", - "\n", - "Then your function would return the maximum possible profit, which would be 7 (buying at 3 and selling at 10).\n", - "\n", - "## Requirements\n", - "\n", - "** Try to solve this problem with paper/pencil first without using an IDE. Also keep in mind you should be able to come up with a better solution than just brute forcing every possible sale combination **\n", - "\n", - "** Also you can't \"short\" a stock, you must buy *before* you sell the stock. **" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Solution\n", - "\n", - "Let's think about a few things before we start coding. One thing to think about right off the bat is that we can't just find the maximum price and the lowest price and then subtract the two, because the max could come before the min.\n", - "\n", - "The brute force method would be to try every possible pair of price combinations, but this would be O(N^2), pretty bad. Also since this is an interview setting you should probably already know that there is a smarter solution.\n", - "\n", - "In this case we will use a [greedy algorithm](https://en.wikipedia.org/wiki/Greedy_algorithm) approach. We will iterate through the list of stock prices while keeping track of our maximum profit.\n", - "\n", - "That means for every price we will keep track of the lowest price so far and then check if we can get a better profit than our current max.\n", - "\n", - "Let's see an implementation of this:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "def profit(stock_prices):\n", - " \n", - " # Start minimum price marker at first price\n", - " min_stock_price = stock_prices[0]\n", - " \n", - " # Start off with a profit of zero\n", - " max_profit = 0\n", - " \n", - " for price in stock_prices:\n", - " \n", - " # Check to set the lowest stock price so far\n", - " min_stock_price = min(min_stock_price,price)\n", - " \n", - " # Check the current price against our minimum for a profit \n", - " # comparison against the max_profit\n", - " comparison_profit = price - min_stock_price\n", - " \n", - " # Compare against our max_profit so far\n", - " max_profit = max(max_profit,comparison_profit)\n", - " \n", - " return max_profit" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "39" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "profit([10,12,14,12,13,11,8,7,6,13,23,45,11,10])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Currently we're finding the max profit in one pass O(n) and in constant space O(1). However, we still aren't thinking about any edge cases. For example, we need to address the following scenarios:\n", - "\n", - "* Stock price always goes down\n", - "* If there's less than two stock prices in the list.\n", - "\n", - "We can take care of the first scenario by returning a negative profit if the price decreases all day (that way we can know how much we lost). And the second issue can be solved with a quick **len()** check. Let's see the full solution:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "def profit2(stock_prices):\n", - " \n", - " # Check length\n", - " if len(stock_prices) < 2:\n", - " raise Exception('Need at least two stock prices!')\n", - " \n", - " # Start minimum price marker at first price\n", - " min_stock_price = stock_prices[0]\n", - " \n", - " # Start off with an initial max profit\n", - " max_profit = stock_prices[1] - stock_prices[0]\n", - " \n", - " # Skip first index of 0\n", - " for price in stock_prices[1:]:\n", - " \n", - " \n", - " # NOTE THE REORDERING HERE DUE TO THE NEGATIVE PROFIT TRACKING\n", - " \n", - " # Check the current price against our minimum for a profit \n", - " # comparison against the max_profit\n", - " comparison_profit = price - min_stock_price\n", - " \n", - " # Compare against our max_profit so far\n", - " max_profit = max(max_profit,comparison_profit)\n", - " \n", - " # Check to set the lowest stock price so far\n", - " min_stock_price = min(min_stock_price,price)\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " return max_profit" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "ename": "Exception", - "evalue": "Need at least two stock prices!", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Exception Raised\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprofit2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mprofit2\u001b[0;34m(stock_prices)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# Check length\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstock_prices\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Need at least two stock prices!'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;31m# Start minimum price marker at first price\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mException\u001b[0m: Need at least two stock prices!" - ] - } - ], - "source": [ - "# Exception Raised\n", - "profit2([1])" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "-1" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "profit2([30,22,21,5])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Great! Now we can prepare for worst case scenarios. Its important to keep edge cases in mind, especially if you are able to solve the original question fairly quickly.\n", - "\n", - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/01-Large E-Commerce Company/01-On-Site-Question-1/01-On-Site Question 1.ipynb b/10-Mock Interviews/01-Large E-Commerce Company/01-On-Site-Question-1/01-On-Site Question 1.ipynb deleted file mode 100644 index 0726438..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/01-On-Site-Question-1/01-On-Site Question 1.ipynb +++ /dev/null @@ -1,56 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 \n", - "\n", - "## Problem\n", - "\n", - "** You've been given a list of historical stock prices for a single day for Amazon stock. The index of the list represents the timestamp, so the element at index of 0 is the initial price of the stock, the element at index 1 is the next recorded price of the stock for that day, etc. Your task is to write a function that will return the maximum profit possible from the purchase and sale of a single share of Amazon stock on that day. Keep in mind to try to make this as efficient as possible.**\n", - "\n", - "\n", - "For example, if you were given the list of stock prices:\n", - "\n", - "prices = [12,11,15,3,10]\n", - "\n", - "Then your function would return the maximum possible profit, which would be 7 (buying at 3 and selling at 10).\n", - "\n", - "## Requirements\n", - "\n", - "** Try to solve this problem with paper/pencil first without using an IDE. Also keep in mind you should be able to come up with a better solution than just brute forcing every possible sale combination **\n", - "\n", - "** Also you can't \"short\" a stock, you must buy *before* you sell the stock. **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/01-Large E-Commerce Company/02-On-Site-Question-2/02-On-Site Question 2 - SOLUTION.ipynb b/10-Mock Interviews/01-Large E-Commerce Company/02-On-Site-Question-2/02-On-Site Question 2 - SOLUTION.ipynb deleted file mode 100644 index ded3038..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/02-On-Site-Question-2/02-On-Site Question 2 - SOLUTION.ipynb +++ /dev/null @@ -1,158 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** Given a list of integers, write a function that will return a list, in which for each index the element will be the product of all the integers except for the element at that index **\n", - "\n", - "**For example, an input of [1,2,3,4] would return [24,12,8,6] by performing [2×3×4,1×3×4,1×2×4,1×2×3] **\n", - "\n", - "## Requirements\n", - "\n", - "** You can not use division in your answer! Meaning you can't simply multiply all the numbers and then divide by eahc element for each index!**\n", - "\n", - "** Try to do this on a white board or with paper/pencil.**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "___\n", - "## Solution\n", - "\n", - "If you look at the list above with the multiplication you'll notice we are repeating multiplications, such as 2 times 3 or 3 times 4 for multiple entries in the new list. \n", - "\n", - "We'll want to take a greedy approach and keep track of these results for later re-use at other indices. We'll also need to think about what if a number is zero!\n", - "\n", - "In order to find the products of all the integers (except for the integer at that index) we will actually go through our list twice in a greedy fashion. \n", - "\n", - "On the first pass we will get the products of all the integers **before** each index, and then on the second pass we will go **backwards** to get the products of all the integers after each index.\n", - "\n", - "Then we just need to multiply all the products before and after each index in order to get the final product answer!\n", - "\n", - "Let's see this in action:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "def index_prod(lst):\n", - " \n", - " # Create an empty output list\n", - " output = [None] * len(lst)\n", - " \n", - " # Set initial product and index for greedy run forward\n", - " product = 1\n", - " i = 0\n", - " \n", - " while i < len(lst):\n", - " \n", - " # Set index as cumulative product\n", - " output[i] = product\n", - " \n", - " # Cumulative product\n", - " product *= lst[i]\n", - " \n", - " # Move forward\n", - " i +=1\n", - " \n", - " \n", - " # Now for our Greedy run Backwards\n", - " product = 1\n", - " \n", - " # Start index at last (taking into account index 0)\n", - " i = len(lst) - 1\n", - " \n", - " # Until the beginning of the list\n", - " while i >=0:\n", - " \n", - " # Same operations as before, just backwards\n", - " output[i] *= product\n", - " product *= lst[i]\n", - " i -= 1\n", - " \n", - " return output " - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[24, 12, 8, 6]" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "index_prod([1,2,3,4])" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[24, 0, 0, 0, 0]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "index_prod([0,1,2,3,4])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Review the solution and make sure you understand it! It uses O(n) time and O(n) space complexity!\n", - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/01-Large E-Commerce Company/02-On-Site-Question-2/02-On-Site Question 2 .ipynb b/10-Mock Interviews/01-Large E-Commerce Company/02-On-Site-Question-2/02-On-Site Question 2 .ipynb deleted file mode 100644 index de447f1..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/02-On-Site-Question-2/02-On-Site Question 2 .ipynb +++ /dev/null @@ -1,51 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 \n", - "\n", - "## Problem\n", - "\n", - "** Given a list of integers, write a function that will return a list, in which for each index the element will be the product of all the integers except for the element at that index **\n", - "\n", - "**For example, an input of [1,2,3,4] would return [24,12,8,6] by performing [2×3×4,1×3×4,1×2×4,1×2×3] **\n", - "\n", - "## Requirements\n", - "\n", - "** You can not use division in your answer! Meaning you can't simply multiply all the numbers and then divide by eahc element for each index!**\n", - "\n", - "** Try to do this on a white board or with paper/pencil.**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/01-Large E-Commerce Company/03-On-Site-Question-3/03-On-Site Question 3 - SOLUTION.ipynb b/10-Mock Interviews/01-Large E-Commerce Company/03-On-Site-Question-3/03-On-Site Question 3 - SOLUTION.ipynb deleted file mode 100644 index 79c21d9..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/03-On-Site-Question-3/03-On-Site Question 3 - SOLUTION.ipynb +++ /dev/null @@ -1,188 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "**Given two rectangles, determine if they overlap. The rectangles are defined as a Dictionary, for example:**" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "r1 = {\n", - " \n", - " # x and y coordinates of the bottom-left corner of the rectangle\n", - " 'x': 2 , 'y': 4,\n", - " \n", - " # Width and Height of rectangle\n", - " 'w':5,'h':12}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "** If the rectangles do overlap, return the dictionary which describes the overlapping section**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Requirements\n", - "\n", - "** Make sure the dictionary you output is in the same form as the input.**\n", - "\n", - "** Feel free to use an IDE for the code, but make sure you use paper/pencil or whiteboard to draw out your plan and logic**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Solution\n", - "\n", - "This is a problem where it helps a lot to draw out your thinking. There are a few things we will need to think about:\n", - "\n", - "* How can we determine an intersection?\n", - "* What if a rectangle is fully inside another rectangle?\n", - "* What if there is no intersection, but the rectangles share an edge?\n", - "\n", - "The key to solving this problem is to *break it up in to sub-problems*. We can split up the problem into an x-axis problem and a y-axis problem. \n", - "\n", - "We will create a function that can detect overlap in 1 dimension. Then we will split the rectangles into x and width, and y and height components. We can then determine that if there is overlap on both dimensions, then the rectangles themselves intersect!\n", - "\n", - "In order to understand the **calc_overlap** function, draw out two flat lines and follow along with the function and notice how it detects an overlap!\n", - "\n", - "Let's begin by creating a general function to detect overlap in a single dimension:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "def calc_overlap(coor1,dim1,coor2,dim2):\n", - " \"\"\"\n", - " Takes in 2 coordinates and their length in that dimension\n", - " \"\"\"\n", - " \n", - " # Find greater of the two coordinates\n", - " # (this is either the point to the most right\n", - " # or the higher point, depending on the dimension)\n", - " \n", - " # The greater point would be the start of the overlap\n", - " greater = max(coor1,coor2)\n", - " \n", - " # The lower point is the end of the overlap\n", - " lower = min(coor1+dim1,coor2+dim2)\n", - " \n", - " # Return a tuple of Nones if there is no overlap\n", - " \n", - " if greater >= lower:\n", - " return (None,None)\n", - " \n", - " # Otherwise, get the overlap length\n", - " overlap = lower-greater\n", - " \n", - " return (greater,overlap)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now let's use this function to detect if the rectangles overlap!" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "def calc_rect_overlap(r1,r2):\n", - " \n", - " \n", - " x_overlap, w_overlap = calc_overlap(r1['x'],r1['w'],r2['x'],r2['w'])\n", - " \n", - " y_overlap, h_overlap = calc_overlap(r1['y'],r1['h'],r2['y'],r2['h'])\n", - " \n", - " # If either returned None tuples, then there is no overlap!\n", - " if not w_overlap or not h_overlap:\n", - " print('There was no overlap!')\n", - " return None\n", - " \n", - " # Otherwise return the dictionary format of the overlapping rectangle\n", - " return { 'x':x_overlap,'y': y_overlap,'w':w_overlap,'h':h_overlap}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Our solution is O(1) for both time and space! Let's see it in action:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'x': 2, 'y': 5, 'w': 5, 'h': 11}" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "r1 = {'x': 2 , 'y': 4,'w':5,'h':12}\n", - "r2 = {'x': 1 , 'y': 5,'w':7,'h':14}\n", - "calc_rect_overlap(r1,r2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Make sure to review the answer and practice writing it out by hand!\n", - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/01-Large E-Commerce Company/03-On-Site-Question-3/03-On-Site Question 3.ipynb b/10-Mock Interviews/01-Large E-Commerce Company/03-On-Site-Question-3/03-On-Site Question 3.ipynb deleted file mode 100644 index 43c2cd7..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/03-On-Site-Question-3/03-On-Site Question 3.ipynb +++ /dev/null @@ -1,76 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3\n", - "\n", - "## Problem\n", - "\n", - "**Given two rectangles, determine if they overlap. The rectangles are defined as a Dictionary, for example:**" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "r1 = {\n", - " \n", - " # x and y coordinates of the bottom-left corner of the rectangle\n", - " 'x': 2 , 'y': 4,\n", - " \n", - " # Width and Height of rectangle\n", - " 'w':5,'h':12}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "** If the rectangles do overlap, return the dictionary which describes the overlapping section**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Requirements\n", - "\n", - "** Make sure the dictionary you output is in the same form as the input.**\n", - "\n", - "** Feel free to use an IDE for the code, but make sure you use paper/pencil or whiteboard to draw out your plan and logic**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/01-Large E-Commerce Company/04-Phone-Screen/04-Phone Screen - SOLUTION.ipynb b/10-Mock Interviews/01-Large E-Commerce Company/04-Phone-Screen/04-Phone Screen - SOLUTION.ipynb deleted file mode 100644 index 5f192ce..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/04-Phone-Screen/04-Phone Screen - SOLUTION.ipynb +++ /dev/null @@ -1,85 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Phone Screen - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "**A tower has 100 floors. You've been given two eggs. The eggs are strong enough that they can be dropped from a particular floor in the tower without breaking. You've been tasked to find the highest floor an egg can be dropped without breaking, in as few drops as possible. If an egg is dropped from above its target floor it will break. If it is dropped from that floor or below, it will be intact and you can test drop the egg again on another floor.**\n", - "\n", - "**Show algorithmically how you would go about doing this in as few drops as possible**\n", - "## Requirements\n", - "\n", - "** Use paper/pencil or a whiteboard for this problem **" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Solution\n", - "\n", - "** We've already seen this problem in the Riddles section, here is the answer from that section.(Alternatively just google \"2 eggs 100 floors\" for a plethora of explanations regarding this same solution **\n", - "\n", - "\n", - "Start from the 10th floor and go up to floors in multiples of 10.\n", - "\n", - "If first egg breaks, say at 20th floor then you can check all the floors between 11th and 19th with the second egg to see which floor it will not break.\n", - "\n", - "In this case, the worst-case number of drops is 19. If the threshold was 99th floor, then you would have to drop the first egg 10 times and the second egg 9 times in linear fashion.\n", - "\n", - "**Best solution:**\n", - "We need to minimize this worst-case number of drops. For that, we need to generalize the problem to have n floors. What would be the step value, for the first egg? Would it still be 10? Suppose we have 200 floors. Would the step value be still 10? \n", - "\n", - "The point to note here is that we are trying to minimize the worst-case number of drops which happens if the threshold is at the highest floors. So, our steps should be of some value which reduces the number of drops of the first egg.\n", - "\n", - "Let's assume we take some step value m initially. If every subsequent step is m-1,\n", - "then, \n", - "$$m+m−1+m−2+.....+1=n$$\n", - "\n", - "This is \n", - "\n", - "$$\\frac{m∗(m+1)}{2}=n$$\n", - "\n", - "If n =100, then m would be 13.65 which since we can't drop from a decimal of a floor, we actually use 14.\n", - "\n", - "So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14.\n", - "\n", - "Note that this is simply a binary search!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/01-Large E-Commerce Company/04-Phone-Screen/04-Phone Screen .ipynb b/10-Mock Interviews/01-Large E-Commerce Company/04-Phone-Screen/04-Phone Screen .ipynb deleted file mode 100644 index 3cb28e9..0000000 --- a/10-Mock Interviews/01-Large E-Commerce Company/04-Phone-Screen/04-Phone Screen .ipynb +++ /dev/null @@ -1,49 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Phone Screen \n", - "\n", - "## Problem\n", - "\n", - "**A tower has 100 floors. You've been given two eggs. The eggs are strong enough that they can be dropped from a particular floor in the tower without breaking. You've been tasked to find the highest floor an egg can be dropped without breaking, in as few drops as possible. If an egg is dropped from above its target floor it will break. If it is dropped from that floor or below, it will be intact and you can test drop the egg again on another floor.**\n", - "\n", - "**Show algorithmically how you would go about doing this in as few drops as possible**\n", - "\n", - "## Requirements\n", - "\n", - "** Use paper/pencil or a whiteboard for this problem **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /01-On-Site-Question-1/01-On-Site Question 1 - SOLUTION.ipynb b/10-Mock Interviews/02-Large Search Engine Company /01-On-Site-Question-1/01-On-Site Question 1 - SOLUTION.ipynb deleted file mode 100644 index 2b13d61..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /01-On-Site-Question-1/01-On-Site Question 1 - SOLUTION.ipynb +++ /dev/null @@ -1,122 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 - SOLUTION\n", - "___\n", - "\n", - "## Question\n", - "** Given a dice which rolls 1 to 7 (with uniform probability), simulate a 5 sided dice. Preferably, write your solution as a function. **\n", - "\n", - "## Requirements\n", - "\n", - "** You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed until you've solved it on pen and paper! **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "___" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# SOLUTION\n", - "\n", - "This is a new problem we haven't seen directly before! Many times this question is asked in the form of functions e.g. your given a function random_7() and you have to take it as an input and create random_5()\n", - "\n", - "The key to solving this problem is to make sure you focus on the requirement that the final distribution of the rolls be uniform, also you were not given any requirements on Time and Space, so the solution is actually *very* simple, just keep re-rolling if you get a number greater than 5!\n", - "\n", - "We can code this out:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "from random import randint\n", - " \n", - "def dice7():\n", - " return randint(1, 7)\n", - " \n", - "# Our Solution\n", - "def convert7to5():\n", - " \n", - " # Starting roll (just needs to be larger than 5)\n", - " roll = 7\n", - " \n", - " while roll > 5:\n", - " \n", - " roll = dice7()\n", - " print('dice7() produced a roll of ',roll)\n", - " print(' Your final returned roll is below:')\n", - " return roll" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "dice7() produced a roll of 3\n", - " Your final returned roll is below:\n" - ] - }, - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "convert7to5()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now, the next problem (On-Site Question 2) will be harder, the reverse conversion of rolls! This question should serve as a reminder not to overthink the solution to a question! Keep in mind that our solution has the potential to run for infinity if we keep rolling 6s and 7s (although this is highly unlikely).\n", - "\n", - "## Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /01-On-Site-Question-1/01-On-Site Question 1 .ipynb b/10-Mock Interviews/02-Large Search Engine Company /01-On-Site-Question-1/01-On-Site Question 1 .ipynb deleted file mode 100644 index 4c78a6b..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /01-On-Site-Question-1/01-On-Site Question 1 .ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 \n", - "___\n", - "\n", - "## Question\n", - "** Given a dice which rolls 1 to 7 (with uniform probability), simulate a 5 sided dice. Preferably, write your solution as a function. **\n", - "\n", - "## Requirements\n", - "\n", - "** You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed until you've solved it on pen and paper! **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "___" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /02-On-Site-Question-2/02-On-Site Question 2 - SOLUTION.ipynb b/10-Mock Interviews/02-Large Search Engine Company /02-On-Site-Question-2/02-On-Site Question 2 - SOLUTION.ipynb deleted file mode 100644 index b887185..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /02-On-Site-Question-2/02-On-Site Question 2 - SOLUTION.ipynb +++ /dev/null @@ -1,143 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 - SOLUTION\n", - "___\n", - "\n", - "## Question\n", - "** Given a dice which rolls from 1 to 5, simulate a uniform 7 sided dice! **\n", - "\n", - "## Requirements\n", - "\n", - "** You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed until you've come up with a solution by hand! **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "___" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# SOLUTION\n", - "\n", - "Because the 5 sided dice can not produce 7 possible outcomes on a single roll, we immediately know that we need to roll the dice at least twice. \n", - "\n", - "If we roll the dice twice we have 25 possible combinations of the results of the two rolls. While 25 is not divisible by 7, 21 is. This means we can implement our previous strategy of throwing out rolls not in our intended range.\n", - "\n", - "It's also important to note that we can't expand the solution to implement more rolls in order to not throw any out, because 5 and 7 are both prime which means that no exponent of 5 will be divisible by 7 no matter how high you go.\n", - "\n", - "We will define our range as a section of the 25 possible combinations of rolls. A good way to do this is by converting the two rolls into a unique outcome number in the range 1 through 25.\n", - "\n", - "We will create this number by taking the rolls, then we take the first roll and after subtracting 1 from it we multiply it by 5. Now the first roll corresponds with a value of 0, 5, 10, 15 and 20.\n", - "\n", - "Then we take the second roll and add it to the result of the first manipulation. Giving us a range of 1-25.\n", - "\n", - "So our final solution is to roll the dice twice. Check the manipulated range from 1 to 25, if its greater than 21, do a reroll. Let's see it in action:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from random import randint\n", - " \n", - "def dice5():\n", - " return randint(1, 5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now for our conversion function:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "def convert5to7():\n", - "\n", - " # For constant re-roll purposes\n", - " while True:\n", - "\n", - " # Roll the dice twice\n", - " roll_1 = dice5()\n", - " roll_2 = dice5()\n", - " \n", - " #print 'The rolls were {} and {}'.format(roll_1,roll_2)\n", - "\n", - " # Convert the combination to the range 1 to 25\n", - " num = ( (roll_1-1) * 5 ) + ( roll_2 ) \n", - "\n", - " #print 'The converted range number was:',num\n", - " if num > 21:\n", - "\n", - " # re-roll if we are out of range\n", - " continue\n", - "\n", - " return num %7 + 1 " - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "convert5to7()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /02-On-Site-Question-2/02-On-Site Question 2 .ipynb b/10-Mock Interviews/02-Large Search Engine Company /02-On-Site-Question-2/02-On-Site Question 2 .ipynb deleted file mode 100644 index df7821e..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /02-On-Site-Question-2/02-On-Site Question 2 .ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 \n", - "___\n", - "\n", - "## Question\n", - "** Given a dice which rolls from 1 to 5, simulate a uniform 7 sided dice! **\n", - "\n", - "## Requirements\n", - "\n", - "** You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed until you've come up with a solution by hand! **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "___" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /03-On-Site-Question-3/03-On-Site Question 3 -SOLUTION.ipynb b/10-Mock Interviews/02-Large Search Engine Company /03-On-Site-Question-3/03-On-Site Question 3 -SOLUTION.ipynb deleted file mode 100644 index 799ac82..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /03-On-Site-Question-3/03-On-Site Question 3 -SOLUTION.ipynb +++ /dev/null @@ -1,89 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3 - SOLUTION\n", - "\n", - "## Question\n", - "\n", - "**Given a string, write a function that uses recursion to reverse it. **\n", - "\n", - "## Requirements\n", - "\n", - "** You MUST use pen and paper or a whiteboard to answer this, no coding allowed! **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "____" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# SOLUTION\n", - "\n", - "Hopefully you remember this problem, you've already seen it! The solution is:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "def reverse(s):\n", - " \n", - " # Base Case\n", - " if len(s) <= 1:\n", - " return s\n", - "\n", - " # Recursion\n", - " return reverse(s[1:]) + s[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "___\n", - "## Notes\n", - "\n", - "Remember when recursion questions arise, think about the base case and the recursive case. Review the recusion section of the course for review for this problem." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /03-On-Site-Question-3/03-On-Site Question 3 .ipynb b/10-Mock Interviews/02-Large Search Engine Company /03-On-Site-Question-3/03-On-Site Question 3 .ipynb deleted file mode 100644 index 071f91a..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /03-On-Site-Question-3/03-On-Site Question 3 .ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3 \n", - "\n", - "## Question\n", - "\n", - "**Given a string, write a function that uses recursion to reverse it. **\n", - "\n", - "## Requirements\n", - "\n", - "** You MUST use pen and paper or a whiteboard to answer this, no coding allowed! **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "____" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /04-On-Site-Question-4/04-On-Site Question 4 - SOLUTION.ipynb b/10-Mock Interviews/02-Large Search Engine Company /04-On-Site-Question-4/04-On-Site Question 4 - SOLUTION.ipynb deleted file mode 100644 index bff41db..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /04-On-Site-Question-4/04-On-Site Question 4 - SOLUTION.ipynb +++ /dev/null @@ -1,247 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 4 - SOLUTION\n", - "\n", - "## Question\n", - "**Find the squareroot of a given number rounded down to the nearest integer, without using the sqrt function. For example, squareroot of a number between [9, 15] should return 3, and [16, 24] should be 4.**\n", - "\n", - "## Requirements\n", - "\n", - "** Feel free to code this out (but its recommended that you use paper/pencil or whiteboard)**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Solution\n", - "\n", - "The squareroot of a (non-negative) number N always lies between 0 and N/2. The straightforward way to solve this problem would be to check every number k between 0 and N/2, until the square of k becomes greater than or rqual to N. If k^2 becomes equal to N, then we return k. Otherwise, we return k-1 because we're rounding down. Here's the code:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "def solution(num): \n", - " if num<0: \n", - " raise ValueError \n", - " if num==1: \n", - " return 1 \n", - " for k in range(1+(num//2)): \n", - " if k**2==num: \n", - " return k \n", - " elif k**2>num: \n", - " return k-1 \n", - " return k " - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "solution(14)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "solution(15)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "solution(16)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The complexity of this approach is O(N), because we have to check N/2 numbers in the worst case. This linear algorithm is pretty inefficient, we can use some sort of binary search to speed it up. We know that the result is between 0 and N/2, so we can first try N/4 to see whether its square is less than, greater than, or equal to N. If it’s equal then we simply return that value. If it’s less, then we continue our search between N/4 and N/2. Otherwise if it’s greater, then we search between 0 and N/4. In both cases we reduce the potential range by half and continue, this is the logic of binary search. We’re not performing regular binary search though, it’s modified. We want to ensure that we stop at a number k, where k^2<=N but (k+1)^2>N. For example:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [], - "source": [ - "def better_solution(num): \n", - " if num<0: \n", - " raise ValueError \n", - " if num==1: \n", - " return 1 \n", - " low=0 \n", - " high=1+(num/2) \n", - " \n", - " while low+1\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - " Fib(n=15000)\n", - "\n", - "\n", - "loops\n", - "recursion\n", - "generators\n", - "memoization\n", - "memoization as decorator\n", - "\n", - "\n", - "45\n", - "87\n", - "58\n", - "44\n", - "43\n", - "\n", - "\n", - "47\n", - "88\n", - "58\n", - "42\n", - "42\n", - "\n", - "\n", - "51\n", - "92\n", - "60\n", - "44\n", - "43\n", - "\n", - "\n", - "43\n", - "87\n", - "58\n", - "42\n", - "43\n", - "\n", - "\n", - "48\n", - "92\n", - "61\n", - "42\n", - "44\n", - "\n", - "\n", - "45\n", - "87\n", - "59\n", - "43\n", - "44\n", - "\n", - "\n", - "44\n", - "85\n", - "57\n", - "42\n", - "44\n", - "\n", - "\n", - "44\n", - "87\n", - "62\n", - "43\n", - "43\n", - "\n", - "\n", - "48\n", - "86\n", - "59\n", - "42\n", - "43\n", - "\n", - "\n", - "45\n", - "91\n", - "61\n", - "45\n", - "45\n", - "\n", - "\n", - "46\n", - "88.2\n", - "59.3\n", - "42.9\n", - "43.4 (Avg)\n", - "\n", - "\n", - "" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/02-Large Search Engine Company /05-Phone-Screen/05-Phone Screen.ipynb b/10-Mock Interviews/02-Large Search Engine Company /05-Phone-Screen/05-Phone Screen.ipynb deleted file mode 100644 index 1cb58d6..0000000 --- a/10-Mock Interviews/02-Large Search Engine Company /05-Phone-Screen/05-Phone Screen.ipynb +++ /dev/null @@ -1,80 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Phone Screen\n", - "\n", - "**This phone screen will consist of a non-technical series of questions about you and the company, and then a second half of a simple technical question to be coded out**\n", - "___\n", - "## Non-Technical Questions\n", - "\n", - "**Answer the following questions (2-5 minute responses) technical answers not required, more interested in hearing about your reasoning**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "* Give me some quick background about you (go over your resume)\n", - "* Why do you want to work here?\n", - "* What's your favorite programming language and why?\n", - "* Where do you see yourself in 5 years?\n", - "* Do you have any questions about the company for me?" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "___" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "___\n", - "## Technical Questions\n", - "\n", - "**Answer the following question in the *Markdown* cell below. It's important to note that the cell below does NOT have syntax highlighting, its common in a phone screen interview to be given a text editor hich doesn't have anything more than basic text support**\n", - "\n", - "1. Write a function that computes the Nth fibonacci number" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.10" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/01-On-Site-Quesion-1/On-Site Question 1 - SOLUTION.ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/01-On-Site-Quesion-1/On-Site Question 1 - SOLUTION.ipynb deleted file mode 100644 index a471b4b..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/01-On-Site-Quesion-1/On-Site Question 1 - SOLUTION.ipynb +++ /dev/null @@ -1,149 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** Given a list of integers, find the largest product you could make from 3 integers in the list **\n", - "\n", - "## Requirements\n", - "\n", - "** You can assume that the list will always have at least 3 integers **\n", - "\n", - "** Paper/pencil only, don't code this out until you've solved it as far as you can by hand. **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Solution\n", - "\n", - "We can solve this problem in O(n) time with O(1) space, we should also be able to take into account negative numbers, so that a list like: [-5,-5,1,3] returns (-5)(-5)(3) = 75 as its answer.\n", - "\n", - "Hopefully you've begun to realize the similarity between this problem and the Amazon stock problem from the E-Commerce Company mock interview questions! You could brute force this problem by just simply trying every single combination of three digits, but this would require O(n^3) time!\n", - "\n", - "How about we use a greedy approach and keep track of some numbers. In the stock problem we kept track of max profit so far, in this problem we are actually going to keep track of several numbers:\n", - "\n", - "* The highest product of 3 numbers so far\n", - "* The highest product of 2 numbers so far\n", - "* The highest number so far\n", - "\n", - "Since we want to keep negative numbers in account, we will also keep track of the lowest product of two and the lowest number:\n", - "\n", - "* The lowest product of 2\n", - "* The lowest number\n", - "\n", - "Once we iterate through the list and reach the end we will have the highest posiible product with 3 numbers. At each iteration we will take the current highest product of 3 and compare it to the current integer multiplied by the highest and lowest products of 2.\n", - "\n", - "Let's see this coded out:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def solution(lst):\n", - " \n", - " # Start at index 2 (3rd element) and assign highest and lowest \n", - " # based off of first two elements\n", - " \n", - " # Highest Number so far\n", - " high = max(lst[0],lst[1])\n", - " \n", - " # Lowest number so far\n", - " low = min(lst[0],lst[1])\n", - " \n", - " # Initiate Highest and lowest products of two numbers\n", - " high_prod2 = lst[0]*lst[1]\n", - " low_prod2 = lst[0]*lst[1]\n", - " \n", - " # Initiate highest product of 3 numbers\n", - " high_prod3 = lst[0]*lst[1]*lst[2]\n", - " \n", - " # Iterate through list\n", - " for num in lst[2:]:\n", - " \n", - " # Compare possible highest product of 3 numbers\n", - " high_prod3 = max(high_prod3,num*high_prod2,num*low_prod2)\n", - " \n", - " \n", - " # Check for possible new highest products of 2 numbers\n", - " high_prod2 = max(high_prod2,num*high,num*low)\n", - " \n", - " # Check for possible new lowest products of 2 numbers\n", - " low_prod2 = min(low_prod2,num*high,num*low)\n", - " \n", - " # Check for new possible high\n", - " high = max(high,num)\n", - " \n", - " # Check for new possible low\n", - " low = min(low,num)\n", - " \n", - " return high_prod3" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "763092" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "l = [99,-82,82,40,75,-24,39, -82, 5, 30, -25, -94, 93, -23, 48, 50, 49,-81,41,63]\n", - "\n", - "solution(l)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Great! Through the use of a greedy approach we have been able to complete the problem in O(n) time. Keep this sort of approach in mind when you have to iterate through a list and a brute force solution is on the order of an exponential!\n", - "\n", - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/01-On-Site-Quesion-1/On-Site Question 1 .ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/01-On-Site-Quesion-1/On-Site Question 1 .ipynb deleted file mode 100644 index ccfa72f..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/01-On-Site-Quesion-1/On-Site Question 1 .ipynb +++ /dev/null @@ -1,49 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 \n", - "\n", - "## Problem\n", - "\n", - "** Given a list of integers, find the largest product you could make from 3 integers in the list **\n", - "\n", - "## Requirements\n", - "\n", - "** You can assume that the list will always have at least 3 integers **\n", - "\n", - "** Paper/pencil only, don't code this out until you've solved it as far as you can by hand. **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/02-On-Site-Quesion-2/On-Site Question 2 - SOLUTION.ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/02-On-Site-Quesion-2/On-Site Question 2 - SOLUTION.ipynb deleted file mode 100644 index 0c61379..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/02-On-Site-Quesion-2/On-Site Question 2 - SOLUTION.ipynb +++ /dev/null @@ -1,115 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** Write a function that given a target amount of money and a list of possible coin denominations, returns the number of ways to make change for the target amount using the coin denominations**\n", - "\n", - "## Requirements\n", - "\n", - "** Write out your work on paper/pencil, then see if you can code up your solution **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Solution\n", - "\n", - "This is a classic interview problem, so classic that you've already seen a very similar problem in the recursion section! Make sure to review that problem first before reading our solution here!\n", - "\n", - "In this solution we will use a [bottom-up](https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design) algorithm.\n", - "\n", - "* As we iterate through each coin, we are adding the ways of making arr[i - coin] to arr[i]\n", - "* If we have 2 ways of making 4, and are now iterating on a coin of value 3, there should be 2 ways of making 7.\n", - "* We are essentially adding the coin we are iterating on to the number of ways of making arr[i]." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def solution(n, coins):\n", - " \n", - " # Set up our array for trakcing results\n", - " arr = [1] + [0] * n\n", - " \n", - " for coin in coins:\n", - " for i in range(coin, n + 1):\n", - " arr[i] += arr[i - coin]\n", - " \n", - " if n == 0:\n", - " return 0\n", - " else:\n", - " return arr[n]\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "884" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "solution(100, [1, 2, 3])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This solution results in O((m)(n)) with m being the number of coins, where we iterate about n operations. This is O(n) space." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/02-On-Site-Quesion-2/On-Site Question 2 .ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/02-On-Site-Quesion-2/On-Site Question 2 .ipynb deleted file mode 100644 index cd47095..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/02-On-Site-Quesion-2/On-Site Question 2 .ipynb +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 \n", - "\n", - "## Problem\n", - "\n", - "** Write a function that given a target amount of money and a list of possible coin denominations, returns the number of ways to make change for the target amount using the coin denominations**\n", - "\n", - "## Requirements\n", - "\n", - "** Write out your work on paper/pencil, then see if you can code up your solution **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/03-On-Site-Quesion-3/On-Site Question 3 - SOLUTION.ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/03-On-Site-Quesion-3/On-Site Question 3 - SOLUTION.ipynb deleted file mode 100644 index 238cc66..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/03-On-Site-Quesion-3/On-Site Question 3 - SOLUTION.ipynb +++ /dev/null @@ -1,121 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** Given a binary tree, check whether it’s a binary search tree or not. **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Requirements\n", - "\n", - "** Use paper/pencil, do not code this in an IDE until you've done it manually**\n", - "\n", - "** Do not use built-in Python libraries to do this, but do mention them if you know about them **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Solution\n", - "\n", - "The first solution that comes to mind is, at every node check whether its value is larger than or equal to its left child and smaller than or equal to its right child (assuming equals can appear at either left or right). However, this approach is erroneous because it doesn’t check whether a node violates any condition with its grandparent or any of its ancestors. \n", - "\n", - "So, we should keep track of the minimum and maximum values a node can take. And at each node we will check whether its value is between the min and max values it’s allowed to take. The root can take any value between negative infinity and positive infinity. At any node, its left child should be smaller than or equal than its own value, and similarly the right child should be larger than or equal to. So during recursion, we send the current value as the new max to our left child and send the min as it is without changing. And to the right child, we send the current value as the new min and send the max without changing. " - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "class Node: \n", - " def __init__(self, val=None): \n", - " self.left, self.right, self.val = None, None, val \n", - " \n", - "INFINITY = float(\"infinity\") \n", - "NEG_INFINITY = float(\"-infinity\") \n", - "\n", - "def isBST(tree, minVal=NEG_INFINITY, maxVal=INFINITY): \n", - " if tree is None:\n", - " return True \n", - " if not minVal <= tree.val <= maxVal: \n", - " return False \n", - " \n", - " return isBST(tree.left, minVal, tree.val) and isBST(tree.right, tree.val, maxVal) " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "There’s an equally good alternative solution. If a tree is a binary search tree, then traversing the tree inorder should lead to sorted order of the values in the tree. So, we can perform an inorder traversal and check whether the node values are sorted or not." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def isBST2(tree, lastNode=[NEG_INFINITY]): \n", - " \n", - " if tree is None: \n", - " return True \n", - " \n", - " if not isBST2(tree.left, lastNode):\n", - " return False \n", - " \n", - " if tree.val < lastNode[0]: \n", - " return False \n", - " \n", - " lastNode[0]=tree.val \n", - " \n", - " return isBST2(tree.right, lastNode) " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This is a common interview problem, its relatively simple, but not trivial and shows that someone has a knowledge of binary search trees and tree traversals.\n", - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/03-On-Site-Quesion-3/On-Site Question 3 .ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/03-On-Site-Quesion-3/On-Site Question 3 .ipynb deleted file mode 100644 index 380ec41..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/03-On-Site-Quesion-3/On-Site Question 3 .ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3 \n", - "\n", - "## Problem\n", - "\n", - "** Given a binary tree, check whether it’s a binary search tree or not. **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Requirements\n", - "\n", - "** Use paper/pencil, do not code this in an IDE until you've done it manually**\n", - "\n", - "** Do not use built-in Python libraries to do this, but do mention them if you know about them **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/04-Phone-Screen/Phone Screen - SOLUTION.ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/04-Phone-Screen/Phone Screen - SOLUTION.ipynb deleted file mode 100644 index 662e837..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/04-Phone-Screen/Phone Screen - SOLUTION.ipynb +++ /dev/null @@ -1,53 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Phone Screen - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** If you were given a list of n integers and knew that they were sorted, how quickly could you check if a given integer was in the list? Elaborate on your reasoning and search methods in general**\n", - "\n", - "## Requirements\n", - "\n", - "** Try explaining your solution to someone and see if it makes sense ot them. Don't code anything for this problem **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Solution\n", - "\n", - "Hopefully this problem sounds familiar! We can use a binary search to search for an intger since the list is already sorted! This means we can find the item in [O(logn) time and O(1) space](http://bigocheatsheet.com/)!\n", - "\n", - "Revisit the lectures on Binary Search and its implementation to fully get the reasoning behind this solution and problem!\n", - "\n", - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/03-Ride Share Start-Up Company/04-Phone-Screen/Phone Screen .ipynb b/10-Mock Interviews/03-Ride Share Start-Up Company/04-Phone-Screen/Phone Screen .ipynb deleted file mode 100644 index 062944a..0000000 --- a/10-Mock Interviews/03-Ride Share Start-Up Company/04-Phone-Screen/Phone Screen .ipynb +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Phone Screen \n", - "\n", - "## Problem\n", - "\n", - "** If you were given a list of n integers and knew that they were sorted, how quickly could you check if a given integer was in the list? Elaborate on your reasoning and search methods in general**\n", - "\n", - "## Requirements\n", - "\n", - "** Try explaining your solution to someone and see if it makes sense ot them. Don't code anything for this problem **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/01-On-Site-Question-1/On-Site Question 1 - SOLUTION.ipynb b/10-Mock Interviews/04-Social Network Company/01-On-Site-Question-1/On-Site Question 1 - SOLUTION.ipynb deleted file mode 100644 index f00525d..0000000 --- a/10-Mock Interviews/04-Social Network Company/01-On-Site-Question-1/On-Site Question 1 - SOLUTION.ipynb +++ /dev/null @@ -1,138 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** Given a list of integers and a target number, write a function that returns a boolean indicating if its possible to sum two integers from the list to reach the target number **\n", - "\n", - "## Requirements\n", - "\n", - "** Try pen/paper before coding out your solution **\n", - "\n", - "** You can not use an integer element twice. Optimize for time over space **" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Solution\n", - "\n", - "For this problem we will take advantage of a **set** data structure. We will make a single pass through the list of integers, treating each element as the first integer of our possible sum.\n", - "\n", - "At each iteration we will check to see if there is a second integer which will allow us hit the target number, adn we will use a set to check if we've already seen it in our list.\n", - "\n", - "We will then update our seen set by adding the current number in the iteration to it.\n", - "\n", - "Let's see this coded out:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def solution(lst,target):\n", - " \n", - " # Create set to keep track of duplicates\n", - " seen = set()\n", - " \n", - " # We want to find if there is a num2 that sums with num to reach the target\n", - " \n", - " for num in lst:\n", - " \n", - " num2 = target - num\n", - " \n", - " if num2 in seen:\n", - " return True\n", - " \n", - " seen.add(num)\n", - " \n", - " # If we never find a pair match which creates the sum\n", - " return False" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "solution([1,3,5,1,7],4)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "solution([1,3,5,1,7],14)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/01-On-Site-Question-1/On-Site Question 1.ipynb b/10-Mock Interviews/04-Social Network Company/01-On-Site-Question-1/On-Site Question 1.ipynb deleted file mode 100644 index a72a467..0000000 --- a/10-Mock Interviews/04-Social Network Company/01-On-Site-Question-1/On-Site Question 1.ipynb +++ /dev/null @@ -1,49 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 1 \n", - "\n", - "## Problem\n", - "\n", - "** Given a list of integers and a target number, write a function that returns a boolean indicating if its possible to sum two integers from the list to reach the target number **\n", - "\n", - "## Requirements\n", - "\n", - "** Try pen/paper before coding out your solution **\n", - "\n", - "** You can not use an integer element twice. Optimize for time over space **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/02-On-Site-Question-2/On-Site Question 2 - SOLUTION.ipynb b/10-Mock Interviews/04-Social Network Company/02-On-Site-Question-2/On-Site Question 2 - SOLUTION.ipynb deleted file mode 100644 index 7d049b8..0000000 --- a/10-Mock Interviews/04-Social Network Company/02-On-Site-Question-2/On-Site Question 2 - SOLUTION.ipynb +++ /dev/null @@ -1,86 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** Given a list of account ID numbers (integers) which contains duplicates , find the one unique integer. (the list is guaranteed to only have one unique (non-duplicated) integer **\n", - "\n", - "## Requirements\n", - "\n", - "** Do not use built-in Python functions or methods **" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Solution\n", - "\n", - "This should feel very familiar to one of the problems we did in the array section of the course! We can use an [XOR](https://en.wikipedia.org/wiki/Exclusive_or) operation. The **exclusive or** operations will take two sets of bits and for each pair it will return a 1 value if **one but not both** of the bits is 1.\n", - "\n", - "In Python we can use the ^ symbol to perform an XOR.\n", - "\n", - "Now for our solution we can simply XOR all the integers in the list. We start with a unique id set to 0, then every time we XOR a new id from the list, it will change the bits. When we XOR with the same ID again, it will cancel out the earlier change.\n", - "\n", - "By the end, we wil be left with the ID that was unique and only appeared once!" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def solution(id_list):\n", - " \n", - " # Initiate unique Id\n", - " unique_id = 0\n", - " \n", - " # XOR fo revery id in id list\n", - " for i in id_list:\n", - " \n", - " # XOR operation\n", - " unique_id ^= i\n", - " \n", - " return unique_id" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/02-On-Site-Question-2/On-Site Question 2.ipynb b/10-Mock Interviews/04-Social Network Company/02-On-Site-Question-2/On-Site Question 2.ipynb deleted file mode 100644 index e65f70c..0000000 --- a/10-Mock Interviews/04-Social Network Company/02-On-Site-Question-2/On-Site Question 2.ipynb +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 2 \n", - "\n", - "## Problem\n", - "\n", - "** Given a list of account ID numbers (integers) which contains duplicates , find the one unique integer. (the list is guaranteed to only have one unique (non-duplicated) integer **\n", - "\n", - "## Requirements\n", - "\n", - "** Do not use built-in Python functions or methods **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/03-On-Site-Question-3/On-Site Question 3 - SOLUTION.ipynb b/10-Mock Interviews/04-Social Network Company/03-On-Site-Question-3/On-Site Question 3 - SOLUTION.ipynb deleted file mode 100644 index 62cbf2b..0000000 --- a/10-Mock Interviews/04-Social Network Company/03-On-Site-Question-3/On-Site Question 3 - SOLUTION.ipynb +++ /dev/null @@ -1,116 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3 - SOLUTION\n", - "\n", - "## Problem\n", - "\n", - "** Create a function that takes in a list of unsorted prices (integers) and a maximum possible price value, and return a sorted list of prices**\n", - "\n", - "## Requirements\n", - "\n", - "** Your function should be able to perform this in less than O(nlogn) time. **" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Solution\n", - "\n", - "We can actually solve this problem by using a [*counting sort*](https://en.wikipedia.org/wiki/Counting_sort). Basically a counting sort works well when you know the range of integer values you will have ahead of time.\n", - "\n", - "Read the wikipedia article linked above for a full break down, and an implementation is here below (using the prices situation described in the problem above)." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def solution(unsorted_prices,max_price):\n", - " \n", - " # list of 0s at indices 0 to max_price\n", - " prices_to_counts = [0]* (max_price+1)\n", - " \n", - " # populate prices\n", - " for price in unsorted_prices:\n", - " prices_to_counts[price] +=1\n", - " \n", - " # populate final sorted prices\n", - " sorted_prices = []\n", - " \n", - " # For each price in prices_to_counts\n", - " for price,count in enumerate(prices_to_counts):\n", - " \n", - " # for the number of times the element occurs\n", - " for time in range(count):\n", - " \n", - " # add it to the sorted price list\n", - " sorted_prices.append(price)\n", - " \n", - " return sorted_prices" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[2, 3, 4, 6, 7, 8, 9]" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "solution([4,6,2,7,3,8,9],9)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This was a great exercise in learning about a new sorting algorithm, make sure to read up on it and practice this problem again!\n", - "\n", - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/03-On-Site-Question-3/On-Site Question 3.ipynb b/10-Mock Interviews/04-Social Network Company/03-On-Site-Question-3/On-Site Question 3.ipynb deleted file mode 100644 index 139bd6c..0000000 --- a/10-Mock Interviews/04-Social Network Company/03-On-Site-Question-3/On-Site Question 3.ipynb +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# On-Site Question 3 \n", - "\n", - "## Problem\n", - "\n", - "** Create a function that takes in a list of unsorted prices (integers) and a maximum possible price value, and return a sorted list of prices**\n", - "\n", - "## Requirements\n", - "\n", - "** Your function should be able to perform this in less than O(nlogn) time. **" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/04-Phone-Screen/Phone Screen - SOLUTION.ipynb b/10-Mock Interviews/04-Social Network Company/04-Phone-Screen/Phone Screen - SOLUTION.ipynb deleted file mode 100644 index fe61bd1..0000000 --- a/10-Mock Interviews/04-Social Network Company/04-Phone-Screen/Phone Screen - SOLUTION.ipynb +++ /dev/null @@ -1,87 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Phone Screen - SOLUTION\n", - "\n", - "## Problem \n", - "\n", - "** Remove duplicate characters in a given string keeping only the first occurrences. For example, if the input is ‘tree traversal’ the output will be ‘tre avsl’. **\n", - "\n", - "## Requirements\n", - "\n", - "**Complete this problem on a text editor that does not have syntax highlighting, such as a goolge doc!**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Solution\n", - "\n", - "We need a data structure to keep track of the characters we have seen so far, which can perform efficient find operation. If the input is guaranteed to be in standard ASCII form, we can just create a boolean array of size 128 and perform lookups by accessing the index of the character’s ASCII value in constant time. But if the string is Unicode then we would need a much larger array of size more than 100K, which will be a waste since most of it would generally be unused.\n", - "\n", - "Set data structure perfectly suits our purpose. It stores keys and provides constant time search for key existence. So, we’ll loop over the characters of the string, and at each iteration we’ll check whether we have seen the current character before by searching the set. If it’s in the set then it means we’ve seen it before, so we ignore it. Otherwise, we include it in the result and add it to the set to keep track for future reference. The code is easier to understand:" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def removeDuplicates(string): \n", - " result=[] \n", - " seen=set() \n", - " \n", - " for char in string: \n", - " if char not in seen: \n", - " seen.add(char) \n", - " result.append(char)\n", - " \n", - " return ''.join(result) " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The time complexity of the algorithm is O(N) where N is the number of characters in the input string, because set supports O(1) insert and find. This is an optimal solution to one of the most common string interview questions. \n", - "\n", - "This problem should have felt very similar to some other array questions you've been asked! Remember that many basic interview question ideas overlap, just their presentation is different!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Job!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/10-Mock Interviews/04-Social Network Company/04-Phone-Screen/Phone Screen .ipynb b/10-Mock Interviews/04-Social Network Company/04-Phone-Screen/Phone Screen .ipynb deleted file mode 100644 index d082e4e..0000000 --- a/10-Mock Interviews/04-Social Network Company/04-Phone-Screen/Phone Screen .ipynb +++ /dev/null @@ -1,47 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Phone Screen \n", - "\n", - "## Problem \n", - "\n", - "** Remove duplicate characters in a given string keeping only the first occurrences. For example, if the input is ‘tree traversal’ the output will be ‘tre avsl’. **\n", - "\n", - "## Requirements\n", - "\n", - "**Complete this problem on a text editor that does not have syntax highlighting, such as a goolge doc!**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Good Luck!" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -}