Microsoft| Onsite Interview | SDE 2 - USA

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2022-10-20Added By Anonymous
Source: Link

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:

1public static void main(String [] args){ 2int[][] arr = new int[][] {{1,2}, {3,2}, {6,8}, {9,8} {2,5}, {8,5}, {5,-1}}; 3print(arr); 4} 5 6public static void print(int[][] arr){ 7int number_nodes = arr.length; 8int[] visited = new int[arr.length]; 9for(int i=0; i<number_nodes; i++) 10 visited[i] = arr[i][0]; 11 12for(int i=0; i<arr.length; i++){ 13 if(arr[i][1] == -1){ 14 visited[i] =-1; 15 System.out.println(arr[i][0] +" -Root"); 16 }else{ 17 for(int j=0; j<arr.length; j++){ 18 if(visited[i] == -1) 19 j++; 20 21 else if(arr[i][0] == arr[j][1]){ 22 visited[i] = -1; 23 System.out.println(arr[i][0]+"- Inner"); 24 } 25 } 26 } 27} 28for(int i=0; i<visited.length; i++){ 29 if(visited[i] == -1) 30 System.out.println(visited[i]+ "-Leaf"); 31} 32 33}


}

}

Comments