Skip to content

Commit 403c20b

Browse files
committed
almost done
1 parent df7f184 commit 403c20b

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

Mock Interviews/Social Network Company/Social Network Company - Interview Questions - SOLUTIONS/.ipynb_checkpoints/On-Site Question 2 - SOLUTION-checkpoint.ipynb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,51 @@
1515
"** Do not use built-in Python functions or methods **"
1616
]
1717
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"source": [
24+
"## Solution\n",
25+
"\n",
26+
"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",
27+
"\n",
28+
"In Python we can use the ^ symbol to perform an XOR.\n",
29+
"\n",
30+
"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",
31+
"\n",
32+
"By the end, we wil be left with the ID that was unique and only appeared once!"
33+
]
34+
},
1835
{
1936
"cell_type": "code",
20-
"execution_count": null,
37+
"execution_count": 2,
2138
"metadata": {
2239
"collapsed": true
2340
},
2441
"outputs": [],
25-
"source": []
42+
"source": [
43+
"def solution(id_list):\n",
44+
" \n",
45+
" # Initiate unique Id\n",
46+
" unique_id = 0\n",
47+
" \n",
48+
" # XOR fo revery id in id list\n",
49+
" for i in id_list:\n",
50+
" \n",
51+
" # XOR operation\n",
52+
" unique_id ^= i\n",
53+
" \n",
54+
" return unique_id"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"# Good Job!"
62+
]
2663
}
2764
],
2865
"metadata": {

Mock Interviews/Social Network Company/Social Network Company - Interview Questions - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,51 @@
1515
"** Do not use built-in Python functions or methods **"
1616
]
1717
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"source": [
24+
"## Solution\n",
25+
"\n",
26+
"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",
27+
"\n",
28+
"In Python we can use the ^ symbol to perform an XOR.\n",
29+
"\n",
30+
"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",
31+
"\n",
32+
"By the end, we wil be left with the ID that was unique and only appeared once!"
33+
]
34+
},
1835
{
1936
"cell_type": "code",
20-
"execution_count": null,
37+
"execution_count": 2,
2138
"metadata": {
2239
"collapsed": true
2340
},
2441
"outputs": [],
25-
"source": []
42+
"source": [
43+
"def solution(id_list):\n",
44+
" \n",
45+
" # Initiate unique Id\n",
46+
" unique_id = 0\n",
47+
" \n",
48+
" # XOR fo revery id in id list\n",
49+
" for i in id_list:\n",
50+
" \n",
51+
" # XOR operation\n",
52+
" unique_id ^= i\n",
53+
" \n",
54+
" return unique_id"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"# Good Job!"
62+
]
2663
}
2764
],
2865
"metadata": {

0 commit comments

Comments
 (0)