Microsoft Interview Question for Software Engineer in Tests


Country: United States




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

class a
{
    public static void Main()
    {
        string a = "abcde";
        string b = "bcf";
        List<char> l = new List<char>();
        Dictionary<char, int> dic = new Dictionary<char, int>();

        for (int i = 0; i < a.Length; i++)
        {
            if (!dic.ContainsKey(a[i]))
            {
                dic.Add(a[i], 1);
            }
        }

        for (int j = 0; j < b.Length; j++)
        {
            if (!dic.ContainsKey(b[j]))
                l.Add(b[j]);
        }

        foreach (char c in l)
        {
            Console.Write(c);
        }
    }
}

- Anonymous November 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I'm assuming that upper are different than lower cases. But It would be a good thing to point out to the interviewer.

Here is my algorithm:

public static void removeCommon(char str1[], char str2[]){
	int letters[] = new int[256];
	for(int i = 0; i < str1.length; i++)
		letters[str1[i]]++;
	for(int i = 0; i < str2.length; i++)
		if(letters[str2[i]]>0)
			letters[str2[i]]= -1;

	int index = 0;
	for(int i = 0; i < str1.length; i++)
	{
		if(letters[str1[i]] != -1){
			str1[index++] = str1[i];
		}
	}
	while(index < str1.length)
		str1[index++] = 0;
	index = 0;

	for(int i = 0; i < str2.length; i++)
	{
		if(letters[str2[i]] != -1){
			str2[index++] = str2[i];
		}
	}
	while(index < str2.length)
		str2[index++] = 0;

	System.out.println(str1);
	System.out.println(str2);
	System.out.println("Common");
	for(int i = 0; i < letters.length; i ++){
		if(letters[i] == -1){
			char c = (char)i;
			System.out.print(c);

		}
	}
	System.out.println();
}
	public static void main(String [] args){
		removeCommon("abcdefghijklmnopqrstuvwxyz".toCharArray(),"AEIOUaeiou123".toCharArray());
		
	}

mafafito@mafafito-Aspire-4752:~/programming$ javac Amazon.java
mafafito@mafafito-Aspire-4752:~/programming$ java Amazon
bcdfghjklmnpqrstvwxyz
AEIOU123
Common
aeiou

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> November 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RemoveAndPrintCommonChar {

/*Remove common characters from two strings and print the common characters and test cases*/
public static void main(String[] args) {
String string1 = "ahob6n";
String string2 = "javbrho";

// char[] arrayofChar = string1.toCharArray();
Set<Character> setOfCharString1 = new HashSet<Character>();
for (int i=0; i<string1.length(); i++){
setOfCharString1.add(string1.charAt(i));
}

StringBuilder duplicatebuilder = new StringBuilder();
StringBuilder string2builder = new StringBuilder();

for (int j=0; j<string2.length(); j++){
char a = string2.charAt(j);
if (setOfCharString1.contains(a)){
setOfCharString1.remove(a);
duplicatebuilder.append(a);
}
else{
string2builder.append(a);
}
}
System.out.println("String 1 without duplicates: "+setOfCharString1.toString());
System.out.println("String 2 without duplicates: "+string2builder.toString());
System.out.println("Duplicates among both strings: "+duplicatebuilder.toString());

}

- Harshdeep December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RemoveAndPrintCommonChar {

	/*Remove common characters from two strings and print the common characters and test cases*/
	public static void main(String[] args) {
		String string1 = "ahob6n";
		String string2 = "javbrho";

//		char[] arrayofChar = string1.toCharArray();
		Set<Character> setOfCharString1 = new HashSet<Character>();
		for (int i=0; i<string1.length(); i++){
			setOfCharString1.add(string1.charAt(i));
		}

		StringBuilder duplicatebuilder = new StringBuilder();
		StringBuilder string2builder = new StringBuilder();

		for (int j=0; j<string2.length(); j++){
			char a = string2.charAt(j);
			if (setOfCharString1.contains(a)){
				setOfCharString1.remove(a);
				duplicatebuilder.append(a);
			}
			else{
				string2builder.append(a);
			}
		}
		System.out.println("String 1 without duplicates: "+setOfCharString1.toString());
		System.out.println("String 2 without duplicates: "+string2builder.toString());
		System.out.println("Duplicates among both strings: "+duplicatebuilder.toString());

	}

- Anonymous December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

check_duplicate( char * S1, char * S2)
{
//Hash both strings S1 and S2 with a Key = tolower(S[i]) - 'a'; Key is int
// length of String S1 : S1len = strlen(S1); S2len = strlen(S2);

if ( S1 || S2 == NULL) return;

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];
}
}

Testcases

1) Pass Null pointers
2) Pass one Null another valid character string.
3) Pass valid characters
4) Pass numbers and special characters
5) Pass huge characters with space separated
6) Record the time taken for the entire program

- Ravi December 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming all characters in a string are ASCII, declare array of 128 boolean elements.

boolean notcommon[128];
/* memset to contain all 0s */

process first string, and forevery character in first string, set corresponding element in notcommon to true

for(i=0; firststr[i]!='\0'; i++)
	notcommon[ firststr[i] - 'a'] = ~notcommon[ firststr[i] - 'a'];

Now process second string in a same manner

for(i=0; secondstr[i]!='\0'; i++)
	notcommon[ secondstr[i] - 'a'] = ~notcommon[ secondstr[i] - 'a'];

So all elements which are 1 in notcommon are appearing only in 1 of two strings. Now for each string, peform following

char *rp = str;
	char *wp = str;
	while( *rp != '\0' )
	{
		if( notcommon[ *rp ] == 1 )
			*wp++ = *rp;
		*rp++;
	}
	*wp='\0';

Eliminated all common characters. Time Complexity O( l1 + l2 ) space complexity is O(1) or O(128)

For non-ASCII characters, for variable length encoding, we'll need to maintain multiple tables for each length.

- confused_banda December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in python :

#!/usr/bin/python

s1="ashwinfg"
s2="ahdkwbgh"
i=0
j=0
l1=len(s1)
l2=len(s2)
r=[]
while(i<l1) :
    j=0
    while(j<l2) :
        if(s1[i]==s2[j]) :
            r.append(s1[i])
            
        else :
            k=1

        j=j+1 
   
    i=i+1

print(r)

- ashfsk July 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Jack williams, subbu, forgot coding, avk
Punk city

- anony November 09, 2013 | Flag Reply


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