Avg CTC: 29LPA
Location: Hyderabad and Noida
Software Engineer Roles, apply here: SE Hyderabad and SE Noida
A string is called palindrome string if the reverse of the string is also the same as original string. For example 'radar' and 'level' are palindrome but 'microsoft' is not palindrome.
A string S is called an extended palindrome string if the string becomes palindrome after chucking it in chunks of length d, whether d is either 1 or a prime divisor of |S|. For example, 'abcxyzabc' is an extended palindrome string because it can be chucked in chunks of length 3 and become a palindrome string '{abc}{xyz}{abc}'.
The degree of a string is defined as the minimum possible chunk length that can make the string become an extended palindrome string.
Note that, a palindrome string is an extended palindrome string of degree 1.
Write function:
int solution(string &S);
that, given a string S(1<= |S| <= 100,000), returns the degree of S, or -1 if Sis not an extended palindrome string.
Example 1:
Input:
S: 'radar'
Output:
1
Explanation:
The string is already a palindrome string which is, by definition, an extended palindrome string of degree 1.
Example 2:
Input:
S: 'abcxyzabc'
Output:
3
Explanation:
By chunking the string into chunks of length 3 (i.e., '{abc}{xyx}{abc}'), the string can be read exactly the same from left to right or from right to left.
Note that 3 is a prime divisor of the length of the string.
Example 3
Input
S: 'microsoft'
Output:
-1
An infrastructure consisting of n cities from 1 to n, and m bidirectional roads between them are given. Roads do not intersect apart from at their start and endpoints (they can pass through underground tunnels to avoid collisions).
For each pair of cities directly connected by a road, lets define their network rank as the total number of roads that are connected to either of the two cities.
Write a function, given two arrays starts, ends consisting of m integers each and an integer n, where starts[i] and ends[i] are cities at the two ends of the i-th road, returns the maximal network rank in the whole infrastructure.
Example
Input
1starts = [1, 2, 3, 3] 2ends = [2, 3, 1, 4] 3n = 4
Output
14
Explanation
The chosen cities may be 2 and 3, and the four roads connected to them are: (2,1), (2,3), (3,1), (3,4).
Given a string with only characters X and Y. Find the minimum number of characters to remove from the string such that there is no interleaving of character X and Y and all the Xs appear before any Y.
Example 1:
Input:
YXXXYXY
Output:
2
Explanation:
We can obtain XXXYY by:
Delete first Y --- XXXYXY
Delete last occurrence of X --- XXXYY
Example 2:
Input:
YYXYXX
Output:
3
Explanation:
We can remove all occurrence of X or Y.
Example 3:
Input:
XXYYYY
Output:
0
Explanation:
String matches the format required.
Given current day as day of the week and an integer K, the task is to find the day of the week after K days.
Example 1:
Input:
day = 'Monday'
K = 3
Output:
Thursday
Example 2:
Input:
day = 'Tuesday'
K = 101
Output:
Friday
Given a string composed of a-z and also the letters B and C, where B denotes a backspace and C denotes a caps lock. The first 'C' denotes Caps lock on, then a second 'C' denotes caps lock off. Find the resultant string after resolving the string.
e.g. CrCellBax -- Relax
Explanation:
first C -- caps lock on, therefore till now string becomes R, then second C --- caps lock off.
Till now:
Rell, B --- backspace --- Relax
Given a string S containing only characters a and b. A substring (contiguous fragment) of S is called a semi-alternating substring if it does not contain three identical consecutive characters. In other words, it does not contain either 'aaa' or 'bbb' substrings. Note that the whole S is its own substring.
Example 1:
Input:
baaabbabbb
Output:
7
Explanation:
The longest semi-alternating substring is aabbabb
Example 2:
Input:
babba
Output:
5
Explanation:
Whole S is semi-alternating.
Example 3:
Input:
abaaaa
Output:
4
Explanation:
The first four letters of S create a semi-alternating substring.
The question states that the string is an extended palindrome, if it is a palindrome, when we group the string into chunks of size d, and then check whether the chunks form a palindrome or not. Two chunks are said to be same, if they are exactly the same (string comparison). Also, it is mentioned that d should either be 1 or a prime divisor of the length of the string S.
We can approach this by first finding all the prime divisors of the length of the string, and then checking for each prime divisor and 1. But if the number of prime divisors is very large, then checking for each prime divisor will take time leading to a very large time complexity and therefore a TLE verdict. Hence, we need to make sure that the number of prime divisors is not very large.
So let us multiply the first few prime numbers, and see what can be the maximum number of distinct prime divisors of a number in the given constraints. To find the maximum number of distinct prime divisors, we multiply the smallest prime numbers (because multiplying larger numbers inplace of smaller will always lead to a smaller result) only once and see till how many can we go inside the constraints of the problem.
2 x 3 x 5 x 7 x 11 x 13 x 15 = 450450, which is larger than the constraints of the problem, since the maximum length of the string can be 100000. 2 x 3 x 5 x 7 x 11 x 13 = 30030, which is inside the constraints of the problem. So, at max, the length of the string can have 6 prime divisors. So even if we check for the solution at all prime divisors and 1, the total number of points at which we would have to check is just 7.
Checking for a single length will take only O(n) time complexity, and so the total time complexity will be O(7*n) = O(700000) which will work. We can check for a single prime divisor, whether the string is a palindrome, by iterating over the (number of chunks)/2, i.e. the first half of chunks, and then comparing the letters in that chunk, to their corresponding mirror chunk in the second half of the string.
Also, we can find the prime divisors of the length using a precomputed Sieve of Erastothenes. You can refer this for [Sieve.][1]
int divisor = prime_divisors[i];
int chunks = n/divisor;
bool palindrome = true;
for (int i = 0; i < chunks/2; i++)
{
for (int j = 0; j < divisor; j++)
{
if (s[i * divisor + j] != s[n- (i + 1) * divisor + j])
palindrome = false;
}
}
if (palindrome)
{
cout << divisor << '\n';
return;
}
Solution to Question 4
Let's make an array of length 7(0-indexed)
having all the days of a week in order. Now, if the given day occurs at position i
, then the answer will be the day at index (i+k)%7
.
Psudocode
string answer(string pres,int k){
days = {"monday","tuesday","wednesday","thursday","friday","saturday","sunday"}
int p=0
for(i in 0,1,2...6){
if(days[i]==pres) p=i
}
print days[(p+k)%7]
}
Question 3
Overview
XXX......XXYY.......Y
.Solution
XX.....XY.....YY
.XXYXYYY
here is two such indexes from which we can convert our string in the right format.Assume the minimum deletions to format s[0..i] (index 0 to index i in string s) is computed and we keep numbers of deletions for range s[0..i] in variable int min_dels, then the minimum deletions to format s[0..i+1] is: `
if(s[i+1] == ‘X’) {
//There are two options: either to include this ‘X’ or exclude it.
// If ‘X' is included then all Y’s before it should be deleted
// If ‘X' is excluded then increment min_dels for range s[0..i] to one,
// that is add the ‘A’ which we going to exclude/delete to the number of deleted items
min_dels[0..i+1] = std::min(num_Ys, min_dels[0..i]+1); // num_Ys is the total number of Ys in s[0..i]
}
else {
// Since Y is at the end there is no need to exclude this Y
min_dels[0..i+1] = min_dels[0..i];
}
`
Question 2
Overview
prerequisite
Solution
Tables of connected cities should look like :`
1------2
2------3
3------1
3------4
Graph :
1 ---------- 2
\ |
\ |
\ |
4----------3
It seems that in the problem we just need to count connected of each pair of given cities
-->we can see that pairs 2 and 3 have the biggest number of connections to cities and the number of such connections is 4
Here is a quick snippet for the code :
// count number of roads to the pair of cities
for(int i = 0; i < roads_num; ++i) {
cities[A[i]]++;
cities[B[i]]++;
}
int result = INT_MIN;
// Find maximum number of roads connected to the pair
// of cities except the road between these two cities
// because it was counted twice for each city.
for(int i = 0; i < roads_num; ++i) {
result = max(result, cities[A[i]] + cities[B[i]] - 1);
}
Question 6
Overview
aaa
or bbb
is not said to be semi alternating substring.Solution
count
andresult
.