I had Microsoft onsite today.
Included 2 technical rounds and 1 round with Hiring Manager
Round 1: Pretty startightforward questions:
2 Sum Follow up Question was 3 Sum
Round 2 :
The Question was: given value retrieved from the table like below:
N P
1 2
3 2
6 8
9 8
2 5
8 5
5 NULL
This is a 2D array, where N is Nodes and P is the Parent of node Print if Node is Root, Leaf or Inner.
eg-
5- Root
8-Inner
1- Leaf
and so on for each node.
Simple again but I got deviated by input type, didn't do well with me. I thought I am being given a TreeNode root as input, but the interviewer said no it's just a 2D array.
Here is my code:
public static void main(String [] args){
int[][] arr = new int[][] {{1,2}, {3,2}, {6,8}, {9,8} {2,5}, {8,5}, {5,-1}};
print(arr);
}
public static void print(int[][] arr){
int number_nodes = arr.length;
int[] visited = new int[arr.length];
for(int i=0; i<number_nodes; i++)
visited[i] = arr[i][0];
for(int i=0; i<arr.length; i++){
if(arr[i][1] == -1){
visited[i] =-1;
System.out.println(arr[i][0] +" -Root");
}else{
for(int j=0; j<arr.length; j++){
if(visited[i] == -1)
j++;
else if(arr[i][0] == arr[j][1]){
visited[i] = -1;
System.out.println(arr[i][0]+"- Inner");
}
}
}
}
for(int i=0; i<visited.length; i++){
if(visited[i] == -1)
System.out.println(visited[i]+ "-Leaf");
}
}