Skip to content

Commit a036b2d

Browse files
committed
mock 2
1 parent 195d14b commit a036b2d

File tree

4 files changed

+168
-22
lines changed

4 files changed

+168
-22
lines changed

Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/.ipynb_checkpoints/On-Site Question 1 - SOLUTION-checkpoint.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"cell_type": "markdown",
9898
"metadata": {},
9999
"source": [
100-
"Now, the next problem (On-Site Question 2) will be harder, the reverse conversion of rolls! This question shoudl serve as a reminder not to overthink the solution to a question!\n",
100+
"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",
101101
"\n",
102102
"## Good Job!"
103103
]

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

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"___\n",
99
"\n",
1010
"## Question\n",
11-
"** Given a function which produces a random integer in the range of 1 to 7, write a function which produces a random integer from 1 to 5**\n",
11+
"** Given a dice which rolls from 1 to 5, simulate a uniform 7 sided dice! **\n",
1212
"\n",
1313
"## Requirements\n",
1414
"\n",
15-
"** You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed! **"
15+
"** 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! **"
1616
]
1717
},
1818
{
@@ -28,14 +28,24 @@
2828
"source": [
2929
"# SOLUTION\n",
3030
"\n",
31-
"This is a new problem we haven't seen before! Many times this question is asked in the form of dice (Given a 5 sided dice, simulate a 7 sided dice).\n",
31+
"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",
3232
"\n",
33-
"There are a few ways to do this, but the most straighforward way would be to try to create an even mapping "
33+
"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",
34+
"\n",
35+
"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",
36+
"\n",
37+
"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",
38+
"\n",
39+
"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 4. Now the first roll corresponds with a value of 1 - 20.\n",
40+
"\n",
41+
"Then we take the second roll and add it to the result of the first manipulation. Giving us a range of 1-25.\n",
42+
"\n",
43+
"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:"
3444
]
3545
},
3646
{
3747
"cell_type": "code",
38-
"execution_count": null,
48+
"execution_count": 1,
3949
"metadata": {
4050
"collapsed": true
4151
},
@@ -44,11 +54,74 @@
4454
"from random import randint\n",
4555
" \n",
4656
"def dice5():\n",
47-
" return randint(1, 5)\n",
48-
" \n",
49-
"def dice7():\n",
50-
" r = dice5() + dice5() * 5 - 6\n",
51-
" return (r % 7) + 1 if r < 21 else dice7()"
57+
" return randint(1, 5)"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"Now for our conversion function:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 24,
70+
"metadata": {
71+
"collapsed": false
72+
},
73+
"outputs": [],
74+
"source": [
75+
"def convert5to7():\n",
76+
"\n",
77+
" # For constant re-roll purposes\n",
78+
" while True:\n",
79+
"\n",
80+
" # Roll the dice twice\n",
81+
" roll_1 = dice5()\n",
82+
" roll_2 = dice5()\n",
83+
" \n",
84+
" #print 'The rolls were {} and {}'.format(roll_1,roll_2)\n",
85+
"\n",
86+
" # Convert the combination to the range 1 to 25\n",
87+
" num = ( (roll_1-1) * 5 ) + ( roll_2 ) \n",
88+
"\n",
89+
" #print 'The converted range number was:',num\n",
90+
" if num > 21:\n",
91+
"\n",
92+
" # re-roll if we are out of range\n",
93+
" continue\n",
94+
"\n",
95+
" return num %7 + 1 "
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 25,
101+
"metadata": {
102+
"collapsed": false
103+
},
104+
"outputs": [
105+
{
106+
"data": {
107+
"text/plain": [
108+
"2"
109+
]
110+
},
111+
"execution_count": 25,
112+
"metadata": {},
113+
"output_type": "execute_result"
114+
}
115+
],
116+
"source": [
117+
"convert5to7()"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"# Good Job!"
52125
]
53126
}
54127
],

Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 1 - SOLUTION.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"cell_type": "markdown",
9898
"metadata": {},
9999
"source": [
100-
"Now, the next problem (On-Site Question 2) will be harder, the reverse conversion of rolls! This question shoudl serve as a reminder not to overthink the solution to a question!\n",
100+
"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",
101101
"\n",
102102
"## Good Job!"
103103
]

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

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"___\n",
99
"\n",
1010
"## Question\n",
11-
"** Given a function which produces a random integer in the range of 1 to 7, write a function which produces a random integer from 1 to 5**\n",
11+
"** Given a dice which rolls from 1 to 5, simulate a uniform 7 sided dice! **\n",
1212
"\n",
1313
"## Requirements\n",
1414
"\n",
15-
"** You MUST do this on pen and paper or on a whiteboard. No actual coding is allowed! **"
15+
"** 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! **"
1616
]
1717
},
1818
{
@@ -28,14 +28,24 @@
2828
"source": [
2929
"# SOLUTION\n",
3030
"\n",
31-
"This is a new problem we haven't seen before! Many times this question is asked in the form of dice (Given a 5 sided dice, simulate a 7 sided dice).\n",
31+
"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",
3232
"\n",
33-
"There are a few ways to do this, but the most straighforward way would be to try to create an even mapping "
33+
"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",
34+
"\n",
35+
"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",
36+
"\n",
37+
"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",
38+
"\n",
39+
"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 4. Now the first roll corresponds with a value of 1 - 20.\n",
40+
"\n",
41+
"Then we take the second roll and add it to the result of the first manipulation. Giving us a range of 1-25.\n",
42+
"\n",
43+
"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:"
3444
]
3545
},
3646
{
3747
"cell_type": "code",
38-
"execution_count": null,
48+
"execution_count": 1,
3949
"metadata": {
4050
"collapsed": true
4151
},
@@ -44,11 +54,74 @@
4454
"from random import randint\n",
4555
" \n",
4656
"def dice5():\n",
47-
" return randint(1, 5)\n",
48-
" \n",
49-
"def dice7():\n",
50-
" r = dice5() + dice5() * 5 - 6\n",
51-
" return (r % 7) + 1 if r < 21 else dice7()"
57+
" return randint(1, 5)"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"Now for our conversion function:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 24,
70+
"metadata": {
71+
"collapsed": false
72+
},
73+
"outputs": [],
74+
"source": [
75+
"def convert5to7():\n",
76+
"\n",
77+
" # For constant re-roll purposes\n",
78+
" while True:\n",
79+
"\n",
80+
" # Roll the dice twice\n",
81+
" roll_1 = dice5()\n",
82+
" roll_2 = dice5()\n",
83+
" \n",
84+
" #print 'The rolls were {} and {}'.format(roll_1,roll_2)\n",
85+
"\n",
86+
" # Convert the combination to the range 1 to 25\n",
87+
" num = ( (roll_1-1) * 5 ) + ( roll_2 ) \n",
88+
"\n",
89+
" #print 'The converted range number was:',num\n",
90+
" if num > 21:\n",
91+
"\n",
92+
" # re-roll if we are out of range\n",
93+
" continue\n",
94+
"\n",
95+
" return num %7 + 1 "
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 25,
101+
"metadata": {
102+
"collapsed": false
103+
},
104+
"outputs": [
105+
{
106+
"data": {
107+
"text/plain": [
108+
"2"
109+
]
110+
},
111+
"execution_count": 25,
112+
"metadata": {},
113+
"output_type": "execute_result"
114+
}
115+
],
116+
"source": [
117+
"convert5to7()"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"# Good Job!"
52125
]
53126
}
54127
],

0 commit comments

Comments
 (0)