Anchanto

In anchanto there are a total of 4 rounds. 1st round is aptitude, 2nd is Technical Round. The 3rd round is Technical Interview and the last one is the HR interview round.

Perfect Substring

A string is comprised of digits from 0 to 9 contains a perfect substring if all the elements within a substring occur exactly k times. Calculate the number of perfect substrings in s.
Example:


            S = 1102021222
            k = 2
            
The 6 perfect substrings are:

            S[0:1] = 11
            S[0:5] = 110202
            S[1:6] = 102021
            S[2:5] = 0202
            S[7:8] = 22
            S[8:9] = 22
            
Function Description: Complete the function perfectSubstring in the editor below. perfectSubstring has the following parameters: str s: a string where the value of each element s[i] is described by the the character at position i (where 0<= i < n) int k: an integer that denotes the required Output: int: an integer that represents the number of perfect substrings in the given string
Constraints:
  • 1<= sizeof(s)<105
  • 0 <= s[i] <= 9 (where 0<=i<n)
  • 1 <= k < = 105

                
                    using namespace std;
                    const int MAX_CHAR = 100;
                    bool check(int freq[], int k){
                        for(int i=0; i<MAX_CHAR; i++){
                            return false;
                        }
                    return true;
                    }
                    int perfectSubstring(string s, int k){
                        int count=0;
                        for(int i=0; s[i]; i++){
                            int index= s[j]-'0';
                            freq[index]++;
                            
                            if(freq[index]>k)
                                break;
                            else if (freq[index] == k && check(freq,k) == true)
                                count++;
                        }
                    }
                
            

Maximize the Value

Rearrange an array integers so that the calculated value U is maximized. Among the arrangements that satisfy that test, choose the array with minimal ordering.The value of U for an array with n elements is calculated as:

U = arr[1]*arr[2]*(1/arr[3])*arr[4]*...*arr[n-1]*(1/arr[n]) if n is odd
U = arr[1]*arr[2]*(1/arr[3])*arr[4]*...(1/arr[n-1])*arr[n] if n is even
The sequence of operations is the same in either case, but the length of the array n, determines whether the calculation ends on arr[n] or (1/arr[n]).
Arrange the elements to maximize U and the items are in the numerically smallest possible order.
Example:
arr = [5, 7, 9, 21, 34]

To maximize U and minimize the order, arrange the array as [9,21,5,34,7] so U = 9*21*(1/5)*34*(1/7) = 183.6. The same U can be achieved using several other orders, e.g. [21,9,7,34,5] = 21*9*(1/7)*34*(1/5) = 183.6, but they are not in the minimal order.
Function Description: Complete the function rearrange in the editor below.
Rearrange has the following parameter(s):
int arr[n] : an array of integers
Returns: int[n]: the elements of arr rearranged as described
Constraints:
  • 1<=n<=10^5
  • 1<=n<=10^9
Input Format For Custom Testing
The first line contains an integer, n the number of elements in arr.
Each line i of the n-subsequent lines (where 1<=i<=n) contains an integer, arr[i].
Sample Case 0
Sample Input For Custom Testing

                STDIN	Function
            ----------	-------------
            4	->	arr[] size n = 4
            1	->	arr = [1,2,3,4]
            2	
            3
            4

            Sample Output
            2
            3
            1
            4
            
Explanation: U = 2*3*(1/1)*4 = 24. All other arrangements where U = 24 are numerically higher than this array, e.g. [2,3,1,4] < [3,4,1,2]

                
                    vector<int> rearrange(vector<int> arr){
                        sort(arr.begin(), arr.end());
                        int n = arr.size();
                        if(n<3){
                            return arr;
                        }
                        vector<int> res;
                        int k = 0;
                        int j = n%2 == 0?((n-1)/2):(n/2);
                        res.push_back(arr[j++]);
                        res.push_back(arr[j++]);
                        for(int i=2; i<n; i++){
                            if(i%2==0){
                                res.push_back(arr[k++]);
                            }else{
                                res.push_back(arr[j++]);
                            }
                        }
                        return res;
                    }
                
            

