# National Instruments Interview Experience (Off-campus)

Hiring was for Internship. The first round was a coding test on HackerEarth. There were two questions. Both carried 100 marks each.

**1.Min-Max Weighted Edge**

Given a tree with N nodes and N- 1 bidirectional edges, and given an integer S. Now, you have to assign the weights to the edges of this tree such that:

1. the sum of the weights of all the edges is equal to S.

2. for every possible diameter of the tree, the maximum weight over all the edges covered by its path is the minimum possible.

You have to output the minimum possible edge weight.

**Note:** The diameter of a tree is the number of nodes on the longest path between two leaves in the tree.

Input Format:

The first line of the input contains an integer T the total number of test cases.

The first line of each test case contains two space-separated integers N and S, the number of nodes in a tree and the integer S as mentioned in the problem statement.

Then the N – L lines follow, each containing two space separated integers and u representing that there is a bidirectional edge between the nodes u and v.

Output Format:

For each test case output the minimum possible edge weight which satisfies the above-mentioned conditions.Constraints

1 <= T <= 10

1 <= N <= 2 x 10^3

1 <= u, v <= N

1 <= S <= 10^9

Examples:

Input :

2

8 18

1 2

1 3

2 4

2 5

2 6

3 7

3 8

7 15

1 2

1 3

1 4

2 5

2 6

3 7

Output :

3

0

Explanation

Sample test case 1: Given below is one of the best ways to assign weights to the edges

Sample test case 2: Note that there are only two possible diameters of the tree: 5 to 7 and 6 to 7, so we can assign S to the edge {1, 4} and for every possible diameter of the tree, the maximum weight over all the edges covered by its path is 0.

**2. Colorful Buildings**

There are N buildings in a row. Each of these buildings needs to be painted by one of the K colors. Buildings look beautiful only if no adjacent buildings are painted with the same color. Find the number of ways to paint these buildings such that they are beautiful. Since the number could be very large output it modulo 10^9+7.

Input Format

The first line of the input contains an integer T, the total number of test cases.

Then T lines follow, each containing two space-separated integers N and K, the total number of buildings and the number of colors available.

Output Format

For each test case output the number of ways to paint the buildings such that they are beautiful modulo 10^9+7.

Constraints

1 <= T <= 10

1 <= N <= 10^5

2 <= K <= 10^5

Sample Input

2

4 2

4 6

Sample Output

2

750

Explanation

Sample test case 1: Let A and B be the two available colors then there are just two possible ways of coloring the buildings as ABAB and BABA.

Shortlisted candidates were called for interview at their Bangalore office. The second round was also coding round. There were two questions.

**1. Board Game**

The Hobbits once asked Gandlaf to prepare a board game for them. So the Gandlaf came up with something like this. There would be a m x n chess like board with each block containing a number. One can start at any number and move either rightwards or downwards such that:

1. The number on the right is **not less **than the current number.

2. The number on below is **not greater **than the current number.

Given these constraints, what is the maximum number of moves that can be made starting at any number.

Input Format

M – The number of rows

N – The number of columns

Followed by the numbers present on the board

Output Format

Maximum number of moves that can be made.

Sample Input 1:

3 3

191

2 31

3 41

SampleOutput 1:

4

Sample Input 2:

2 2

2 1

3 2

SampleOutput 2:

1

Sample Input 3:

2 2

45

3 6

SampleOutput 3:

3

**2. Skynet**

Skynet has grown to become the dominant force on earth and has almost completely wiped out the human race. Skynet has been building robots ever since it’s inception and has been updating it’s models every year while making them better. Skynet wants to annihilate humanity completely. It plans to remove one last band of humans lead by John Connor. Skynet thinks it can destroy these humans using only two of it’s robots. But Skynet doesn’t want to send two robots with the same model number lest John Connor finds out a weakness in that model and easily destroy both of them.

Skynet has at its disposal N robots and to save space Skynet has stored information about pairs of robots belonging to the same model. If it doesn’t have any info stored for a particular robot then it is implied that the robot is the only one in that model.

Given these constraints, in how many ways can Skynet pick two robots to destroy John Connor and his rag tag group of humans.

Input Format

N – Total number of robots. Each robot is assigned a number from 0 to N-1

P – Number of pairs for which Skynet has information

This is followed by P pairs. Each pair has two numbers P1 and P2 each where 0<=P1<=N-1 and 0<=P2<=N-1 and P1 != P2

OutputFormat

Number of ways in which Skynet can select 2 robots such that both the robots are different models.

Sample Input 1:

4 2

0 1

2 3

SampleOutput 1:

4

Explanation: Here robots 0 and 1 are of one model, say model A. And 2 and 3 are of another model, say B. Therefore the total number of possibilities of picking 2 robots such that no two robots are of the same model are – (0, 2), (0, 3), (1, 2) and (1, 3) = 4

Sample Input 2:

10 4

2 8

1 3

0 2

5 8

Sample Output 2:

38

Shortlisted candidates had face to face interview. There were two people in the panel. There was discussion about the logic of the two questions and their implementation. They asked about its time complexity and ways to optimize it. They gave another problem to solve.

There would be a 2D array with values 0 and 1 in it. 0 representing the ocean and 1 representing the land. Output the number of islands present.

Sample Input 1:

3 3

1 1 0

0 1 0

0 0 1

Sample Output 1:

2

Explanation:

The land is considered as one island if it is either stand alone or it can be reached from one land to other either vertically or horizontally but not diagonally. So the lands in first two rows are considered as one island and the one in the third row is considered as another one.

Sample Input 2:

3 3

0 1 1

0 1 0

1 1 0

Sample Output 2:

1

Sample Input 3:

3 3

0 1 0

0 0 1

1 0 1

Sample Output 3:

3

They asked about my most challenging situation until now and previous internship experience. Then they asked me if I had any question or feedback about the company.