Skip to content

Commit cafdda4

Browse files
committed
Taken Facebook 2nd phone interview. 3 problems asked and nailed it. More likely to pass this round and get invitation to onsite interviews :)
1 parent d46196a commit cafdda4

10 files changed

+871
-548
lines changed

Diff for: README.md

+558-548
Large diffs are not rendered by default.

Diff for: source-code/Baseball_Game.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int calPoints(vector<string>& ops) {
4+
stack<int> Stack;
5+
int sum = 0;
6+
for(int i = 0; i < ops.size(); i++) {
7+
if(ops[i] == "+") {
8+
int a = Stack.top();
9+
Stack.pop();
10+
int b = Stack.top();
11+
Stack.pop();
12+
int c = a + b;
13+
Stack.push(b); Stack.push(a); Stack.push(c);
14+
sum += c;
15+
} else if(ops[i] == "D") {
16+
int top = Stack.top();
17+
top <<= 1;
18+
sum += top;
19+
Stack.push(top);
20+
} else if(ops[i] == "C") {
21+
int top = Stack.top();
22+
Stack.pop();
23+
sum -= top;
24+
} else {
25+
int value = atoi(ops[i].c_str());
26+
sum += value;
27+
Stack.push(value);
28+
}
29+
}
30+
return sum;
31+
}
32+
};

Diff for: source-code/Employee_Importance.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
// Employee info
3+
class Employee {
4+
public:
5+
// It's the unique ID of each node.
6+
// unique id of this employee
7+
int id;
8+
// the importance value of this employee
9+
int importance;
10+
// the id of direct subordinates
11+
vector<int> subordinates;
12+
};
13+
*/
14+
class Solution {
15+
int getImportance(int id, vector<Employee*> const& employees, unordered_map<int, int>& employeeMap) {
16+
int totalImportance = employees[employeeMap[id]]->importance;
17+
for(int subordinateId : employees[employeeMap[id]]->subordinates) {
18+
totalImportance += getImportance(subordinateId, employees, employeeMap);
19+
}
20+
return totalImportance;
21+
}
22+
public:
23+
int getImportance(vector<Employee*> employees, int id) {
24+
unordered_map<int, int> employeeMap;
25+
for(int i = 0; i < employees.size(); i++) {
26+
employeeMap[employees[i]->id] = i;
27+
}
28+
return getImportance(id, employees, employeeMap);
29+
}
30+
};

Diff for: source-code/Implement_Magic_Dictionary.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
class MagicDictionary {
2+
class trie {
3+
class trieNode {
4+
public:
5+
bool mark;
6+
unordered_map<char, trieNode*> childrenMap;
7+
trieNode(): mark(false) {
8+
childrenMap = unordered_map<char, trieNode*>();
9+
}
10+
~trieNode() {
11+
for(auto it = childrenMap.begin(); it != childrenMap.end(); ++it) {
12+
delete it->second;
13+
}
14+
childrenMap = unordered_map<char, trieNode*>();
15+
}
16+
};
17+
18+
bool search(int indx, int ignored, string const& key, trieNode* pCrawl) {
19+
if(ignored > 1) {
20+
return false;
21+
}
22+
if(indx == key.length() - 1) {
23+
if(ignored == 0) {
24+
for(auto it = pCrawl->childrenMap.begin(); it != pCrawl->childrenMap.end(); ++it) {
25+
if(it->first != key[indx] and it->second->mark) {
26+
return true;
27+
}
28+
}
29+
return false;
30+
}
31+
return pCrawl->childrenMap.find(key[indx]) != pCrawl->childrenMap.end()
32+
and pCrawl->childrenMap[key[indx]]->mark
33+
and ignored == 1;
34+
}
35+
for(auto it = pCrawl->childrenMap.begin(); it != pCrawl->childrenMap.end(); ++it) {
36+
if(search(indx + 1, ignored + (it->first != key[indx]), key, it->second)) {
37+
return true;
38+
}
39+
}
40+
return false;
41+
}
42+
43+
trieNode* root;
44+
45+
public:
46+
trie(): root(new trieNode()) {
47+
}
48+
49+
~trie() {
50+
// delete root;
51+
root = nullptr;
52+
}
53+
54+
void insert(string const& key) {
55+
trieNode* pCrawl = root;
56+
for(int i = 0; i < key.length(); i++) {
57+
if(pCrawl->childrenMap.find(key[i]) == pCrawl->childrenMap.end()) {
58+
pCrawl->childrenMap[key[i]] = new trieNode();
59+
}
60+
pCrawl = pCrawl->childrenMap[key[i]];
61+
}
62+
pCrawl->mark = true;
63+
}
64+
65+
bool search(string const& key) {
66+
trieNode* pCrawl = root;
67+
return search(0, 0, key, root);
68+
}
69+
70+
};
71+
public:
72+
trie Trie;
73+
/** Initialize your data structure here. */
74+
MagicDictionary() {
75+
Trie = trie();
76+
}
77+
78+
/** Build a dictionary through a list of words */
79+
void buildDict(vector<string> dict) {
80+
for(string const& key: dict) {
81+
Trie.insert(key);
82+
}
83+
}
84+
85+
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
86+
bool search(string word) {
87+
return Trie.search(word);
88+
}
89+
};
90+
91+
/**
92+
* Your MagicDictionary object will be instantiated and called as such:
93+
* MagicDictionary obj = new MagicDictionary();
94+
* obj.buildDict(dict);
95+
* bool param_2 = obj.search(word);
96+
*/

