Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace T1
{
class Pad
{
public int digit;
public char[] letter;
}
class Test1
{
private HashSet<string> words = new HashSet<string>();
private int[] phoneNumber;
private Pad[] pad;
public Test1()
{
words.Add("what");
words.Add("this");
words.Add("nothing");
words.Add("aran");
phoneNumber = new int[] { 7, 0, 1, 8, 2, 3 };
pad = new Pad[4];
pad[0] = new Pad();
pad[0].digit = 1;
pad[0].letter = new char[] { 'a', 'w', 't' };
pad[1] = new Pad();
pad[1].digit = 2;
pad[1].letter = new char[] { 'r', 'w', 'h' };
pad[2] = new Pad();
pad[2].digit = 3;
pad[2].letter = new char[] { 'a', 'n', 'f' };
pad[3] = new Pad();
pad[3].digit = 4;
pad[3].letter = new char[] { 'n', 'j', 't' };
}
public void FindOneWord(int[] letters)
{
string prototype = "";
int nWhich = 0;
foreach (int position in phoneNumber)
{
if (position >= 0 && position < pad.Length)
{
prototype += pad[position].letter[letters[nWhich]];
++nWhich;
}
}
if (words.Contains(prototype))
{
Console.WriteLine(prototype);
}
}
public void FindAllWords()
{
int[] letters = { 0, 0, 0, 0 };
for (int counter = 0; counter <= 2222; ++counter)
{
int k3 = counter % 10;
int k2 = (counter % 100) / 10;
int k1 = (counter % 1000) / 100;
int k0 = (counter % 10000) / 1000;
if (k3 >= 3) continue;
if (k2 >= 3) continue;
if (k1 >= 3) continue;
if (k0 >= 3) continue;
//Console.Write(k0);
//Console.Write(k1);
//Console.Write(k2);
//Console.Write(k3);
//Console.WriteLine();
letters[0] = k0;
letters[1] = k1;
letters[2] = k2;
letters[3] = k3;
FindOneWord(letters);
}
}
}
class Program
{
static void Main(string[] args)
{
Test1 t1 = new Test1();
t1.FindAllWords();
}
}
}
// Shows the full declarative power of ZoomBA
tel_dict = { 2 : [ 'a','b','c' ] , 3 : ['d' ,'e' ,'f' ],
4 : [ 'g' , 'h' ,'i' ], 5 : [ 'j', 'k', 'l' ],
6 : ['m','n','o' ], 7 : [ 'p', 'q','r' ,'s' ],
8 : [ 't' ,'u','v' ], 9 : [ 'w' , 'y' ,'z' ] }
// meaningful words?
words = select( file('/usr/share/dict/words') ) :: { #|$.o| == 7 } -> { $.o.toLowerCase }
ph_no = 2742538 // your input : cricket
args = list( str(ph_no).value ) -> { tel_dict[ int($.o) ] }
meaningful = join( @ARGS = args ) :: {
word = str( $.o ,'' )
word @ words } -> { str( $.o ,'' ) }
println ( meaningful )
def find_all_words(prefix, current_alphabet, more_alphabets):
next_alphabet = more_alphabets[0] if len(more_alphabets) >= 1 else []
words = set([prefix]) if not current_alphabet else set()
for letter in current_alphabet:
words |= find_all_words(prefix + letter, next_alphabet,
more_alphabets[1:])
return words
if __name__ == '__main__':
num_pad = {2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'],
4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'],
6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'],
8: ['t', 'u', 'v'], 9: ['w', 'y', 'z']}
phone_number = '7899533'
alphabets = [num_pad[int(num)] for num in phone_number]
# word list from github
real_words = set(open('words.txt', 'r').read().splitlines())
created_words = find_all_words('', alphabets[0], alphabets[1:])
print [word for word in created_words if word in real_words]
- Anonymous November 24, 2016