Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4cc4652

Browse files
author
kaidul
committed
Bloomberg onsite ahead. Google phone interview ahead
1 parent 5f09c4d commit 4cc4652

19 files changed

+432
-139
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
TreeNode* constructFromPrePost(int preLeft, int preRight, vector<int>& preOrder,
12+
int postLeft, int postRight, vector<int>& postOrder) {
13+
if(preLeft > preRight) {
14+
return nullptr;
15+
}
16+
TreeNode* root = new TreeNode(preOrder[preLeft]);
17+
if(preLeft == preRight) {
18+
return root;
19+
}
20+
21+
int pivot = -1;
22+
for(int i = preLeft + 1; i <= preRight; i++) {
23+
if(preOrder[i] == postOrder[postRight - 1]) {
24+
pivot = i;
25+
break;
26+
}
27+
}
28+
root->left = constructFromPrePost(preLeft + 1, pivot - 1, preOrder,
29+
postLeft, postLeft + pivot - 1 - preLeft - 1, postOrder);
30+
root->right = constructFromPrePost(pivot, preRight, preOrder,
31+
postRight - 1 - preRight + pivot, postRight - 1, postOrder);
32+
33+
return root;
34+
}
35+
36+
public:
37+
TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
38+
return constructFromPrePost(0, pre.size() - 1, pre, 0, post.size() - 1, post);
39+
}
40+
};

‎source-code/Find_the_Duplicate_Number.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// O(n)
2+
class Solution {
3+
public:
4+
int findDuplicate(vector<int>& nums) {
5+
for(int i = 0; i < nums.size(); i++) {
6+
int indx = abs(nums[i]) - 1;
7+
if(nums[indx] < 0) {
8+
return indx + 1;
9+
}
10+
nums[indx] = -nums[indx];
11+
}
12+
return INT_MIN;
13+
}
14+
};
15+
116
class Solution {
217
int countNumbers(vector<int> const& nums, int mid) {
318
int cnt = 0;

‎source-code/LRU_Cache.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,45 @@ class LRUCache {
8282
tail = node;
8383
}
8484
}
85+
};
86+
87+
// using C++ list. got RE for memory corruption though :(
88+
class LRUCache {
89+
public:
90+
LRUCache(int capacity) {
91+
entries = list<pair<int, int>> ();
92+
table = unordered_map<int, list<pair<int, int>>::iterator> ();
93+
LRUCache::capacity = capacity;
94+
}
95+
96+
int get(int key) {
97+
if (table.find(key) != table.end()) {
98+
auto node = table[key];
99+
pair<int, int> entry = *node;
100+
entries.erase(node);
101+
entries.push_front(entry);
102+
return entry.second;
103+
}
104+
return -1;
105+
}
106+
107+
void put(int key, int value) {
108+
pair<int, int> newEntry = {key, value};
109+
if (table.find(key) != table.end()) {
110+
auto curr = table[key];
111+
entries.erase(curr);
112+
} else {
113+
if(entries.size() >= capacity) {
114+
pair<int, int> tailEntry = entries.back();
115+
entries.pop_back();
116+
table.erase(tailEntry.first);
117+
}
118+
}
119+
entries.push_front(newEntry);
120+
table[key] = entries.begin();
121+
}
122+
private:
123+
list<pair<int, int>> entries;
124+
unordered_map<int, list<pair<int, int>>::iterator> table;
125+
int capacity;
85126
};

‎source-code/Leaf-Similar_Trees.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
void leafSimilarUtil(TreeNode* root, vector<int>& leafSequence) {
12+
if(!root) return;
13+
if(!root->left and !root->right) {
14+
leafSequence.push_back(root->val);
15+
return;
16+
}
17+
leafSimilarUtil(root->left, leafSequence);
18+
leafSimilarUtil(root->right, leafSequence);
19+
}
20+
21+
public:
22+
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
23+
vector<int> leafSequence1;
24+
leafSimilarUtil(root1, leafSequence1);
25+
vector<int> leafSequence2;
26+
leafSimilarUtil(root2, leafSequence2);
27+
28+
return leafSequence1 == leafSequence2;
29+
}
30+
};

