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

Hello! Here we are! Today we have a jubilee article at number 30!

We again prepared for you a selection of interesting questions and tasks from interviews at leading IT companies! Answers to problems from the previous issue have already been published .



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

This week we collected tasks from interviews in the American company Visa.

Questions


1. 100 Prisoners with Red / Black Hats
100 prisoners in jail are standing in a queue facing in one direction. Each prisoner is wearing a hat of color either black or red. A prisoner can see hats of all prisoners in front of him in the queue, but cannot see his hat and hats of prisoners standing behind him.
The jailer is going to ask color of each prisoner's hat starting from the last prisoner in queue. If a prisoner tells the correct color, then is saved, otherwise executed. How many prisoners can be saved at most if they are allowed to discuss a strategy before the jailer starts asking colors of their hats.

Transfer
100 . . , , .
, . , , , . , , .
.

2. Ratio of Boys and Girls in a Country where people want only boys
In a country, all families want a boy. They keep having babies till a boy is born. What is the expected ratio of boys and girls in the country?

Transfer
. . ?

Tasks


1. The Lazy Caterer's Problem
Given an integer N , denoting the number of cuts that can be made on a pancake, find the maximum number of pieces that can be formed by making N cuts.

Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of one line. The first line of each test case consists of an integer N.

Output:
Corresponding to each test case, in a new line, print the maximum number of pieces that can be formed by making N cuts.

Constraints: Example: Input Output

1 ≤ T ≤ 100
1 ≤ N ≤ 106




2
5
3


16
7

Transfer
N, , . , , N .

:
T, . T. . N.

:
, , N .

:

1 ≤ T ≤ 100
1 ≤ N ≤ 106


:

2
5
3


16
7

2. Peak element
Given an array A of N integers. The task is to find a peak element in it.
An array element is peak if it is not smaller than its neighbours. For corner elements, we need to consider only one neighbour.

Note: There may be multiple peak element possible, in that case you may return any valid index.

Input Format:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N. Then in the next line are N space separated values of the array.

Output Format:
For each test case output will be 1 if the index returned by the function is an index with peak element.

User Task:
You don't have to take any input. Just complete the provided function peakElement () and return the valid index.

Constraints: Example: Input: Output: Explanation: Testcase 1: In the given array, 3 is the peak element. Testcase 2: 4 is the peak element.
1 <= T <= 100
1 <= N <= 100
1 <= A[] <= 1000




2
3
1 2 3
2
3 4


3
4





Transfer
A N . , .
, . .

. , .

:
T, . T. N. N .

:
1 <= T <= 100
1 <= N <= 100
1 <= A [] <= 1000


:
:

2
3
1 2 3
2
3 4

:
3
4


:
1:
3 .
2: 4 .

3. Maximum Intervals Overlap
Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in register are not in any order.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the entry and exit array. Then the next two line contains the entry and exit array respectively.

Output:
Print the maximum no of guests and the time at which there are maximum guests in the party.

Constraints:
1<=T<=10^5
1<=N<=10^5
1<=entry[i],exit[i]<=10^5


Example:
Input:

2
5
1 2 10 5 5
4 5 12 9 12
7
13 28 29 14 40 17 3
107 95 111 105 70 127 74


Output:
3 5
7 40

, . , . , .

:
T, . T. n, . .

:
, .

:
1 <= <= 10 ^ 5
1 <= N <= 10 ^ 5
1 <= [I], [I] <= 10 ^ 5


:
:

2
5
1 2 10 5 5
4 5 12 9 12
7
13 28 29 14 40 17 3
107 95 111 105 70 127 74


:
3 5
7 40



1
99 , 100- 50-50 .
, .

100- , . , , 99- .

99- 100- . 100-. 99- .

100- «» ( )
) 99- , .
) 99- , — .

100- «» ( )
) 99- , — .
) 99- , — .

98- 99- .

97 1 .

2
: . , , , .

.
NG .

, (1-p) , .

NG .

NG = 0*(1-p) + 1*p*(1-p) + 2*p*p*(1-p) + 3*p*p*p*(1-p) + 4*p*p*p*p*(1-p) +.....

p = 1/2 (1-p) = 1/2 .

NG = 0*(1/2) + 1*(1/2)2 + 2*(1/2)3 + 3*(1/2)4 + 4*(1/2)5 + ...
1/2*NG = 0*(1/2)2 + 1*(1/2)3 + 2*(1/2)4 + 3*(1/2)5 + 4*(1/2)6 + ...


NG - NG/2 = 1*(1/2)2 + 1*(1/2)3 + 1*(1/2)4 + 1*(1/2)5 + 1*(1/2)6 + ...

1
NG/2 = (1/4)/(1-1/2) = 1/2

NG = 1

1
, : (n + c*c + 2) / 2
int main() {
	int t,n,final;
	cin>>t;
	while(t--){
	   cin>>n;
	   final=(n+(n*n)+2)/2;
	   cout<<final<<endl;
	}
	return 0;
}

2
// arr: input array
// n: size of array
int peakElement(int a[], int n)
{
    int i;
    for(i=1;i<n-1;i++){
        if(a[i]>a[i-1] && a[i]>a[i+1]){
            return i;
        }
    }
    if(n>1 && a[0]>a[1])
        return a[0];
    if(n-2>=0 && a[n-1]>a[n-2])
    return a[n-1];
}

3
/*package whatever //do not write package name here */

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		
		while (T > 0) {
		    int N = sc.nextInt();
		    int[] entry = new int[N];
		    int[] exit = new int[N];
		    for (int i = 0; i < N; i++) {
		        entry[i] = sc.nextInt();
		    }
		    
		    for (int i = 0; i < N; i++) {
		        exit[i] = sc.nextInt();
		    }
		    
		    Arrays.sort(entry);
		    Arrays.sort(exit);
		    //System.out.println(Arrays.toString(entry));
		    //System.out.println(Arrays.toString(exit));
		    
		    int counter = 0;
		    int maxCounter = -1;
		    int maxTime = -1;
		    
		    int entryIdx = 0;
		    int exitIdx = 0;
		    
		    while (entryIdx < N && exitIdx < N) {
		        if (entry[entryIdx] <= exit[exitIdx]) {
		            counter++;
		            if (counter > maxCounter) {
		                maxCounter = counter;
		                maxTime = entry[entryIdx];
		            }
		            entryIdx++;
		        } else {
		            counter--;
		            exitIdx++;
		        }
		    }
		    
		    System.out.println(maxCounter + " " + maxTime);   
		    T--;
		}
	}
}

Source: https://habr.com/ru/post/undefined/


All Articles