## Linkedin Interview Question for Software Engineers

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
2
of 2 vote

Solution to Closest K Nodes

``````vector<int> closestKValues(TreeNode* root, double target, int k) {
vector<int> res;
inorder(root, target, k, res);
return res;
}
void inorder(TreeNode *root, double target, int k, vector<int> &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 - target)) {
res.erase(res.begin());
res.push_back(root->val);
} else return;
inorder(root->right, target, k, res);
}``````

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

Comment hidden because of low score. Click to expand.
0
of 0 vote

II nested list sum

``````t=[]
def flatten(l):
for i in l:
print(type(i),i)
if type(i)!=list:
t.append(i)
else:
flatten(i)
return t``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Version:

``````public class KClosestNodes {

public static void kClosestNodes(BinaryTreeNode root, int target, int k, Deque<Integer> q){
if(root == null)
return;
kClosestNodes(root.getLeft(), target, k, q);

if(q.size() < k){
}else if(Math.abs(target - root.getData()) < Math.abs(target - q.peekFirst())){
q.removeFirst();
}else{
return;
}
kClosestNodes(root.getRight(), target, k, q);
}

public static List<Integer> kClosestNodes(BinaryTreeNode root, int target, int k){
List<Integer> result = new ArrayList<>();

kClosestNodes(root, target, k, q);

while(!q.isEmpty()){
}

return result;
}
public static void main(String[] args) {
BinaryTreeNode root = buildBST();
int target = 21, k = 3;
System.out.println(kClosestNodes(root, target, k));

}

public static BinaryTreeNode buildBST(){
BinaryTreeNode node3 = new BinaryTreeNode(3);
BinaryTreeNode node5 = new BinaryTreeNode(5);
BinaryTreeNode node7 = new BinaryTreeNode(7);
BinaryTreeNode node20 = new BinaryTreeNode(20);

BinaryTreeNode node6 = new BinaryTreeNode(6);
node6.setLeft(node5);
node6.setRight(node7);

BinaryTreeNode node22 = new BinaryTreeNode(22);
node22.setRight(node20);

BinaryTreeNode node4 = new BinaryTreeNode(4);
node4.setLeft(node3);
node4.setRight(node6);

BinaryTreeNode node17 = new BinaryTreeNode(17);
node17.setRight(node22);

BinaryTreeNode root = new BinaryTreeNode(9);
root.setLeft(node4);
root.setRight(node17);

return root;
}
}

public class BinaryTreeNode {
private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public BinaryTreeNode(int data){
this.data = data;
this.left = null;
this.right = null;
}

public int getData(){
return this.data;
}

public void setData(int data){
this.data = data;
}

public BinaryTreeNode getLeft(){
return this.left;
}

public void setLeft(BinaryTreeNode left){
this.left = left;
}

public BinaryTreeNode getRight(){
return this.right;
}

public void setRight(BinaryTreeNode right){
this.right = right;
}
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.