(This question has been seen in the interviews of the following companies: Linkedin)
1, Closest K nodes to a target in BST? (Do it in O(n)?)
vector closestKValues(TreeNode* root, double target, int k) {
vector res;
inorder(root, target, k, res);
return res;
}
void inorder(TreeNode *root, double target, int k, vector &res) {
if (!root) return;
inorder(root->left, target, k, res);
if (res.size() < k) res.push_back(root->val);
else if (abs(root->val - target) < abs(res[0] - target)) {
res.erase(res.begin());
res.push_back(root->val);
} else return;
inorder(root->right, target, k, res);
}
2, Nested List sum?
3, III. Square root of a number?
int sqrt(int x) {
if (x == 0)
return x;
int left = 1, right = x;
while (true) {
int mid = (left + right) / 2;
if (mid > x / mid)
right = mid - 1;
else if (mid + 1 > x / (mid + 1)) //mid < x / mid
return mid;
else //mid < x / mid
left = mid + 1;
}
}
4, Expression operators? Add signs (+, -, *, /) to a string to form target
5, Top trending posts in last 5m, 1H, 1Day?
Get one-to-one training from Google Facebook engineers
Top-notch Professionals
Learn from Facebook and Google senior engineers interviewed 100+ candidates.
Most recent interview questions and system design topics gathered from aonecode alumnus.
One-to-one online classes. Get feedbacks from real interviewers.
Customized Private Class
Already a coding expert? - Advance straight to hard interview topics of your interest.
New to the ground? - Develop basic coding skills with your own designated mentor.
Days before interview? - Focus on most important problems in target company question bank.