Microsoft Interview Question for SDE1s


Country: United States




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

O(KN) where K is the number of entries in the input list and N is the number of digits in the longest entry.
public int countGroups(int[] values)
{
	if(values==null||values.length==0)
	{
		return 0;
	}
	HashMap<Integer,Boolean> mp=new HashMap<Integer,Boolean>();
	int groupCount=0;
	for(int x:values)
	{
		int[] digitCounts=countDigits(x);
		int key=Arrays.hashCode(digitCounts);
		if(!mp.containsKey(key))
		{
			mp.put(key);
			groupCount++;
		}
		
	}
	return groupCount;
}

private int[] countDigits(int val)
{
	int[] counts=new int[10];
	if(val==0)
	{
		counts[0]==1;
		return counts;
	}
	while(val!=0)
	{
		int rem=val%10;
		counts[rem]++;
		val/=10;
	}
	return counts;
}

- divm01986 April 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Thanks for posting this question, please post some more examples to clarify question as its vague right now.

Should this have only two groups ? 33,
15,
151

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

Please clarify question with more examples (negative ones)

- GatorsRock April 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Numbers with an odd number of digits only succeed if all
// digits are same.

function findGroupOfNumber (n) {
  var map = []
  var output = []
  var m = n.toString().split('')
  var i = 0
  for (; i < 10; ++i) {
    map[i] = []
  }
  for (i = 0; i < m.length; ++i) {
    var p = parseInt(m[i], 10)
    if (m.length % 2 !== 0) {
      if (map[p].length !== i) {
        return []
      }
      map[p].push(p)
    } else {
      map[p].push(p)
    }
  }
  var frequency = 0
  for (i = 0; i < map.length; ++i) {
    if (map[i].length === 0) {
      continue
    }
    frequency = frequency || map[i].length
    if (frequency !== map[i].length) {
      return []
    }
    output.push(map[i])
  }
  return output
}

[
  0,
  1,
  10,
  3,
  33,
  15,
  151,
  555,
  4222,
  4422,
  4442,
  4444
].forEach(function (n) {
  console.log(findGroupOfNumber(n))
})

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

clarify question with more examples please

- test April 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think that when numbers has digits with the same frequence, they belong to common group. 15 and 151 are not from the same group, because 15 has one 1 and 1 five , but 151 has 2 ones and 1 five

- EPavlova April 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Quick solution i wrote up in Java

public static ArrayList<HashMap<Integer, Integer>> getGrouping(int[] numbers) {
        if (numbers == null) return null;
        ArrayList<HashMap<Integer, Integer>> listMap = new ArrayList<>();
        HashMap<Integer, Integer> digitMap;
        for(int i : numbers) {
            digitMap = new HashMap<>();
            if (i == 0) {
                digitMap.put(i, 1);
            }
            else {
                // get each digit in the number and add it to the map
                while (i > 0) {
                    int nextNum = i % 10;
                    if (digitMap.get(nextNum) == null) {
                        digitMap.put(nextNum, 1);
                    }
                    else {
                        int currentCount = digitMap.get(nextNum);
                        digitMap.put(nextNum, currentCount + 1);
                    }
                    i = i/10;
                }
            }
            // add the generated map to the list
            listMap.add(digitMap);
        }
        return listMap;
    }

- tmgunternet April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//problem solved in C++ 
#include <map>
#include <assert.h>

int problem(std::vector<int> &list)
{
	int totalNumberOfMatchingGroups = 0;
	for(auto i : list)
	{
		if( i < 10 )
			totalNumberOfMatchingGroups++;
		else
		{
			int remainder = i;
			int div = -1;
			std::map<int,int> countMap;
			while( remainder > 0)
			{
				div = remainder % 10;
				if ( countMap.find(div) != countMap.end())
					countMap[div]++;
				else 
					countMap[div] = 1;

				remainder = remainder / 10;
			}
			assert(countMap.size() >0 );
			int comp = (countMap.begin())->second;
			bool bCount = true;
			for(const auto& val : countMap)
			{
				if(comp != val.second)
				{
					bCount = false;
					break;
				}
			}
			if(bCount)
				totalNumberOfMatchingGroups++;
		}

	}
	return totalNumberOfMatchingGroups;
}

you can test the code using

int _tmain(int argc, _TCHAR* argv[])
{
	int arr[] = {1,10,3,33};
	vector<int> A (arr, arr + sizeof(arr) / sizeof(arr[0]) );
	cout<<problem(A)<<endl;

	int arr1[] = {1};
	vector<int> A1 (arr1, arr1 + sizeof(arr1) / sizeof(arr1[0]) );
	cout<<problem(A1)<<endl;

	int arr2[] = {	0,
					1,
					10,
					3,
					33,
					15,
					151,
					555,
					4222,
					4422,
					4442,
					4444
				};

	vector<int> A2 (arr2, arr2 + sizeof(arr2) / sizeof(arr2[0]) );
	cout<<problem(A2)<<endl;

return 0;

- GhostInABox May 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "iostream"
#include "vector"
#include "map"
#include "algorithm"
#include "functional"
using namespace std;


int main(int argc, char** argv){
	vector<int> numbers {1,10,33,3};
	int cnt = 0;
	for(int i = 0; i < numbers.size() ; i++){
		int val = numbers[i];

		map<int , int> mp;
		while(val){
			int num = val%10;
			val = val/10;

			if(mp.find(num) != mp.end()){
				mp[num] += mp[num];
			}
			else{
				mp.insert(make_pair(num , 1));
			}
		}

		bool notingrp = false;
		map<int , int>::iterator itr_prev = mp.begin();
		map<int , int>::iterator itr_curr = mp.begin();
		itr_curr++;
		for( ; itr_curr != mp.end() ; itr_curr++){
			if(itr_curr->second != itr_prev->second){
				notingrp = true;
				break;
			}
		}
		if(!notingrp){
			cnt++;
		}
	}
	cout << cnt;
	return 0;
}

- michael May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "iostream"
#include "vector"
#include "map"
#include "algorithm"
#include "functional"
using namespace std;


int main(int argc, char** argv){
	vector<int> numbers {1,10,33,3};
	int cnt = 0; 
	for(int i = 0; i < numbers.size() ; i++){
		int val = numbers[i];

		map<int , int> mp;
		while(val){
			int num = val%10;
			val = val/10;

			if(mp.find(num) != mp.end()){
				mp[num] += mp[num];
			}
			else{
				mp.insert(make_pair(num , 1));
			}
		}

		bool notingrp = false;
		map<int , int>::iterator itr_prev = mp.begin();
		map<int , int>::iterator itr_curr = mp.begin();
		itr_curr++;
		for( ; itr_curr != mp.end() ; itr_curr++){
			if(itr_curr->second != itr_prev->second){
				notingrp = true;
				break;
			}
		}
		if(!notingrp){
			cnt++;
		}
	}
	cout << cnt;
	return 0;

}

- deeath_call May 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var a=11356644337;
a = a.toString().split('');
var countMap = {}; 
for(var i in a){
countMap[a[i]] = countMap[a[i]]?countMap[a[i]]++:1
}
console.log(countMap);

- Pranay Kumar Sanam June 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, i can't able to understand the question. Can anyone, please explain me?

- Sathish Kumar June 20, 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