Microsoft Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

import java.util.*;

	public static ArrayList<String> setOperation(ArrayList<String> a, ArrayList<String> b) {
		Collections.sort(a);
		Collections.sort(b);
		
		for (int i = 0, j = 0; i < a.size() && j < b.size(); i++) {
			while (j < b.size() - 1 && a.get(i).compareTo(b.get(j)) > 0) {
				j++;
			}

			if (a.get(i).equals(b.get(j))) {
				while (i < a.size() - 1 && a.get(i).equals(a.get(i+1))) {
					a.remove(i+1);
				}
				while (j < b.size() - 1 && b.get(j).equals(b.get(j+1))) {
					b.remove(j+1);
				}
				a.remove(i);
				b.remove(j);
				i--;
			}
		}
		a.addAll(b);
		return a;
	}

- corbin June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please explain

- Anonymous April 10, 2017 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Set<String> getOutput(Set<String> a,Set<String> b){
		//Set<String> common=new HashSet<>();
		Iterator itra = a.iterator();
		
		while(itra.hasNext()){
			String abc =(String) itra.next();
			if(b.contains(abc)){
				//common.add(abc);
				b.remove(abc);
				itra.remove();
			}
		}
		
		//b.removeAll(common);
		
		a.addAll(b);
		return a;
		
	}

- Anonymous June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

How are we ensuring lexical ordering of set elements?

- settyblue July 27, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Set<String> getOutput(Set<String> a,Set<String> b){
		//Set<String> common=new HashSet<>();
		Iterator itra = a.iterator();
		
		while(itra.hasNext()){
			String abc =(String) itra.next();
			if(b.contains(abc)){
				//common.add(abc);
				b.remove(abc);
				itra.remove();
			}
		}
		
		//b.removeAll(common);
		
		a.addAll(b);
		return a;
		
	}

- Avi June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Set<String> getOutput(Set<String> a,Set<String> b){
//Set<String> common=new HashSet<>();
Iterator itra = a.iterator();

while(itra.hasNext()){
String abc =(String) itra.next();
if(b.contains(abc)){
//common.add(abc);
b.remove(abc);
itra.remove();
}
}

//b.removeAll(common);

a.addAll(b);
return a;

}

- avinash June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Union {

	/**
	 * @param args
	 */
	String s="";
	int count;
	public void takes(String s1,String s2){
		 
		for(int i=0;i<s1.length();i++){
			char temp=s1.charAt(i);
			if(s2.indexOf(temp)!=-1){
				s=s+temp;
				System.out.println(s);
				
			}
			else{
				count=count+1;
				
			}
			
		}//end of for
		if(count==s1.length()){
			System.out.println("NO----------------------------------");
			
			return ;
			
		}
		String A=s1.replace(s,"");
		String B=s2.replace(s,"");
		System.out.println(A+B);
		
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Union u=new Union();
		String s1="abcd";
		String s2="cdgh";
		u.takes(s1, s2);
		
		
	}

}

- nandagirisaipranay June 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Set<String> SymmetricDifference(Set<String> a,Set<String> b){
		Set<String >temp=new HashSet<String>();
		temp.addAll(a);
		a.removeAll(b);

		b.removeAll(temp);
		
		a.addAll(b);
		
	}

- Anonymous June 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class SetOFStrings {

public static void main(String[] args) {
// TODO Auto-generated method stub

// A-b objcts that belong to A and not to b;
ArrayList<String> arr1 = new ArrayList<>();
ArrayList<String> arr2 = new ArrayList<>();
arr1.add("munny");
arr1.add("sas");
arr1.add("sunnynnnn");
arr1.add("ciaia");

arr2.add("munny");
arr2.add("sas");

arr2.add("nana");
arr2.add("mama");

arr2.add("sharma");
arr2.add("wayoming");

Iterator<String> itr = arr1.iterator();
while (itr.hasNext()) {
String temp = itr.next();

if (arr2.contains(temp)) {
arr2.remove(temp);
itr.remove();
}}

Collections.sort(arr1);
Collections.sort(arr2);

arr1.addAll(arr2);
for (String a : arr1)
System.out.println(a)}}

- MrWayne June 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

// Total time complexity  O(2*nlog(n)) + O(2n) + O(n) --> O(nlog(n))

int _tmain(int argc, _TCHAR* argv[])
{
	
    string array1[] = {"asd", "efg", "lkm", "sumit", "rohit", "zxc", "awe"}; //input1
    string array2[] = {"zxc", "awe", "efg", "zzx"}; // input2
    vector<string>  result1; // output
    vector<string>  result2; // output

    int size1 = sizeof(array1)/sizeof(array1[0]);

    sort(array1, array1 + size1); // time complexity O(nlog(n))

    int size2 = sizeof(array2)/sizeof(array2[0]);

    sort(array2, array2 + size2); // time complexity O(nlog(n))

    int k  = 0;

    for (int i = 0, j = 0; i < size1 || j < size2; ) // time complexity O(2n)
    {
       if ( i == size1)
       {
           k = 1;
       }
       else if (j == size2)
       {
           k = -1;
       }
       else
       {
           k = array1[i].compare(array2[j]);
       }

        if (k > 0)
        {
            result2.push_back(array2[j]);
            j++;
        }
        else if (k < 0)
        {
            result1.push_back(array1[i]);
            i++;
        }
        else
        {
            i++;
            j++;
        }
    }

    for(int y = 0; y < result2.size(); y++) // time complexity O(n)
    {
       result1.push_back(result2[y]);
    }

    for(int y = 0; y < result1.size(); y++)
    {
        cout << result1[y].c_str() << "  ";
    }

     int u;

     cin >> u;
    
    return 0;
}

- Sumit Khedkar July 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<string> getUnion(const vector<string>& A, const vector<string>& B)
{
    vector<string> result;
    unordered_set<string> setRemove;
    vector<string>::iterator itr;
    for (itr = B.cbegin(); itr != B.cend(); itr++)
    {
        setRemove.emplace(*itr);
    }
    for (itr = A.cbegin(); itr != A.cend(); itr++)
    {
        if (!setRemove.count(*itr))
            result.push_back(*itr);
    }
    setRemove.clear();
    for (itr = A.cbegin(); itr != A.cend(); itr++)
    {
        setRemove.emplace(*itr);
    }
    for (itr = B.cbegin(); itr != B.cend(); itr++)
    {
        if (!setRemove.count(*itr))
            result.push_back(*itr);
    }
    
    return result;
}

- LANorth August 15, 2016 | 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