Diff for: source-code/Longest_Univalue_Path.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
int longestUnivaluePath(TreeNode* root, int val) {
12+
if(!root or root->val != val) return 0;
13+
return 1 + max(longestUnivaluePath(root->left, val), longestUnivaluePath(root->right, val));
14+
}
15+
public:
16+
int longestUnivaluePath(TreeNode* root) {
17+
if(!root) return 0;
18+
int sub = max(longestUnivaluePath(root->left), longestUnivaluePath(root->right));
19+
return max(sub, longestUnivaluePath(root->left, root->val) + longestUnivaluePath(root->right, root->val));
20+
}
21+
};

Diff for: source-code/Next_Closest_Time.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// there are at most 4 * 4 * 4b* 4 = 256 possible times only. So lets try them all
2+
class Solution {
3+
void search(int indx, vector<int>& digits, string curr, string& result, int& diff, int const target) {
4+
if(indx == 4) {
5+
int total = atoi(curr.substr(0, 2).c_str()) * 60 + atoi(curr.substr(2).c_str());
6+
if(total == target) {
7+
return;
8+
}
9+
int currDiff = total - target > 0 ? total - target : 1440 + total - target;
10+
if(currDiff < diff) {
11+
diff = currDiff;
12+
result = curr.substr(0, 2) + ":" + curr.substr(2);
13+
}
14+
return;
15+
}
16+
for(int i = 0; i < digits.size(); i++) {
17+
if(indx == 0 and digits[i] > 2) { continue; }
18+
if(indx == 1 and atoi(curr.c_str()) * 10 + digits[i] > 23) { continue; }
19+
if(indx == 2 and digits[i] > 5) { continue; }
20+
if(indx == 3 and atoi(curr.substr(2).c_str()) * 10 + digits[i] > 59) { continue; }
21+
22+
search(indx + 1, digits, curr + to_string(digits[i]), result, diff, target);
23+
}
24+
}
25+
public:
26+
string nextClosestTime(string time) {
27+
vector<int> digits;
28+
unordered_set<int> digitSet;
29+
digitSet.insert(atoi(time.substr(0, 1).c_str()));
30+
digitSet.insert(atoi(time.substr(1, 1).c_str()));
31+
digitSet.insert(atoi(time.substr(3, 1).c_str()));
32+
digitSet.insert(atoi(time.substr(4).c_str()));
33+
if(digitSet.size() == 1) {
34+
return time;
35+
}
36+
for(auto it = digitSet.begin(); it != digitSet.end(); ++it) {
37+
digits.push_back(*it);
38+
}
39+
string result = "";
40+
int target = atoi(time.substr(0, 2).c_str()) * 60 + atoi(time.substr(3).c_str());
41+
int diff = INT_MAX;
42+
search(0, digits, "", result, diff, target);
43+
44+
return result;
45+
}
46+
};

Diff for: source-code/Redundant_Connection.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
int find(int u, vector<int>& parent) {
3+
return parent[u] != u ? parent[u] = find(parent[u], parent) : parent[u];
4+
}
5+
public:
6+
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
7+
int n = edges.size();
8+
vector<int> parent(n + 1);
9+
for(int i = 1; i <= n; i++) {
10+
parent[i] = i;
11+
}
12+
for(int i = 0; i < edges.size(); i++) {
13+
int u = edges[i][0], v = edges[i][1];
14+
u = find(u, parent), v = find(v, parent);
15+
if(u == v) {
16+
return edges[i];
17+
}
18+
parent[v] = u;
19+
}
20+
return vector<int>(2);
21+
}
22+
};

Diff for: source-code/Redundant_Connection_II.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
int find(int u, vector<int>& parent) {
3+
return parent[u] != u ? parent[u] = find(parent[u], parent) : parent[u];
4+
}
5+
public:
6+
vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
7+
int n = edges.size();
8+
vector<int> candidateA{-1, -1}, candidateB{-1, -1}; // edge pair pointing two parents for same node i.e. [2, 1], [3, 1]
9+
vector<int> parent(n + 1, 0);
10+
for(vector<int>& edge: edges) {
11+
if(parent[edge[1]] == 0) {
12+
parent[edge[1]] = edge[0];
13+
} else {
14+
candidateA = vector<int>{parent[edge[1]], edge[1]};
15+
candidateB = edge;
16+
edge[1] = 0;
17+
}
18+
}
19+
for(int i = 0; i <= n; i++) {
20+
parent[i] = i;
21+
}
22+
for(int i = 0; i < edges.size(); i++) {
23+
int u = edges[i][0], v = edges[i][1];
24+
if(v == 0) {
25+
continue;
26+
}
27+
u = find(u, parent), v = find(v, parent);
28+
if(u == v) {
29+
if(candidateA[0] == -1) {
30+
return edges[i];
31+
}
32+
return candidateA;
33+
}
34+
parent[v] = u;
35+
}
36+
return candidateB;
37+
}
38+
};

Diff for: source-code/Repeated_String_Match.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int repeatedStringMatch(string A, string B) {
4+
for(int i = 0, j = 0; i < A.length(); ++i) {
5+
for(j = 0; j < B.length() and A[(i + j) % A.length()] == B[j]; ++j);
6+
if(j == B.length()) {
7+
return (i + j) / A.length() + ((i + j) % A.length() != 0);
8+
}
9+
}
10+
return -1;
11+
}
12+
};
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int findUnsortedSubarray(vector<int>& nums) {
4+
vector<int> numsSorted(nums);
5+
sort(numsSorted.begin(), numsSorted.end());
6+
int n = nums.size();
7+
int left = 0, right = n - 1;
8+
while(left < n and nums[left] == numsSorted[left]) {
9+
left++;
10+
}
11+
while(right >= left and nums[right] == numsSorted[right]) {
12+
right--;
13+
}
14+
return max(0, right - left + 1);
15+
}
16+
};

0 commit comments

Comments
 (0)