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

Urgent in the room! Revival of the heading IT training. We again gathered the questions and tasks asked at interviews in IT companies.



image



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



Today we have tasks - of very different levels of difficulty - from interviews to the Indian company Flipkart. Well, have passed the social security?



Questions



1. Thief, Treasure and 2 Doors

A thief has just found a pair of ancient treasure caves. One of the caves is filled with unbelievable treasure and the other has a fire breathing monster that will eat anyone who opens that cave.

One cave has a black door decorated with diamonds and the other cave has a brown door decorated with sapphires.

Each of the doors has an engraved description on top. The descriptions say:



Black Door: Monster is here.

Brown Door: Only One Door speaks the truth.



Which door should the thief open?


Transfer
The thief just found a couple of ancient caves. One of the caves is filled with incredible treasures, and in another there is a fire-breathing monster that will eat anyone who opens this cave.

The entrance to the first cave is blocked by a black door decorated with diamonds, and into another - a brown door decorated with sapphires.

Each door has an engraved description on top. Descriptions read:



Black door: the monster is here.

Brown door: only one door is telling the truth.



What door should a thief open?


2. Find ages of daughters

Alok has three daughters. His friend Shyam wants to know the ages of his daughters. Alok gives him first hint.

  1. The product of their ages is 72. Shyam says this is not enough information Alok gives him a second hint.
  2. The sum of their ages is equal to my house number. Shyam goes out and look at the house number and says β€œI still do not have enough information to determine the ages”. Alok admits that Shyam can not guess and gives him the third hint
  3. The oldest of the girls likes strawberry ice-cream. Shyam is able to guess after the third hint.


Can you guess what are the ages of three daughters?


Transfer
Alok has three daughters. His friend Shiyam wants to know the age of his daughters. Alok gives him the first hint.

  1. The product of their ages is 72. Shiyam says that this information is not enough, then Alok gives him a second clue.
  2. The sum of their ages is equal to the number of my house. Shiyam comes out, looks at the house number and says: "I still do not have enough information to determine the age." Alok admits that Shiyam will not be able to guess, and therefore gives him a third clue.
  3. The eldest of the girls love strawberry ice cream. Only after the third clue did Shiyama manage to guess the age of his daughters.


Can you guess how old each of the three daughters is?


Tasks



1. Tom and Jerry

Since very long time Tom and Jerry have been fighting with each other for a piece of Cheese. So finally you came to rescue and decided that the result of the fight will be decided by a mathematical game, in which you will write a number N (1 <= N <= 10^6)



. Tom and Jerry will play the game alternatively and each of them would subtract a number n [n < N]



such that N % n = 0



.

The game is repeated turn by turn until the one, who now cannot make a further move looses the game.

The game begins with Tom playing first move. It is well understood that both of them will make moves in optimal way. You are to determine who wins the game.



Input: the first line of each test case consists of N the number.

Output: print 1 if Tom wins and print 0 if Jerry wins in a separate line.



Constraints:

1 <= N <= 10^6







Sample:

Input: 2 / Output: 1

Input: 4 / Output: 1


Transfer
For a long time, Tom and Jerry fought each other for a piece of cheese. You decided to help them quickly determine the winner. The result of the match will be decided in a mathematical game in which you will write the number N (1 <= N <= 10^6)



. Tom and Jerry will play the game one at a time. Each of them will subtract the number n [n < N]



so that N % n = 0



.

The game continues until one of the participants can make a move. Anyone who cannot make the last move loses.

The game begins with Tom making the first move. It is clear that both of them will make moves in an optimal way. You must determine who wins the game.



The input is: the first line of input for each test consists of the number N.

The program should output: 1, if Tom wins; 0 if Jerry wins. In a separate line.



Limitations:

1 <= N <= 10 ^ 6







Example

Input: 2 / Output: 1

Input: 4 / Output: 1



2. N meetings in one room

There is one meeting room in a firm. There are N meetings in the form of (S[i], F[i])



where S [i] is start time of meeting i and F [i] is finish time of meeting i.

