Facebook Interview Question


Country: United States




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

Working code

////////////////////////////////////////////////////////////////////////////////////
//Input: "129"
//Output: //ADY,ADZ,AEY,AEZ,AFY,AFZ,BDY,BDZ,BEY,BEZ,BFY,BFZ,CDY,CDZ,CEY,CEZ,CFY,CFZ

//Input: "239"
Output: DGY,DGZ,DHY,DHZ,DIY,DIZ,EGY,EGZ,EHY,EHZ,EIY,EIZ,FGY,FGZ,FHY,FHZ,FIY,FIZ
////////////////////////////////////////////////////////////////////////////////////
var letComb = FindCharProduct("129");
Console.WriteLine($"{string.Join(",", letComb)}");

private static HashSet<string> FindCharProduct(string inDigis)
        {
            var inpDic = new Dictionary<char, List<char>>() {
                { '1',new List<char>() {'A', 'B', 'C'}},
                { '2',new List<char>() { 'D', 'E', 'F' } },
                { '3',new List<char>() { 'G', 'H', 'I' } },
                { '4',new List<char>() { 'J', 'K', 'L' } },
                { '5',new List<char>() { 'M', 'N', 'O' } },
                { '6',new List<char>() { 'P', 'Q', 'R' } },
                { '7',new List<char>() { 'S', 'T', 'U' } },
                { '8',new List<char>() { 'V', 'W', 'X' } },
                { '9',new List<char>() { 'Y', 'Z'} },
            };
            var outDic = new HashSet<string>();            
            digitoLetterComb(inDigis);
            void digitoLetterComb(string strKeys)
            {
                if (!strKeys.Any(char.IsDigit))
                {
                    outDic.Add(strKeys);                    
                    return;
                }
                char digit = strKeys.First(char.IsDigit);
                foreach (var ch in inpDic[digit])
                {
                    digitoLetterComb(strKeys.Replace(digit, ch));
                }
            }
            return outDic;
        }
////////////////////////////////////////////////////////////////////////////////////

- syam.bollu May 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Scala:

object Main {  
  
  val mapping = Map(1 -> "ABC", 2 -> "DEF") 
  
  def main(args: Array[String]): Unit = {            
    val a = mapping.keySet.toArray        
    var c = Array.ofDim[Char](a.length)
    printCombos(a, 0, c)
  }   
    
  def printCombos(a:Array[Int], i:Int, c:Array[Char]) : Unit = {    
    if (i == a.length) {			
      c foreach print
      println
		} else if (i < a.length) {
		  var j = 0		  
			for (j <- 0 to mapping(a(i)).length() - 1) {
				c(i) = mapping(a(i)).charAt(j)
				printCombos(a, i + 1, c)
			}
		}
	}
}

// output:
/*
AD
AE
AF
BD
BE
BF
CD
CE
CF
*/

- guilhebl May 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how you do it - in a fully declarative paradigm.

__mapping__ = {
    1 : [ 'A', 'B', 'C' ], 2 : [ 'D', 'E', 'F' ],
    3 : [ 'G', 'H', 'I' ], 4 : [ 'J', 'K', 'L' ],
    5 : [ 'M', 'N', 'O' ], 6 : [ 'P', 'Q', 'R' ],
    7 : [ 'S', 'T', 'U' ], 8 : [ 'V', 'W', 'X' ],
    9 : ['Y', 'Z'] }

def map_back(n){
    args = list( str(n).value ) as { __mapping__[int($.o)] }
    join(@ARGS = args ) where { true } as { str($.o,'') }
}
println(map_back(12))

The result:

zmb tmp.zm
[ AD,AE,AF,BD,BE,BF,CD,CE,CF ]

- NoOne May 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Combs(unordered_map<char, vector<char>> &map, string const &digits,
	vector<string> &combs, string const comb = "")
{
	if (comb.size() == digits.size()) {
		if (!comb.empty()) {
			combs.push_back(comb);
		}
		return;
	}
	int digit_idx = comb.size();
	char digit = digits[digit_idx];
	auto it = map.find(digit);
	if (it != map.end()) {
		vector<char> const &letters = it->second;
		for (char c : letters) {
			Combs(map, digits, combs, comb + c);	
		}
	}
}

- Alex May 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findcombination(self, s):
  d={'1':'','2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz','10':''}
  value_list = list()
  if len(s) == 0:
      return []
  result = [""]
  for digit in s:
     value_list=d[digit]
     for i in value_list:
         out=[]
         for j in result:
              out.append(i+j)
     result=out
  return result

- deepjoarder July 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const map = {
  '1': ['A', 'B', 'C'],
  '2': ['D', 'E', 'F']
}

const input = '112'
let output = ['']

for (let i = 0; i < input.length; i++) {
  let tmp = map[input.charAt(i)]
  
  output = output
    .map(a => tmp.map(b => a + b))
    .reduce((a, b) => a.concat(b), [])  
}

console.log(output)

- Prosperi December 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const output = input
                .split('')
                .reduce((x, y) => 
                  x
                    .map(a => map[y].map(b => a + b))
                    .reduce((a, b) => a.concat(b), [])
                , [''])

- prosperrriii December 02, 2017 | 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