Skip to content

Commit 40ddd2b

Browse files
committed
mck
1 parent 544265f commit 40ddd2b

File tree

6 files changed

+318
-20
lines changed

6 files changed

+318
-20
lines changed

Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems - SOLUTIONS/.ipynb_checkpoints/On-Site Question 2 - SOLUTION-checkpoint.ipynb

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,130 @@
1414
"\n",
1515
"## Requirements\n",
1616
"\n",
17-
"** You can not use division in your answer**\n",
17+
"** 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",
18+
"\n",
1819
"** Try to do this on a white board or with paper/pencil.**"
1920
]
2021
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {
25+
"collapsed": true
26+
},
27+
"source": [
28+
"___\n",
29+
"## Solution\n",
30+
"\n",
31+
"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",
32+
"\n",
33+
"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",
34+
"\n",
35+
"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",
36+
"\n",
37+
"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",
38+
"\n",
39+
"Then we just need to multiply all the products before and after each index in order to get the final product answer!\n",
40+
"\n",
41+
"Let's see this in action:"
42+
]
43+
},
2144
{
2245
"cell_type": "code",
23-
"execution_count": null,
46+
"execution_count": 19,
2447
"metadata": {
2548
"collapsed": true
2649
},
2750
"outputs": [],
28-
"source": []
51+
"source": [
52+
"def index_prod(lst):\n",
53+
" \n",
54+
" # Create an empty output list\n",
55+
" output = [None] * len(lst)\n",
56+
" \n",
57+
" # Set initial product and index for greedy run forward\n",
58+
" product = 1\n",
59+
" i = 0\n",
60+
" \n",
61+
" while i < len(lst):\n",
62+
" \n",
63+
" # Set index as cumulative product\n",
64+
" output[i] = product\n",
65+
" \n",
66+
" # Cumulative product\n",
67+
" product *= lst[i]\n",
68+
" \n",
69+
" # Move forward\n",
70+
" i +=1\n",
71+
" \n",
72+
" \n",
73+
" # Now for our Greedy run Backwards\n",
74+
" product = 1\n",
75+
" \n",
76+
" # Start index at last (taking into account index 0)\n",
77+
" i = len(lst) - 1\n",
78+
" \n",
79+
" # Until the beginning of the list\n",
80+
" while i >=0:\n",
81+
" \n",
82+
" # Same operations as before, just backwards\n",
83+
" output[i] *= product\n",
84+
" product *= lst[i]\n",
85+
" i -= 1\n",
86+
" \n",
87+
" return output "
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 20,
93+
"metadata": {
94+
"collapsed": false
95+
},
96+
"outputs": [
97+
{
98+
"data": {
99+
"text/plain": [
100+
"[24, 12, 8, 6]"
101+
]
102+
},
103+
"execution_count": 20,
104+
"metadata": {},
105+
"output_type": "execute_result"
106+
}
107+
],
108+
"source": [
109+
"index_prod([1,2,3,4])"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 21,
115+
"metadata": {
116+
"collapsed": false
117+
},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/plain": [
122+
"[24, 0, 0, 0, 0]"
123+
]
124+
},
125+
"execution_count": 21,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"index_prod([0,1,2,3,4])"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"Review the solution and make sure you understand it! It uses O(n) time and O(n) space complexity!\n",
139+
"# Good Job!"
140+
]
29141
}
30142
],
31143
"metadata": {

Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems - SOLUTIONS/.ipynb_checkpoints/Phone Screen - SOLUTION-checkpoint.ipynb

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,52 @@
1212
"\n",
1313
"## Requirements\n",
1414
"\n",
15-
"** Use parper/pencil or a whiteboard for this problem **"
15+
"** Use paper/pencil or a whiteboard for this problem **"
1616
]
1717
},
1818
{
19-
"cell_type": "code",
20-
"execution_count": null,
19+
"cell_type": "markdown",
2120
"metadata": {
2221
"collapsed": true
2322
},
24-
"outputs": [],
25-
"source": []
23+
"source": [
24+
"## Solution\n",
25+
"\n",
26+
"** 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",
27+
"\n",
28+
"\n",
29+
"Start from the 10th floor and go up to floors in multiples of 10.\n",
30+
"\n",
31+
"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",
32+
"\n",
33+
"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",
34+
"\n",
35+
"**Best solution:**\n",
36+
"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",
37+
"\n",
38+
"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",
39+
"\n",
40+
"Let's assume we take some step value m initially. If every subsequent step is m-1,\n",
41+
"then, \n",
42+
"$$m+m−1+m−2+.....+1=n$$\n",
43+
"\n",
44+
"This is \n",
45+
"\n",
46+
"$$\\frac{m∗(m+1)}{2}=n$$\n",
47+
"\n",
48+
"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",
49+
"\n",
50+
"So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14.\n",
51+
"\n",
52+
"Note that this is simply a binary search!"
53+
]
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"metadata": {},
58+
"source": [
59+
"# Good Job!"
60+
]
2661
}
2762
],
2863
"metadata": {

Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,130 @@
1414
"\n",
1515
"## Requirements\n",
1616
"\n",
17-
"** You can not use division in your answer**\n",
17+
"** 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",
18+
"\n",
1819
"** Try to do this on a white board or with paper/pencil.**"
1920
]
2021
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {
25+
"collapsed": true
26+
},
27+
"source": [
28+
"___\n",
29+
"## Solution\n",
30+
"\n",
31+
"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",
32+
"\n",
33+
"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",
34+
"\n",
35+
"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",
36+
"\n",
37+
"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",
38+
"\n",
39+
"Then we just need to multiply all the products before and after each index in order to get the final product answer!\n",
40+
"\n",
41+
"Let's see this in action:"
42+
]
43+
},
2144
{
2245
"cell_type": "code",
23-
"execution_count": null,
46+
"execution_count": 19,
2447
"metadata": {
2548
"collapsed": true
2649
},
2750
"outputs": [],
28-
"source": []
51+
"source": [
52+
"def index_prod(lst):\n",
53+
" \n",
54+
" # Create an empty output list\n",
55+
" output = [None] * len(lst)\n",
56+
" \n",
57+
" # Set initial product and index for greedy run forward\n",
58+
" product = 1\n",
59+
" i = 0\n",
60+
" \n",
61+
" while i < len(lst):\n",
62+
" \n",
63+
" # Set index as cumulative product\n",
64+
" output[i] = product\n",
65+
" \n",
66+
" # Cumulative product\n",
67+
" product *= lst[i]\n",
68+
" \n",
69+
" # Move forward\n",
70+
" i +=1\n",
71+
" \n",
72+
" \n",
73+
" # Now for our Greedy run Backwards\n",
74+
" product = 1\n",
75+
" \n",
76+
" # Start index at last (taking into account index 0)\n",
77+
" i = len(lst) - 1\n",
78+
" \n",
79+
" # Until the beginning of the list\n",
80+
" while i >=0:\n",
81+
" \n",
82+
" # Same operations as before, just backwards\n",
83+
" output[i] *= product\n",
84+
" product *= lst[i]\n",
85+
" i -= 1\n",
86+
" \n",
87+
" return output "
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 20,
93+
"metadata": {
94+
"collapsed": false
95+
},
96+
"outputs": [
97+
{
98+
"data": {
99+
"text/plain": [
100+
"[24, 12, 8, 6]"
101+
]
102+
},
103+
"execution_count": 20,
104+
"metadata": {},
105+
"output_type": "execute_result"
106+
}
107+
],
108+
"source": [
109+
"index_prod([1,2,3,4])"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 21,
115+
"metadata": {
116+
"collapsed": false
117+
},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/plain": [
122+
"[24, 0, 0, 0, 0]"
123+
]
124+
},
125+
"execution_count": 21,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"index_prod([0,1,2,3,4])"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"Review the solution and make sure you understand it! It uses O(n) time and O(n) space complexity!\n",
139+
"# Good Job!"
140+
]
29141
}
30142
],
31143
"metadata": {

Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems - SOLUTIONS/Phone Screen - SOLUTION.ipynb

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,52 @@
1212
"\n",
1313
"## Requirements\n",
1414
"\n",
15-
"** Use parper/pencil or a whiteboard for this problem **"
15+
"** Use paper/pencil or a whiteboard for this problem **"
1616
]
1717
},
1818
{
19-
"cell_type": "code",
20-
"execution_count": null,
19+
"cell_type": "markdown",
2120
"metadata": {
2221
"collapsed": true
2322
},
24-
"outputs": [],
25-
"source": []
23+
"source": [
24+
"## Solution\n",
25+
"\n",
26+
"** 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",
27+
"\n",
28+
"\n",
29+
"Start from the 10th floor and go up to floors in multiples of 10.\n",
30+
"\n",
31+
"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",
32+
"\n",
33+
"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",
34+
"\n",
35+
"**Best solution:**\n",
36+
"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",
37+
"\n",
38+
"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",
39+
"\n",
40+
"Let's assume we take some step value m initially. If every subsequent step is m-1,\n",
41+
"then, \n",
42+
"$$m+m−1+m−2+.....+1=n$$\n",
43+
"\n",
44+
"This is \n",
45+
"\n",
46+
"$$\\frac{m∗(m+1)}{2}=n$$\n",
47+
"\n",
48+
"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",
49+
"\n",
50+
"So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14.\n",
51+
"\n",
52+
"Note that this is simply a binary search!"
53+
]
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"metadata": {},
58+
"source": [
59+
"# Good Job!"
60+
]
2661
}
2762
],
2863
"metadata": {

Riddles/Riddle Interview Problems/Riddle Interview Problems - SOLUTIONS/.ipynb_checkpoints/Egg Drop - SOLUTION-checkpoint.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
"\n",
3030
"This is \n",
3131
"\n",
32-
"$$m∗(m+1)2=n$$\n",
32+
"$$\\frac{m∗(m+1)}{2}=n$$\n",
3333
"\n",
3434
"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",
3535
"\n",
36-
"So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14."
36+
"So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14.\n",
37+
"\n",
38+
"Note that this is simply a binary search!"
3739
]
3840
},
3941
{

0 commit comments

Comments
 (0)