Skip to content

Commit c0c71c3

Browse files
committed
Careem 2nd Phone interview successfully completed. Stood 16th in Topcoder SRM 722 division two with 3 problems solved and 3 successful challenges.
1 parent 91e1b9a commit c0c71c3

8 files changed

+643
-565
lines changed

README.md

Lines changed: 554 additions & 552 deletions
Large diffs are not rendered by default.

readme-generator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ int main(void) {
2828
}
2929
filename[n] = '\0';
3030
string sFileName = string(filename);
31+
// ignoring Mac's .DS_Store
32+
if(sFileName == ".DS_Store") {
33+
continue;
34+
}
3135
int dot = sFileName.find('.');
3236
assert(dot != string::npos);
3337
sFileName = sFileName.substr(0, dot); // trimming extension
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
public:
12+
vector<double> averageOfLevels(TreeNode* root) {
13+
vector<double> result;
14+
if(!root) {
15+
return result;
16+
}
17+
unordered_map<int, long long> sum;
18+
unordered_map<int, int> nodeCount;
19+
queue<pair<TreeNode*, int>> Q;
20+
int level = 0;
21+
Q.push({root, level});
22+
while(!Q.empty()) {
23+
TreeNode* curr = Q.front().first;
24+
int currLevel = Q.front().second;
25+
Q.pop();
26+
if(currLevel > level) {
27+
double avg = (double) sum[level] / nodeCount[level];
28+
result.push_back(avg);
29+
level = currLevel;
30+
}
31+
sum[level] += curr->val;
32+
nodeCount[level]++;
33+
if(curr->left) {
34+
Q.push({curr->left, currLevel + 1});
35+
}
36+
if(curr->right) {
37+
Q.push({curr->right, currLevel + 1});
38+
}
39+
}
40+
double avg = (double) sum[level] / nodeCount[level];
41+
result.push_back(avg);
42+
43+
return result;
44+
}
45+
};

source-code/Jump_Game_II.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ class Solution {
2626
return -1;
2727
}
2828
*/
29-
int jump(int A[], int n) {
29+
30+
int jump(vector<int>& A) {
31+
int n = (int)A.size();
3032
if(n < 2) return 0;
31-
int curr = 0;
3233
int steps = 0;
3334
for(int i = 0; i < n; ) {
34-
curr = max(curr, A[i] + i);
35-
if (curr > 0) steps++;
36-
37-
if (curr >= n - 1) return steps;
35+
int maxDistance = A[i] + i;
36+
steps++;
37+
if (maxDistance >= n - 1) return steps;
3838
int tmp = 0;
39-
for (int j = i + 1; j <= curr; j++) {
39+
for (int j = i + 1; j <= maxDistance; j++) {
4040
if (j + A[j] > tmp) {
4141
tmp = A[j] + j;
4242
i = j;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int maximumProduct(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
int n = (int)nums.size();
6+
int X = nums[n - 1] * nums[n - 2] * nums[n - 3];
7+
int Y = nums[0] * nums[1] * nums[n - 1];
8+
return max(X, Y);
9+
}
10+
};

source-code/Remove_Duplicates_from_Sorted_Array_II.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ class Solution {
22
public:
33
int removeDuplicates(vector<int>& nums) {
44
if(nums.size() <= 2) return nums.size();
5-
int len = 2;
5+
int k = 1;
66
for(int i = 2; i < nums.size(); i++) {
7-
if(nums[len - 2] < nums[i]) {
8-
nums[len++] = nums[i];
7+
if(nums[i] != nums[k - 1]) {
8+
nums[++k] = nums[i];
99
}
1010
}
11-
return len;
11+
return k + 1;
1212
}
1313
};

source-code/Symmetric_Tree.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
88
* };
99
*/
10+
// using level order traversal
1011
class Solution {
1112
public:
1213
bool isPalindrome(vector<int> &container) {
@@ -51,4 +52,21 @@ class Solution {
5152
}
5253
return true;
5354
}
55+
};
56+
57+
// using recursion
58+
class Solution {
59+
bool isSymmetric(TreeNode* root1, TreeNode* root2) {
60+
if(!root1 and !root2) {
61+
return true;
62+
}
63+
if(!root1 or !root2) {
64+
return false;
65+
}
66+
return root1->val == root2->val and isSymmetric(root1->left, root2->right) and isSymmetric(root1->right, root2->left);
67+
}
68+
public:
69+
bool isSymmetric(TreeNode* root) {
70+
return isSymmetric(root, root);
71+
}
5472
};

source-code/Wildcard_Matching.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Solution {
2-
// AC :) did pruning as much as I can, but still very laggy in cpu time graph
32
bool isMatchRecur(string const& txt, int p, string const& pattern, int q, vector<vector<bool>>& visited, vector<vector<bool>>& dp) {
43
if(p == txt.length() and q == pattern.length()) return true;
54
if(q == pattern.length()) return false;
@@ -18,7 +17,7 @@ class Solution {
1817
if(pattern[q] == '?') {
1918
return dp[p][q] = isMatchRecur(txt, p + 1, pattern, q + 1, visited, dp);
2019
} else if(pattern[q] == '*') {
21-
for(int i = 0; i <= txt.length(); ++i) {
20+
for(int i = 0; i <= txt.length() - p; ++i) {
2221
if(isMatchRecur(txt, p + i, pattern, q + 1, visited, dp)) {
2322
return dp[p][q] = true;
2423
}

0 commit comments

Comments
 (0)