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

Hello!
Today in our rubric tasks with interviews on LinkedIn.

image

If you decide all of them on the fly, and seriously think about joining LinkedIn, we recommend that you listen to the release of our podcast.

True, it is about products, but in it we ask Dmitry Berdnikov, Strategy & Operations Director LinkedIn, in detail about all stages of interviews with the company.

Listen where convenient ↓
Apple Podcasts
Google Podcasts
Yandex.Music
Or on the podcast page

And by the way, answers to previous problems have already been published ! Check with them.

Questions


1. Monty Hall problem
Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2? ”
Is it to your advantage to switch your choice?

image
Transfer
Suppose you are on a game show, and you are given a choice of three doors: one car door; for the other goats. You choose the door, say, No. 1, and the presenter, who knows what is behind the doors, opens another door, say, No. 3, in which there is a goat. Then he tells you, “Do you want to choose door number 2?”
Is it profitable for you to change your choice?

2. Find the Jar with contaminated pills
You have 5 jars of pills. Each pill weighs 10 grams, except for contaminated pills contained in one jar, where each pill weighs 9 grams. Given a scale, how could you tell which jar had the contaminated pills in just one measurement?

Transfer
You have 5 cans of tablets. Each tablet weighs 10 grams, with the exception of infected tablets contained in one jar, where each tablet weighs 9 grams. You have scales, how would you be able to determine in which bank the infected pills were, in just one weigh-in?

Tasks


1. Distinct Substrings
Given a string S consisting of uppercase alphabetic characters. Return the number of different substrings of size 2 that appear in S as contiguous substrings.

Input: The first line contains 'T' denoting the number of testcases. Then follows description of test cases. The next T lines contains the string S.

Output: Output the number of different substrings of size 2 in S.

Constraints:
1<=T<=50
1<=|S|<=100


Example:
Input:
2
ABCAB
XYZ


Output:
3
2


Explanation: For "ABCAB", the three distinct substrings of size 2 are "AB", "BC" and "CA". For "XYZ", the two distinct substrings of size 2 are "XY" and "YZ".

Transfer
Given string S, consisting of uppercase alphabetic characters. Return the number of different substrings of size 2 that appear in S as adjacent substrings.

Input: The first line contains 'T', indicating the number of tests. The following is a description of the tests. The next T lines contain the string S.

Output: Print a single number - the number of different substrings of size 2 in string S.

Limitations:
1< = T< = 50
1<= / S / < = 100


Example:
Input:
2
ABCAB
XYZ


Exit:
3
2


Explanation: For “ABCAB,” the three different substrings of size 2 are “AB,” “BC,” and “CA”. For “XYZ”, two separate substrings with size 2 are “XY” and “YZ”.

2. Longest consecutive subsequence
Given an array arr [] of positive integers. Find the length of the longest sub-sequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order.

Input:
The first line of input contains T, number of test cases. First line of line each test case contains a single integer N.
Next line contains N integer array.

Output:
Print the output of each test case in a seprate line.

Constraints:
1 <= T <= 100
1 <= N <= 105
0 <= a[i] <= 105


Example:
Input:
2
7
2 6 1 9 4 5 3
7
1 9 3 10 4 20 2


Output:
6
4


Explanation:
Testcase 1: The consecutive numbers here are 1, 2, 3, 4, 5, 6. These 6 numbers form the longest consecutive subsquence.

Testcase2: 1, 2, 3, 4 is the longest consecutive subsequence.

Transfer
An array of arr [] positive integers is given. Find the length of the longest subsequence such that the elements in the subsequence are consecutive integers, consecutive numbers can be in any order .

Input:
The first line of input contains T, the number of test cases. The first line of each test contains one integer N.
The next line contains N integer arrays.

Exit:
Print the output of each test on a separate line.

Limitations:
1 < = T < = 100
1 < = N < = 105
0 <= a[i] < = 105


Example:
Input:
2
7
2 6 1 9 4 5 3
7
1 9 3 10 4 20 2


Exit:
6
4


Explanation:
Test 1: The sequential numbers here are 1, 2, 3, 4, 5, 6. These 6 numbers form the longest sequential subquery.

Test 2: 1, 2, 3, 4 - The longest sequential subsequence.

3. Distinct palindromic substrings
Given a string of lowercase ASCII characters, find all distinct continuous palindromic sub-strings of it.

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 a string.

Output:
Print the count of distinct continuous palindromic sub-strings of it.

Constraints:
1<=T<=10^5
1<=length of string<=10^5


Example:
Input:
2
abaaa
geek


Output:
5
4

Transfer
Given a string of lowercase ASCII characters, find all of its individual continuous palindrome substrings.

Input:
The first line of input contains an integer T, indicating the number of tests. Then tests follow. Each test contains a line.

Exit:
Print the number of individual continuous palindromic substrings of this type.

Limitations:
1< = T<=10^5
1< = <=10^5


Example:
Input:
2
abaaa
geek


Exit:
5
4


The answers


Question 1
If you change your choice, you will get a car with a 2/3 probability. So a change of choice in such a situation increases the probability of winning.
More details are described on Wikipedia . You can also study this lecture . In addition there is an online simulation of the Monty Hall task.

Question 2
Take 1 tablet from a can 1, 2 tablets from a can 2, 3 tablets from a can 3, 4 tablets from a can 4 and 5 tablets from a can 5. Put all these 15 tablets on a scale. The correct weight is 150 (15 * 10). But one of the cans has infected pills. So the weight will definitely be less than 150. If the weight is 149, then Bank 1 has infected tablets, because there is only one infected tablet. If the weight is 148, then can 2, If the weight is 147, then can 3, If 146, then can 4, If 145, then can 5.

Task 1
Python solution
 for _ in range(int(input())): s = input() ls = [s[i:i+2] for i in range(0,len(s))] del ls[-1] ls = list(set(ls)) print(len(ls)) 

Task 2
 #include<iostream> using namespace std; int main() { int t; cin >> t; while(t--) { int n; cin >> n; int ar[n]; for(int i=0;i<n;i++) cin >> ar[i]; //************************************* //Finding Max Element of Array int max=-1; for(int i=0;i<n;i++) { if(ar[i]>max) { max=ar[i]; } } //*************************************** int size = max+1; int hash[size]={0}; for(int i=0;i<n;i++) hash[ar[i]]++; //*************************************** /*for(int i=0;i<size;i++) cout << hash[i] << " "; cout << endl;*/ for(int i=0;i<n;i++) { if(hash[ar[i]]>1) hash[ar[i]]=1; } int count = 1,max_count = 1; for(int i=0;i<size;i++) { if(hash[i]==1 && hash[i+1]==1) count++; else { if(count>max_count) max_count=count; count = 1; } } cout << max_count << endl; } } 

Task 3
 #include<bits/stdc++.h> using namespace std; bool ispalindrome(string s) { int i=0;int j=s.length()-1; while(i<j) { if(s[i]!=s[j]) return false; i++; j--; } return true; } int main() { //code int t; cin>>t; while(t--) { string s; cin>>s; vector<string>ans; for(int l=1;l<=s.length();l++) { for(int i=0;i<s.length()-l+1;i++) { if(ispalindrome(s.substr(i,l))) ans.push_back(s.substr(i,l)); } } sort(ans.begin(),ans.end()); ans.erase(unique(ans.begin(),ans.end()),ans.end()); cout<<ans.size()<<endl; } return 0; } 

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


All Articles