Microsoft Interview Question
SDE1sCountry: United States
// 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))
})
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;
}
//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;
#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;
}
#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;
}
- divm01986 April 22, 2016