Disk Space Analysis

A company is performing an analysis on the computers at its main office. The computers are spaced along a single row. The analysis is performed in the following way:

  • Choose a contiguous segment of a certain number of computers, starting from the beginning of the row.
  • Analyze the available hard disk space on each of the computers.
  • Determine the minimum available disk space within the segment.
After performing these steps for the first segment, it is then repeated for the next segment, continuing this procedure, find the maximum available disk space among all the minima that are found during the analysis.
Example:

                n = 3, the number of computers
                space = [8,2,4]
                x = 2, the length of analysis segments
                

In this array of computers, the subarrays of size 2 are [8,2] and [2,4]. Thus, the initial analysis returns 2 and 2 because those are the minima for the segments. Finally, the maximum of these values is 2. Therefore the answer is 2. Function Description:
Complete the function segment in the editor below.
Segment has the following parameter(s):
int x: the segment length to analyze 
            int space[n]: the available hard disk space on each of the computers.
Returns:
Int: the maximum of the minimum values of available hard disk space found while analyzing the computers in segments of numComps.
Constraints
  • 1 <= n <= 10^6
  • 1 <= x <= n
  • 1 <= space[i] <= 10^9

            Sample Input:
            STDIN	Function
            ---------	------------
            1	-> length of segments x = 1
            5	-> size of space n= 5
            1	-> space = [1,2,3,1,2]
            2
            3
            1
            2
            
Sample Output: 3 Explanation:
The subarrays of size x = 1 are [1] , [2] , [3], [1] and [2]. Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3.

                
                int segment(int x, vector<space>){
                    int n = space.size();
                    if(n==1)
                        return space[0];
                    
                    int count = 0;
                    int currMin = INT_MAX;
                    int globMax = -1;
                    for(int i=0; i<n; i++){
                        if(count<x){
                            currMin = min(currMin, space[i]);
                        }else{
                            globMax = max(globMax, currMin);
                            if(space[i-count]==currMin){
                                currMin = space[i-count+1];
                                int j = i-count+1;
                                while(j<=i){
                                    currMin = min(currMin, space[j]);
                                    j++;
                                }
                            }else{
                                currMin = min(currMin, space[i]);
                            }
                        }
                    }
                    globMax = globMax == -1?currMin:globMax;
                    return globMax;
                }
                
            

Value of Properties Owned

There are two tables in a database of real estate owners. One has ownership information and the other has price information, in millions. An owner may own multiple houses, but a house will have only one owner.
Write a query to print the lDs fo the owners who have at least 100 million worth of houses and own more than 1 house. The order of output does not matter. The result should be in the format:
BUYER_ID TOTAL_WORTH
Table Schema



Sample Data


                
                    SELECT house.BUYER_ID, SUM(price.PRICE) AS TOTAL_WORTH
                    FROM house
                        LEFT JOIN price ON house.HOUSE_ID = price.HOUSE_ID
                        GROUP BY house.BUYER_ID
                        HAVING count(house.BUYER_ID)>1 and sum(price.PRICE)>100;
                
            

Least Earning Location

A ride hailing company has their DB structured in 3 major tables as described in SCHEMA below.
Write a query to fetch the city names along with earnings from each city.
‘Earnings’ are calculated as the sum of fares of all the rides taken in that city. The output should be structured as: cities.name earnings
The output is sorted ascending by earnings, then ascending by the city name.

Table Schema




Sample Data



                
                    SELECT c.name, SUM(r.fare)
                    FROM CITIES c
                        LEFT JOIN USERS u ON c.id = u.city_id
                        LEFT JOIN RIDES r ON u.id = r.user_id
                    GROUP BY c.name
                    ORDER BY SUM(r.fare) ASC;