Showing posts with label HR. Show all posts
Showing posts with label HR. Show all posts

Sunday, May 23, 2021

HACKERRANK - INTERVIEW PREP KIT- SOLUTIONS IN CPP

HACKERRANK INTERVIEW PREP KIT SOLUTIONS-

* WARM-UP CHALLEGES-


COUNTING VALLEYS

PROBLEM



* DATA STRUCTURES


ARRAY


LINKED LIST


STACK


QUEUE



 HACKERRANK - CRACKING THE CODING INTERVIEW TUTORIALS SOLUTIONS IN CPP


HASH TABLES - RANSOM NOTE

PROBLEM

Complete the checkMagazine function in the editor below. It must print  if the note can be formed using the magazine, or .

checkMagazine has the following parameters:

  • string magazine[m]: the words in the magazine
  • string note[n]: the words in the ransom note

Prints

  • string: either  or , no return value is expected

Input Format

The first line contains two space-separated integers,  and , the numbers of words in the  and the , respectively.
The second line contains  space-separated strings, each .
The third line contains  space-separated strings, each .

Constraints

  • .
  • Each word consists of English alphabetic letters (i.e.,  to  and  to ).

Sample Input 0

6 4
give me one grand today night
give one grand today

Sample Output 0

Yes
MY SOLUTION-
void checkMagazine(vector<string> magazine, vector<string> note) {
    //Simple finding in the string magazine - passes some test cases
    // for(int i = 0; i < note.size();){
    //     if(find(magazine.begin()+i, magazine.end(), note[i]) != magazine.end())
    //         i++;
    //     else{
    //         cout << "No" <<"\n";
    //         return;
    //     }    
    // }
    // cout << "Yes" <<"\n";
//Hashing in Strings
    unordered_map<string, int>words;
    
    for(auto &it: magazine){ //N words
        words[it]++;
    }
    
    for(auto &it : note){// M words
        if(words[it]> 0){
            words[it]--;
        }
        else{
            cout << "No" <<"\n";
            return;
        }
    }
    cout << "Yes" <<"\n";
    return;
}
TC = O(N+M)

Wednesday, April 7, 2021

HACKERRANK SOLUTIONS - INTERVIEW PREPARATION KIT - ARRAY - IN C++

  HACKERRANK SOLUTIONS - INTERVIEW PREPARATION KIT - IN C++


ARRAY


1. 2D ARRAY DS

https://www.hackerrank.com/challenges/2d-array/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=arrays

min sum = -9*7 as there are 7 values in 1 hourglass

int hourglassSum(vector<vector<int>> arr) {
    int row = arr.size();
    int col = arr[0].size();
    int max_sum = -63, sum = 0;
    for(int i = 0; i < row-2; i++){
        for(int j = 0; j < col-2; j++){
            sum = arr[i][j] + arr[i][j+1] + 
                arr[i][j+2] + arr[i+1][j+1] + 
                arr[i+2][j] + arr[i+2][j+1] + 
                arr[i+2][j+2];
            if(sum > max_sum)
                max_sum = sum;
        }
    }
    return max_sum;
}


2. LEFT ROTATION




vector<int> rotLeft(vector<int> a, int d) {
    for(int j = 0; j < d; j++){
        //rotate left by 1
        int temp = a[0];
        int n = a.size();
        for(int i = 0; i < n-1; i++){
            a[i] = a[i+1];
        }
        a[n-1] = temp;
    }
    return a;
}










HACKERRANK SOLUTIONS - INTERVIEW PREPARATION KIT WARM -UP CHALLENGES - IN C++

 HACKERRANK SOLUTIONS - INTERVIEW PREPARATION KIT - IN C++


WARM-UP CHALLENGES


1. SALES BY MATCH

https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=warmup


Solution - 1:

Algo-

Sort array in increasing/ decreasing order

Compare adjacent sock colors - if equal increase count of a pair, and also increment index, so as to not a sock twice (and count 2 pairs of 3 socks)

TC = O(nlogn+n) = O(nlogn)

int sockMerchant(int n, vector<int> ar) {
    sort(ar.begin(), ar.end());
    int cnt_pair = 0;
    for(int i = 0; i < ar.size() - 1; i++){
        if(ar[i] == ar[i+1]){
            cnt_pair++;
            i++;
        }
    }
    return cnt_pair;
}



2. COUNTING VALLEYS

https://www.hackerrank.com/challenges/counting-valleys/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=warmup&h_r=next-challenge&h_v=zen

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE JUNE 1 JUNE 2 JUNE 3 JUNE 4 JUNE 5 Maximum Performance of a Team LINK You are given two integers ...