What is the maximum number of meetings that can be accommodated in the meeting room?



Input:

The first line of input consists number of the test cases. The description of T test cases is as follows:

The first line consists of the size of the array, second line has the array containing the starting time of all the meetings each separated by a space, ie, S [i]. And the third line has the array containing the finishing time of all the meetings each separated by a space, ie, F [i].

Output:

In each separate line print the order in which the meetings take place separated by a space.



Constraints:

1 ≀ T ≀ 70

1 ≀ N ≀ 100

1 ≀ S[ i ], F[ i ] ≀ 100000








Example:

Input:

2

6

1 3 0 5 8 5

2 4 6 7 9 9

8

75250 50074 43659 8931 11273 27545 50879 77924

112960 114515 81825 93424 54316 35533 73383 160252






Output:

1 2 4 5

6 7 1






Transfer
The company has one meeting room. There are N meetings in the form (S [i], F [i])



, where S [i] is the start time of meeting i, and F [i] is the end time of meeting i.

The task is to place the maximum number of meetings in the meeting room.



Input data:

The first line of input contains the number of tests. The description of the tests looks like this:

β€’ the first line consists of the size of the array;

β€’ the second line has an array containing the start time of all meetings S [i], each of which is separated by a space;

β€’ the third line contains an array containing the end time of all meetings F [i], each of which is separated by a space.

Output:

in each separate line print the order in which the meetings take place, separated by spaces.



Limitations:

1 ≀ T ≀ 70

1 ≀ N ≀ 100

1 ≀ S [i], F [i] ≀ 100000








Example:

Input:

2

6

1 3 0 5 8 5

2 4 6 7 9 9

8

75250 50074 43659 8931 11273 27545 50879

77924 112960 114515 81825 93424 54316 35533 73383 160252






Exit:

1 2 4 5

6 7 1








3. Inversion of array

Given an array of positive integers. The task is to find inversion count of array.

Inversion Count: For an array, inversion count indicates how far (or close) the array is from being sorted. If array is sorted in reverse order that inversion count is the maximum.

Formally, two elements a [i] and a [j] form an inversion if a[i] > a[j]



and i < j



.



Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, the size of array. The second line of each test case contains N elements.

Output: Print the inversion count of array.



Constraints:

1 ≀ T ≀ 100

1 ≀ N ≀ 107

1 ≀ C ≀ 1018








Example:

Input:

1

5

2 4 1 3 5






Output:

3





Transfer
An array of natural numbers is given. The task is to find the number of inversions of the array.

Number of inversions: for an array, the number of inversions indicates how far (or close) the array is from sorting. If the array is already sorted, then the number of inversions is 0. If the array is sorted in the reverse order, then the number of inversions is the maximum.

Formally, two elements a [i] and a [j] form an inversion if a[i] > a[j]



and i < j



.



Input data:

the first line contains an integer T, indicating the number of tests. The first line of each test is N, the size of the array. The second line of each test contains N elements.

Exit:

print the number of inversions of the array.



Limitations:

1 ≀ T ≀ 100

1 ≀ N ≀ 10 7

1 ≀ C ≀ 10 18








Example:

Input:

1

5

2 4 1 3 5






Exit:

3




Well, as promised! Many were able to answer questions, but with the tasks, apparently, it was more difficult! :)



The answers



Question 1
Answer: at the black door.

Solution: let's look at the description on the brown door "only one door is telling the truth." It can be true or false.

Scenario 1: The description on the brown door is true. Then the description on the black door (β€œthe monster is here”) should be a lie. This means that the cave with the black door contains treasures!

Scenario 2: the description on the brown door is false. Then either both descriptions are false or both true. Both descriptions cannot be true, as this is impossible and not consistent. This means that both descriptions are false. Again, a treasure in the black door.



Question 2
Answer: daughters age: 3 years, 3 years and 8 years.

Solution: 1) For starters, it is said that the product of ages is 72.

