Skip to content

Commit df7f184

Browse files
committed
working hard on xmas!
1 parent 846a491 commit df7f184

File tree

12 files changed

+418
-150
lines changed

12 files changed

+418
-150
lines changed

Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/.ipynb_checkpoints/On-Site Question 3 - SOLUTION-checkpoint.ipynb

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,92 @@
88
"\n",
99
"## Problem\n",
1010
"\n",
11-
"** Given a long string sentence, create a dictionary that counts the occurences of each word. Capitalized words should be the same as lwoercase, count wise. For example: **"
11+
"** Given a binary tree, check whether it’s a binary search tree or not. **"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"## Requirements\n",
19+
"\n",
20+
"** Use paper/pencil, do not code this in an IDE until you've done it manually**\n",
21+
"\n",
22+
"** Do not use built-in Python libraries to do this, but do mention them if you know about them **"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"## Solution\n",
30+
"\n",
31+
"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",
32+
"\n",
33+
"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. "
1234
]
1335
},
1436
{
1537
"cell_type": "code",
16-
"execution_count": 1,
38+
"execution_count": 2,
1739
"metadata": {
1840
"collapsed": false
1941
},
20-
"outputs": [
21-
{
22-
"data": {
23-
"text/plain": [
24-
"{'about': 2, 'and': 1, 'that': 1, 'this': 1}"
25-
]
26-
},
27-
"execution_count": 1,
28-
"metadata": {},
29-
"output_type": "execute_result"
30-
}
31-
],
42+
"outputs": [],
3243
"source": [
33-
"s = 'About this, and about that'\n",
34-
"def word_count(s):\n",
35-
" # Fill me in\n",
36-
" pass\n",
44+
"class Node: \n",
45+
" def __init__(self, val=None): \n",
46+
" self.left, self.right, self.val = None, None, val \n",
47+
" \n",
48+
"INFINITY = float(\"infinity\") \n",
49+
"NEG_INFINITY = float(\"-infinity\") \n",
3750
"\n",
38-
"# Sample output\n",
39-
"word_count(s)"
51+
"def isBST(tree, minVal=NEG_INFINITY, maxVal=INFINITY): \n",
52+
" if tree is None:\n",
53+
" return True \n",
54+
" if not minVal <= tree.val <= maxVal: \n",
55+
" return False \n",
56+
" \n",
57+
" return isBST(tree.left, minVal, tree.val) and isBST(tree.right, tree.val, maxVal) "
4058
]
4159
},
4260
{
4361
"cell_type": "markdown",
4462
"metadata": {},
4563
"source": [
46-
"## Requirements\n",
47-
"\n",
48-
"** Use paper/pencil, do not code this in an IDE (writing it down makes the problem harder)**\n",
49-
"** Do not use built-in Python libraries to do this, but do mention them if you know about them **"
64+
"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."
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 3,
70+
"metadata": {
71+
"collapsed": true
72+
},
73+
"outputs": [],
74+
"source": [
75+
"def isBST2(tree, lastNode=[NEG_INFINITY]): \n",
76+
" \n",
77+
" if tree is None: \n",
78+
" return True \n",
79+
" \n",
80+
" if not isBST2(tree.left, lastNode):\n",
81+
" return False \n",
82+
" \n",
83+
" if tree.val < lastNode[0]: \n",
84+
" return False \n",
85+
" \n",
86+
" lastNode[0]=tree.val \n",
87+
" \n",
88+
" return isBST2(tree.right, lastNode) "
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"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",
96+
"# Good Job!"
5097
]
5198
}
5299
],

Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 3 - SOLUTION.ipynb

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,92 @@
88
"\n",
99
"## Problem\n",
1010
"\n",
11-
"** Given a long string sentence, create a dictionary that counts the occurences of each word. Capitalized words should be the same as lowercase, count wise. For example: **"
11+
"** Given a binary tree, check whether it’s a binary search tree or not. **"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"## Requirements\n",
19+
"\n",
20+
"** Use paper/pencil, do not code this in an IDE until you've done it manually**\n",
21+
"\n",
22+
"** Do not use built-in Python libraries to do this, but do mention them if you know about them **"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"## Solution\n",
30+
"\n",
31+
"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",
32+
"\n",
33+
"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. "
1234
]
1335
},
1436
{
1537
"cell_type": "code",
16-
"execution_count": 1,
38+
"execution_count": 2,
1739
"metadata": {
1840
"collapsed": false
1941
},
20-
"outputs": [
21-
{
22-
"data": {
23-
"text/plain": [
24-
"{'about': 2, 'and': 1, 'that': 1, 'this': 1}"
25-
]
26-
},
27-
"execution_count": 1,
28-
"metadata": {},
29-
"output_type": "execute_result"
30-
}
31-
],
42+
"outputs": [],
3243
"source": [
33-
"s = 'About this, and about that'\n",
34-
"def word_count(s):\n",
35-
" # Fill me in\n",
36-
" pass\n",
44+
"class Node: \n",
45+
" def __init__(self, val=None): \n",
46+
" self.left, self.right, self.val = None, None, val \n",
47+
" \n",
48+
"INFINITY = float(\"infinity\") \n",
49+
"NEG_INFINITY = float(\"-infinity\") \n",
3750
"\n",
38-
"# Sample output\n",
39-
"word_count(s)"
51+
"def isBST(tree, minVal=NEG_INFINITY, maxVal=INFINITY): \n",
52+
" if tree is None:\n",
53+
" return True \n",
54+
" if not minVal <= tree.val <= maxVal: \n",
55+
" return False \n",
56+
" \n",
57+
" return isBST(tree.left, minVal, tree.val) and isBST(tree.right, tree.val, maxVal) "
4058
]
4159
},
4260
{
4361
"cell_type": "markdown",
4462
"metadata": {},
4563
"source": [
46-
"## Requirements\n",
47-
"\n",
48-
"** Use paper/pencil, do not code this in an IDE (writing it down makes the problem harder)**\n",
49-
"** Do not use built-in Python libraries to do this, but do mention them if you know about them **"
64+
"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."
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 3,
70+
"metadata": {
71+
"collapsed": true
72+
},
73+
"outputs": [],
74+
"source": [
75+
"def isBST2(tree, lastNode=[NEG_INFINITY]): \n",
76+
" \n",
77+
" if tree is None: \n",
78+
" return True \n",
79+
" \n",
80+
" if not isBST2(tree.left, lastNode):\n",
81+
" return False \n",
82+
" \n",
83+
" if tree.val < lastNode[0]: \n",
84+
" return False \n",
85+
" \n",
86+
" lastNode[0]=tree.val \n",
87+
" \n",
88+
" return isBST2(tree.right, lastNode) "
89+
]
90+
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"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",
96+
"# Good Job!"
5097
]
5198
}
5299
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# On-Site Question 1 - SOLUTION\n",
8+
"\n",
9+
"## Problem\n",
10+
"\n",
11+
"** 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",
12+
"\n",
13+
"## Requirements\n",
14+
"\n",
15+
"** Try pen/paper before coding out your solution **\n",
16+
"\n",
17+
"** You can not use an integer element twice. Optimize for time over space **"
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"collapsed": true
24+
},
25+
"source": [
26+
"## Solution\n",
27+
"\n",
28+
"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",
29+
"\n",
30+
"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",
31+
"\n",
32+
"We will then update our seen set by adding the current number in the iteration to it.\n",
33+
"\n",
34+
"Let's see this coded out:"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 3,
40+
"metadata": {
41+
"collapsed": true
42+
},
43+
"outputs": [],
44+
"source": [
45+
"def solution(lst,target):\n",
46+
" \n",
47+
" # Create set to keep track of duplicates\n",
48+
" seen = set()\n",
49+
" \n",
50+
" # We want to find if there is a num2 that sums with num to reach the target\n",
51+
" \n",
52+
" for num in lst:\n",
53+
" \n",
54+
" num2 = target - num\n",
55+
" \n",
56+
" if num2 in seen:\n",
57+
" return True\n",
58+
" \n",
59+
" seen.add(num)\n",
60+
" \n",
61+
" # If we never find a pair match which creates the sum\n",
62+
" return False"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 6,
68+
"metadata": {
69+
"collapsed": false
70+
},
71+
"outputs": [
72+
{
73+
"data": {
74+
"text/plain": [
75+
"True"
76+
]
77+
},
78+
"execution_count": 6,
79+
"metadata": {},
80+
"output_type": "execute_result"
81+
}
82+
],
83+
"source": [
84+
"solution([1,3,5,1,7],4)"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 7,
90+
"metadata": {
91+
"collapsed": false
92+
},
93+
"outputs": [
94+
{
95+
"data": {
96+
"text/plain": [
97+
"False"
98+
]
99+
},
100+
"execution_count": 7,
101+
"metadata": {},
102+
"output_type": "execute_result"
103+
}
104+
],
105+
"source": [
106+
"solution([1,3,5,1,7],14)"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"# Good Job!"
114+
]
115+
}
116+
],
117+
"metadata": {
118+
"kernelspec": {
119+
"display_name": "Python 2",
120+
"language": "python",
121+
"name": "python2"
122+
},
123+
"language_info": {
124+
"codemirror_mode": {
125+
"name": "ipython",
126+
"version": 2
127+
},
128+
"file_extension": ".py",
129+
"mimetype": "text/x-python",
130+
"name": "python",
131+
"nbconvert_exporter": "python",
132+
"pygments_lexer": "ipython2",
133+
"version": "2.7.11"
134+
}
135+
},
136+
"nbformat": 4,
137+
"nbformat_minor": 0
138+
}

0 commit comments

Comments
 (0)