‎source-code/Palindrome_Linked_List.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
// Recursive
1010
class Solution {
1111
public:
12-
void isPalindrome(ListNode* &head, ListNode* tail, int& i, int j, bool& flag) {
12+
void isPalindrome(ListNode* &head, ListNode* tail, int& left, int right, bool& flag) {
1313
if(!tail or !flag) return;
14-
isPalindrome(head, tail->next, i, j + 1, flag);
15-
if(i > j) return;
14+
isPalindrome(head, tail->next, left, right + 1, flag);
15+
if(left >= right) return;
1616
if(head->val != tail->val) {
1717
flag = false;
1818
}
1919
head = head->next;
20-
i++;
20+
left++;
2121
}
2222
bool isPalindrome(ListNode* head) {
2323
int i = 0;

‎source-code/Path_Sum.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class Solution {
1212
bool hasPathSum(TreeNode *root, int sum) {
1313
if (!root) return false;
1414
if (!root->left and !root->right and sum - root->val == 0) return true;
15-
return (hasPathSum(root->left, sum - root->val) or hasPathSum(root->right, sum - root->val));
15+
if(hasPathSum(root->left, sum - root->val)) {
16+
return true;
17+
}
18+
return hasPathSum(root->right, sum - root->val);
1619
}
1720
};

‎source-code/Permutation_Sequence.cpp‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
class Solution {
22
public:
3-
void swap(string &str, int idx, int k) {
4-
for(int i = idx + k; i > idx; --i) {
5-
char tmp = str[i];
6-
str[i] = str[i - 1];
7-
str[i - 1] = tmp;
3+
void shift(string& permutation, int idx, int k) {
4+
for(int i = idx + k - 1; i >= idx; --i) {
5+
swap(permutation[i], permutation[i + 1]);
86
}
97
}
10-
void getPermutationUtils(string &str, int idx, int n, int k, int *fact) {
8+
9+
void getPermutationUtils(string& permutation, int idx, int k, vector<int>& fact) {
10+
int n = (int) permutation.length();
1111
if (k == 0 or idx == n) return;
12-
swap(str, idx, k / fact[n - idx - 1]);
13-
getPermutationUtils(str, idx + 1, n, k % fact[n - idx - 1], fact);
12+
shift(permutation, idx, k / fact[n - idx - 1]);
13+
getPermutationUtils(permutation, idx + 1, k % fact[n - idx - 1], fact);
1414
}
15+
1516
string getPermutation(int n, int k) {
16-
string str = "";
17-
int fact[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; //n factorail
17+
string permutation = "";
18+
vector<int> fact{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; //n factorial
1819
for (int i = 1; i <= n; i++) {
19-
str += char(i + '0');
20+
permutation += char(i + '0');
2021
}
21-
getPermutationUtils(str, 0, n, k - 1, fact);
22-
return str;
22+
getPermutationUtils(permutation, 0, k - 1, fact);
23+
return permutation;
2324

2425
}
2526
};

‎source-code/Populating_Next_Right_Pointers_in_Each_Node_II.cpp‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,41 @@
99
class Solution {
1010
public:
1111
void connect(TreeLinkNode *root) {
12-
if(!root) return;
13-
queue <pair <TreeLinkNode*, int> > Q;
14-
Q.push(make_pair(root, 1));
15-
pair <TreeLinkNode*, int> node;
16-
int prev_level = 0, curr_level;
17-
TreeLinkNode *curr = nullptr, *head = nullptr;
18-
while(!Q.empty()) {
19-
node = Q.front(), Q.pop();
20-
curr = node.first, curr_level = node.second;
21-
if(curr_level > prev_level) {
22-
if(head) head->next = nullptr;
23-
head = curr;
24-
} else {
25-
head->next = curr;
26-
head = head->next;
12+
while (root) {
13+
TreeLinkNode* sentinel = new TreeLinkNode(INT_MIN);
14+
TreeLinkNode* currNode = sentinel;
15+
while (root) {
16+
if (root->left) {
17+
currNode->next = root->left;
18+
currNode = currNode->next;
19+
}
20+
if (root->right) {
21+
currNode->next = root->right;
22+
currNode = currNode->next;
23+
}
24+
root = root->next;
2725
}
28-
if(curr->left) Q.push(make_pair(curr->left, curr_level + 1));
29-
if(curr->right) Q.push(make_pair(curr->right, curr_level + 1));
30-
prev_level = curr_level;
26+
root = sentinel->next;
27+
}
28+
}
29+
};
30+
31+
// using recursion in inorder fashion
32+
class Solution {
33+
public:
34+
void flatten(TreeNode* root) {
35+
if(!root) return;
36+
37+
flatten(root->left);
38+
39+
TreeNode* right = root->right;
40+
root->right = root->left;
41+
root->left = nullptr;
42+
while(root->right) {
43+
root = root->right;
3144
}
45+
root->right = right;
46+
47+
flatten(right);
3248
}
3349
};

‎source-code/Reverse_Bits.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,35 @@ class Solution {
1111
}
1212
return n;
1313
}
14+
};
15+
16+
17+
// optimization by lookup table
18+
class Solution {
19+
static unsigned char BitReverseTable256[256] =
20+
{
21+
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
22+
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
23+
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
24+
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
25+
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0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0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
37+
};
38+
public:
39+
uint32_t reverseBits(uint32_t n) {
40+
return (BitReverseTable256[n & 0xff] << 24) |
41+
(BitReverseTable256[(n >> 8) & 0xff] << 16) |
42+
(BitReverseTable256[(n >> 16) & 0xff] << 8) |
43+
(BitReverseTable256[(n >> 24) & 0xff]);
44+
}
1445
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /