Microsoft Interview Question
Software Engineer in TestsCountry: United States
public class RemoveDuplicates
{
private static String str1;
private static String str2;
private static boolean flag=false;
private static int i;
public static void main(String [] a)
{
str1=a[0];
System.out.println("1st Input: "+ str1);
str2=a[1];
System.out.println("2nd Input: "+ str2);
new RemoveDuplicates().remover(str1,str2);
}
public void remover(String s1, String s2)
{
String token = "\\ ";
String [] fs = s1.split(token);
String [] ss = s2.split(token);
for( i=0;i<fs.length;i++)
{
if(fs[i]!=null)
{
int j=0;
do
{
while(ss[j]==null)
{
j++;
}
if(fs[i].equals(ss[j]))
{
ss[j]=null;
flag=true;
}
j++;
}while(j<ss.length);
int k=i+1;
do
{
if(k==fs.length)
break;
while(fs[k]==null)
{
k++;
if(k==fs.length) break;
}
if(k==fs.length)
break;
if(fs[i].equals(fs[k]))
{
fs[k]=null;
flag=true;
}
k++;
}while(k<fs.length);
}
if(flag)
{
fs[i]=null;
flag=false;
}
}
System.out.println ("Post Removal of Duplicate Strings ");
System.out.print ("1st String becomes: ");
for(String s: fs)
{
if(s!=null)
{
System.out.print(s + " ");
}
}
System.out.println();
System.out.print ("2nd String becomes: ");
for(String s: ss)
{
if(s!=null)
System.out.print(s + " ");
}
}
}
==============================
OUTPUT:
=======
1st Input: Subham is good boy Subham
2nd Input: Subham is not a good girl
Post Removal of Duplicate Strings
1st String becomes: boy
2nd String becomes: not a girl
Remove duplicates of chars as well? removed duplicates from one of the strings? What happens if you get the following 2 strings: "aa", "aa"?
check_duplicate( char * S1, char * S2)
{
//Hash both strings S1 and S2 with a Key = tolower(S[i]) - 'a';
// length of String S1 : S1len = strlen(S1); S2len = strlen(S2);
for (i = 0; i < S1len; i++) {
key = tolower(S1[i]) - 'a' ;
A[ key ] = A[key] + 1;
}
for (i = 0; i < S2len; i++) {
key = tolower(S2[i]) - 'a' ;
A[ key ] = A[key] - 1;
}
// Now check the hash value for S1, find out duplicates and copy the not duplicates to target array
for (i = 0; i < S1len ; i++) {
key = tolower(S1[i]) - 'a';
if ( A[ key ] != 0) target1[i] = S1[i];
}
// Now check the hash value and find out duplicates in S2 and copy not duplicates to target2
for (i = 0; i < S2len ; i++) {
key = tolower(S2[i]) - 'a';
if ( A[ key ] != 0) target2[i] = S2[i];
}
}
public List<char> RemoveDupInStr(string str1, string str2)
{
List<char> Res = new List<char>();
List<char> CommonStr = new List<char>();
for (int i = 0; i < str1.Length; i++)
{
if (Res.Contains(str1[i]))
{
CommonStr.Add(str1[i]);
}
else
{
Res.Add(str1[i]);
}
}
for (int i = 0; i < str2.Length; i++)
{
if (Res.Contains(str2[i]))
{
CommonStr.Add(str1[i]);
}
else
{
Res.Add(str2[i]);
}
}
return Res;
}
public List<char> RemoveDupInStr(string str1, string str2)
{
List<char> Res = new List<char>();
List<char> CommonStr = new List<char>();
for (int i = 0; i < str1.Length; i++)
{
if (Res.Contains(str1[i]))
{
CommonStr.Add(str1[i]);
}
else
{
Res.Add(str1[i]);
}
}
for (int i = 0; i < str2.Length; i++)
{
if (Res.Contains(str2[i]))
{
CommonStr.Add(str1[i]);
}
else
{
Res.Add(str2[i]);
}
}
return Res;
}
Simple Ruby Solution in linear time
# O(n)
def remove_duplicates str1, str2
bigger = (str1.length > str2.length ? str1 : str2).split('')
smaller = (bigger == str1 ? str2 : str1).split('')
bigger.each do |c|
if smaller.include?(c)
bigger.delete(c)
smaller.delete(c)
end
end
smaller.join + ' ' + bigger.join
end
import java.util.*;
public class Recusiveley_Remove_all_Duplicates {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String str = "";
str = in.next();
System.out.println("Initial String is: " + str);
System.out.println("Removing Duplicates ...");
String retVal ="";
int initialLength = 0;
int finalLength = str.length();
retVal = str;
/* Check if string is of length 1 or 0 */
if(str.length() == 1 || str.length() == 0)
{
initialLength = finalLength;
}
while(initialLength != finalLength)
{
initialLength = finalLength;
retVal = removeDuplicates(retVal);
finalLength = retVal.length();
}
System.out.println("Final String is: " + retVal);
in.close();
}
public static String removeDuplicates(String strVal)
{
int ctr = 0;
for(int i=1; i<strVal.length(); i++)
{
if (strVal.toLowerCase().substring(i-1, i).equals(strVal.toLowerCase().substring(i,i+1)))
{
strVal = strVal.substring(0, i) + strVal.substring(i+1, strVal.length());
ctr = 1;
i -= 1;
}
else if(ctr == 1)
{
strVal = strVal.substring(0, i-1) + strVal.substring(i, strVal.length());
ctr = 0;
i -= 1;
}
else{}
}
if (ctr == 1)
{
strVal = strVal.substring(0, strVal.length()-1);
}
return strVal;
}
}
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
#define S 6
int main()
{
string n1 = "aabbb", n2 = "aaaa";
int s1 = n1.length(), s2 = n2.length();
int bigger = ((s1 > s2) ? s1 : s2);
int smaller = ((s1 < s2) ? s1 : s2);
for (int z = 0; z < bigger; ++z)
{
char c = n1[z];
for (int j = 0; j < smaller; ++j)
{
if (c == n2[j])
{
n1[z] = '*';
n2[j] = '*';
break;
}
}
}
cout << "n1 : " << n1 << endl;
cout << "n2 : " << n2 << endl;
string score1 = "**bbb", score2 = "**aa";
assert(score1==n1);
assert(score2 == n2);
system("pause");
return 0;
}
Please provide some more data... or at least an example.
- celeritas November 10, 2013Data insufficient.