Linkedin Interview Question for SDE1s


Team: Hacker rank test
Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Extract all integers from string */
#include <iostream> 
#include <sstream> 
#include <bits/stdc++.h> 

using namespace std; 

string getSubString(string str, map<char, int> &m, int k)
{
    int needed = k;
    int count = 0;
    int start = 0;
    int end   = 1;
    int minLen = str.size()+1;
    int minStart = 0;
    int minEnd = 0;
    
    if (m[str[start]] == 0) {
        count++;
    }
    
    while(end != str.size()) {
        if (m[str[end]] == 0) {
            count++;
        }
        while(needed <= count && start < end) {
            if (m[str[start]] == 1){
                start++;
            }
            else if (needed < count && m[str[start]] == 0) {
                count--;
                start++;    
            }
            else {
                break;
            }
        }
        if(needed == count && minLen > (end - start + 1)) {
            minLen = (end - start + 1);
            minStart = start;
            minEnd = end;
        }
        end++;
    }
    
    return str.substr(minStart, (minEnd - minStart +1));
}

// Driver code 
int main() 
{ 
	string str = "abcdebdac"; 
	map<char, int> m;
	m['a'] = 1;
	m['b'] = 0;
	m['c'] = 1;
	m['d'] = 0;
	m['e'] = 1;
	m['f'] = 1;
	m['g'] = 1;
	m['h'] = 1;
	m['i'] = 1;
	m['j'] = 1;
	m['k'] = 1;
	m['l'] = 1;
	cout << getSubString(str, m, 2) << endl; 
	return 0; 
}

- Arpit Baheti March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Extract all integers from string */
#include <iostream> 
#include <sstream> 
#include <bits/stdc++.h> 

using namespace std; 

string getSubString(string str, map<char, int> &m, int k)
{
    int needed = k;
    int count = 0;
    int start = 0;
    int end   = 1;
    int minLen = str.size()+1;
    int minStart = 0;
    int minEnd = 0;
    
    if (m[str[start]] == 0) {
        count++;
    }
    
    while(end != str.size()) {
        if (m[str[end]] == 0) {
            count++;
        }
        while(needed <= count && start < end) {
            if (m[str[start]] == 1){
                start++;
            }
            else if (needed < count && m[str[start]] == 0) {
                count--;
                start++;    
            }
            else {
                break;
            }
        }
        if(needed == count && minLen > (end - start + 1)) {
            minLen = (end - start + 1);
            minStart = start;
            minEnd = end;
        }
        end++;
    }
    
    return str.substr(minStart, (minEnd - minStart +1));
}

// Driver code 
int main() 
{ 
	string str = "abcdebdac"; 
	map<char, int> m;
	m['a'] = 1;
	m['b'] = 0;
	m['c'] = 1;
	m['d'] = 0;
	m['e'] = 1;
	m['f'] = 1;
	m['g'] = 1;
	m['h'] = 1;
	m['i'] = 1;
	m['j'] = 1;
	m['k'] = 1;
	m['l'] = 1;
	cout << getSubString(str, m, 2) << endl; 
	return 0; 
}

- Arpit Baheti March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* Extract all integers from string */
#include <iostream> 
#include <sstream> 
#include <bits/stdc++.h> 

using namespace std; 

string getSubString(string str, map<char, int> &m, int k)
{
    int needed = k;
    int count = 0;
    int start = 0;
    int end   = 1;
    int minLen = str.size()+1;
    int minStart = 0;
    int minEnd = 0;
    
    if (m[str[start]] == 0) {
        count++;
    }
    
    while(end != str.size()) {
        if (m[str[end]] == 0) {
            count++;
        }
        while(needed <= count && start < end) {
            if (m[str[start]] == 1){
                start++;
            }
            else if (needed < count && m[str[start]] == 0) {
                count--;
                start++;    
            }
            else {
                break;
            }
        }
        if(needed == count && minLen > (end - start + 1)) {
            minLen = (end - start + 1);
            minStart = start;
            minEnd = end;
        }
        end++;
    }
    
    return str.substr(minStart, (minEnd - minStart +1));
}

// Driver code 
int main() 
{ 
	string str = "abcdebdac"; 
	map<char, int> m;
	m['a'] = 1;
	m['b'] = 0;
	m['c'] = 1;
	m['d'] = 0;
	m['e'] = 1;
	m['f'] = 1;
	m['g'] = 1;
	m['h'] = 1;
	m['i'] = 1;
	m['j'] = 1;
	m['k'] = 1;
	m['l'] = 1;
	cout << getSubString(str, m, 2) << endl; 
	return 0; 
}

- arpitbaheti7 March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It’s good, except it’s not ministering we want, we want maxstring.

- Ns March 21, 2020 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More