Issue # 27: IT training - current issues and challenges from leading companies

Well, how did you manage to solve the problems from the last article? Answers already published !

And now we have new arrivals!



image



Issues will appear every week - stay tuned! The column is supported by the recruitment agency Spice IT .



This week we collected tasks from interviews at the Norwegian company Opera Software.



Questions



1. Pay an employee using a 7 units gold rod?

An employee works for an employer for 7 days. The employer has a gold rod of 7 units. How does the employer pays to the employee so that the employee gets 1 unit at the end of everyday? The employer can make at most 2 cuts in rod.


Transfer
The employee works 7 days. The employer has a gold rod of 7 units. How does the employer pay the employee so that the employee receives 1 unit at the end of each working day? The employer can make no more than 2 cuts in the rod.


2. The mystery of Einstein

This task is attributed to Albert Einstein - supposedly with his help he selected his assistants. Another almost legendary story attributes authorship to Lewis Caroll. Note that it is very easily solved on paper, but if you want hardcore, try to solve it in your mind.



There are five houses on the street.

The Englishman lives in a red house.

The Spaniard has a dog.

In the green house they drink coffee.

Ukrainian is drinking tea.

The green house stands immediately to the right of the white house.

Anyone who smokes Old Gold breeds snails.

In the yellow house smoke Kool.

In the central house they drink milk.

The Norwegian lives in the first house.

The neighbor of the smoker of Chesterfield holds a fox.

In the house next to the one in which the horse is kept, Kool is smoked.

Anyone who smokes Lucky Strike drinks orange juice.

Japanese smokes Parliament.

A Norwegian lives next to a blue house.

Each of the houses is painted in a different color, in each house there is a representative of a different nationality, each has its own pet, its own favorite brand of cigarettes and a drink.

Question: Who drinks water? Who holds a zebra?


Tasks



1. Product array puzzle

Given an array A of size N , construct a Product Array P (of the same size) such that P is equal to the product of all the elements of A except A [i] .



Input: Each testcase contains two lines of input. The first line is N. The second line contains N elements separated by spaces.



Output: For each testcase, print the Product array P.



Constraints:

1 <= T <= 10

1 <= N <= 1000

1 <= Ai <= 20








Example:

Input

5

10 3 5 6 2






Output

180 600 360 300 900





Input

2

12 20






Output

20 12







Explanation:

Testcase1: For the product array P, at i = 0 we have 3 * 5 * 6 * 2 . At i = 1 we have 10 * 5 * 6 * 2 . At i = 2 we have 10 * 3 * 6 * 2 . At i = 3 we have 10 * 3 * 5 * 2 . At i = 4 we have 10 * 3 * 5 * 6 .

So P is 180 600 360 300 900 .


Transfer
For an array A of size N, create an array of products P (of the same size) so that P is equal to the product of all elements of A except A [i] .



Input: each test case contains two input lines. The first line is N. The second line contains N elements separated by spaces.



Output: For each test output an array of products P.



Limitations:

1 <= T <= 10

1 <= N <= 1000

1 <= Ai <= 20








Example:

input

5

10 3 5 6 2






Exit

180 600 360 300 900





input

2

12 20






Exit

20 12







Explanation:

First test: for an array of products P with i = 0 we have 3 * 5 * 6 * 2 . For i = 1 we have 10 * 5 * 6 * 2 . For i = 2 we have 10 * 3 * 6 * 2 . For i = 3, we have 10 * 3 * 5 * 2 . For i = 4, we have 10 * 3 * 5 * 6 .

Thus, the array P consists of elements: 180 600 360 300 900


2. Egg Dropping Puzzle

Suppose you have N eggs and you want to determine from which floor in a K-floor building you can drop an egg such that it doesn't break. You have to determine the minimum number of attempts you need in order find the critical floor in the worst case while using the best strategy.

  • An egg that survives a fall can be used again.
  • A broken egg must be discarded.
  • The effect of a fall is the same for all eggs.
  • If the egg doesn't break at a certain floor, it will not break at any floor below.
  • If the eggs breaks at a certain floor, it will break at any floor above.


For more description on this problem see wiki page .



Input:

The first line of input is T denoting the number of testcases. Then each of the T lines contains two positive integer N and K where 'N' is the number of eggs and 'K' is number of floor in building.



Output:

For each test case, print a single line containing one integer the minimum number of attempt you need in order find the critical floor.



Constraints:

1<=T<=30

1<=N<=10

1<=K<=50








Example:

Input:

2

2 10

3 5








Output:

4

3






Transfer
Suppose you have N eggs and you want to determine from which floor of a K-story building you can drop an egg so that it does not break. You must determine the minimum number of attempts that you need to find the last floor from which the egg will not break, using the best strategy. Below are a few rules.



  • An egg that survives a fall can be used again.
  • A broken egg must be discarded.
  • The drop effect is the same for all eggs.
  • If an egg does not break on a particular floor, it will not break on any floor below.
  • If the eggs break on a particular floor, they break on any floor above.


See the wiki page for a more detailed description of this problem.



Input data:

The first line of input is T, indicating the number of test cases. Each of the T lines contains two positive integers N and K, where โ€œNโ€ is the number of eggs and โ€œKโ€ is the number of floors in the building.



Exit:

For each test case print on a separate line one integer - the minimum number of attempts necessary to determine the critical floor.



Limitations:

1 <= <= 30

1 <= N <= 10

1 <= K <= 50








Example:

Input data:

2

2 10

3 5








Exit:

4

3








3. Traveling Salesman Problem

Given a matrix M of size N where M [i] [j] denotes the cost of moving from city i to city j. Your task is to complete a tour from the city 0 (0 based index) to all other cities such that you visit each city atmost once and then at the end come back to city 0 in min cost.



Input:

The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the matrix then in the next line are N * N space separated values โ€‹โ€‹of the matrix M.



Output:

For each test case print the required result denoting the min cost of the tour in a new line.



Constraints:

1<=T<=15

1<=N<=12

1<=M[][]<=10000








Example:

Input:

2

2

0 111

112 0

3

0 1000 5000

5000 0 1000

1000 5000 0






Output:

223

3000






Transfer
A matrix M of size N is given, where M [i] [j] denotes the cost of moving from city i to city j. Your task is to make a trip from city 0 (index based on 0) to all other cities so that you visit each city at most once, and then return to city 0 at the end for the minimum cost.



Input data:

The first line of input contains an integer T, indicating the number of test cases. Then the T tests follow. Each test case contains an integer N, denoting the size of the matrix, the next line contains N * N values โ€‹โ€‹of the matrix M separated by spaces.



Exit:

For each test case, in a new line print the desired result indicating the minimum cost of the tour.



Limitations:

1 <= <= 15

1 <= N <= 12

1 <= [] [] <= 10000








Example:

Input data:

2

2

0 111

112 0

3

0 1000 5000

5000 0 1000

1000 5000 0






Exit:

223

3000








Answers to the tasks will be given during the next week - have time to solve it. Good luck



All Articles