## unknown Interview Question for SDE-2s

Country: India
Interview Type: Phone Interview

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

c++, implementation
"abcdefghijklmnopqrstuvwxyz", "xcobra" => "abc"

``````string latgestSubstringPresentOther(string s1, string s2) {
vector<int> arr(s1.size() + 1, 0);
int present[256] = {0, };
int i, start, maxLength, length;

for (i = 0; i < s2.size(); i++) {
present[ s2[i] ] = 1;
}

for (i = 0; i < s1.size(); i++) {
arr[i] = present[ s1[i] ];
}

start = 0;
maxLength = 0;
length = 0;
for (i = 0; i < arr.size(); i++) {
if (arr[i] == 0) {
if (maxLength < length) {
maxLength = length;
start = i - length;
}
length = 0;
continue;
}
length++;
}

return s1.substr(start, maxLength);
}``````

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

there is a problem is solution in line

``"int present[256] = {0, };"``

The problem is in hard-coded value 256. Because symbols with code greater than 255 exist.
With s2 = "1234" and s1 = "ĐÁ" the program fails: it gives the output "ĐÁ", while it should be null.
To improve this, we can create one more loop to obtain the greatest char code as follows (c#):

``````string bothStrings = s1 + s2;
int greatestCode = 0;
for (int i = 0; i < bothStrings.Length; i++) {
if ( greatestCode < bothStrings[i] ) {
greatestCode = bothStrings[i];
}
}
bool[] present = new bool[ greatestCode + 1 ];``````

Or, even better to apply sort

``````string bothStrings = s1 + s2;
var chArr = bothStrings.ToCharArray();
Array.Sort( chArr );
int greatestCode = chArr[ chArr.Length - 1 ];``````

So, the complexity will be O(3n+2k) against O(2n+k) in initial solution. Almost the same, but without the bug.

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

I believe that condition should be the "smallest" substring. Otherwise the largest substring would be s1 itself (if it contains all characters from s2).

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

We could have 2 sub strings with the same count of characters as in s2 but different lengths.May be that's what the problem is about.Of course the largest substring with at least the characters in substr would be s1 as you mentioned.

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

``````// C# implementation

public class StringSearcher
{
public static string GetMax(string s1, string s2)
{
if (string.IsNullOrEmpty(s2))
return string.Empty;

var charSet = new HashSet<char>(s2);

StringBuilder sb = null;
string current = string.Empty;

foreach (char c in s1)
if (false == charSet.Contains(c))
{
if (sb != null)
{
if (sb.Length > current.Length)
current = sb.ToString();
sb = null;
}
}
else
{
if (sb == null)
sb = new StringBuilder(s1.Length); // give length of s1 to avoid reallocations

sb.Append(c);
}

// the case when there's a last stringbuilder and after it no character was skipped.
if (sb != null && sb.Length > current.Length)
current = sb.ToString();

return current;
}
}``````

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

c# implementation.
O(k*n), where k, n - lengths of 2 given strings.

``````static private string GetLargestSubstring( string s1, string s2 ) {

var start = -1;
var end = -1;
var res = string.Empty;
var tmpRes = string.Empty;

for( int i = 0; i < s1.Length; i++ ) {
for( int j = 0; j < s2.Length; j++ ) {

if( s1[ i ] != s2[ j ] ){
end = i;
} else {
if( start == -1 ) start = i;
end = -1;
tmpRes += s1[ i ];
break;
}
}

if( end != -1 && start != -1) {
start = -1;
if( tmpRes.Length > res.Length ){
res = tmpRes;
}
tmpRes = string.Empty;
}
}
return res;
}``````

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

O(k + n)
Java code:

``````import java.util.HashMap;

public class MaxSubstring {

public static String maxSubstring(String s1, String s2) {
HashMap<Character, Boolean> s2CharsMap = new HashMap<>();
// mapping all unique chars
char[] s2Chars = s2.toCharArray();
for (int i = 0; i < s2Chars.length; i++) {
s2CharsMap.put(s2Chars[i], true);
}
// now ready to iterate over s1 and get substrings
char[] s1Chars = s1.toCharArray();
String maxSubstring = "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s1Chars.length; i++) {
if (s2CharsMap.get(s1Chars[i]) != null) {
sb.append(s1Chars[i]);
} else if (sb.length() > 0) {
if (sb.length() > maxSubstring.length()) {
maxSubstring = sb.toString();
}
sb.setLength(0);
} else
continue;
}
if (sb.length() > maxSubstring.length()) {
maxSubstring = sb.toString();
}
return maxSubstring;
}

public static void main(String[] args) {
String s1 = "abcdefgfoo1elt_fooMaxLength";
String s2 = "kpdrefbt!!!01 a werwerwc";
String maxSubstring = maxSubstring(s1, s2);
System.out.println("s1=\"" + s1 + "\"");
System.out.println("s2=\"" + s2 + "\"");
System.out.println("maxSubstring=\"" + maxSubstring + "\"");
System.out.println("-----------------------------------------------------");
s2 = "xaM Length of";
maxSubstring = maxSubstring(s1, s2);
System.out.println("s1=\"" + s1 + "\"");
System.out.println("s2=\"" + s2 + "\"");
System.out.println("maxSubstring=\"" + maxSubstring + "\"");
}

}``````

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.

### 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.