Find all possible options for which the product will be equal to 72:

  • 1 * 1 * 72 = 72
  • 1 * 2 * 36 = 72
  • 1 * 3 * 24 = 72
  • 1 * 4 * 18 = 72
  • 1 * 6 * 12 = 72
  • 1 * 8 * 9 = 72
  • 2 * 2 * 18 = 72
  • 2 * 3 * 12 = 72
  • 2 * 4 * 9 = 72
  • 2 * 6 * 6 = 72
  • 3 * 3 * 8 = 72
  • 3 * 4 * 6 = 72


This, of course, is not enough to give an exact answer.

Next, Shiyam looks at the house number (which number is not reported) and still can not give an exact answer. Summarize all the options found earlier:

  • 1 + 1 + 72 = 74
  • 1 + 2 + 36 = 39
  • 1 + 3 + 24 = 28
  • 1 + 4 + 18 = 23
  • 1 + 6 + 12 = 19
  • 1 + 8 + 9 = 18
  • 2 + 2 + 18 = 22
  • 2 + 3 + 12 = 17
  • 2 + 4 + 9 = 15
  • 2 + 6 + 6 = 14
  • 3 + 3 + 8 = 14
  • 3 + 4 + 6 = 13


Obviously, one of the sums is the house number. Since Shiyam could not answer exactly, the unequivocal conclusion is that the house number is 14, since there are two options with this result.

  • 2 + 6 + 6 = 14
  • 3 + 3 + 8 = 14


3) From the third hint, it should be understood that there is an older daughter (not several, but one), therefore, of the two options that we found in the second step, only one is suitable.



Task 1
Lolohaev was thinking correctly!

Answer: (n - 1) % 2





Decision:

 using System; public class TomJerry { static public void Main () { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.printIn((n - 1) % 2); } }
      
      







Task 2
The solution used the Greedy algorithm.

 #include <bits/stdc++.h> using namespace std; struct Activity { int start, finish, index; }; bool activityCompare(Activity s1, Activity s2) { return s1.finish < s2.finish; } int main() { int t, n, i, j; cin >> t; while (t--) { cin >> n; Activity arr[n]; for (i = 0; i < n; i++) { cin >> arr[i].start; arr[i].index = i; } for (i = 0; i < n; i++) cin >> arr[i].finish; sort(arr, arr + n, activityCompare); i = 0; cout << arr[i].index + 1 << " "; for (int j = 1; j < n; j++) { if (arr[j].start >= arr[i].finish) { cout << arr[j].index + 1 << " "; i = j; } } cout << endl; } return 0; }
      
      







Task 3
 #include<iostream> using namespace std; int merge(int* arr, int* LArr, int* RArr, int m, int n) { int i = 0, j = 0, k = 0; int count = 0; while (i < m && j < n) { if (LArr[i] <= RArr[j]) arr[k++] = LArr[i++]; else { arr[k++] = RArr[j++]; count = count + m - i; } } while (i < m) arr[k++] = LArr[i++]; while (j < n) arr[k++] = RArr[j++]; return count; } int mergeSort(int* arr, int start, int end) { if (start > end) return 0; if (start == end) return 0; if (start == end - 1) { if (arr[start] <= arr[end]) return 0; else swap(arr[start], arr[end]); return 1; } int mid = (start + end) / 2; int* LArr = new int[mid + 1]; int* RArr = new int[end - mid]; int i; for (i = start; i <= mid; i++) LArr[i] = arr[i]; for (i = mid + 1; i <= end; i++) RArr[i - (mid + 1)] = arr[i]; int count = 0; count += mergeSort(LArr, 0, mid); count += mergeSort(RArr, 0, end - mid - 1); count += merge(arr, LArr, RArr, mid + 1, end - mid); delete[] LArr; delete[] RArr; return count; } int main() { int t, n,*arr; cin >> t; while (t--) { cin >> n; arr = new int[n]; for (int i = 0; i < n; i++) cin >> arr[i]; cout << mergeSort(arr, 0, n - 1) << endl; } }
      
      






All Articles