Interview Question for SDE-2s


Country: United States




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

(zoomba)m = { 1 : [ 'a' , 'x' ] , 2 : ['b','y'] }
{1=@[ a,x ], 2=@[ b,y ]} // ZMap
(zoomba)join( @ARGS = list( '12'.value ) -> { m[int($.o)]} ) ->{str($.o,'') }
[ ab,ay,xb,xy ] // ZList

Hmm. Not that hard. Probably a standard recursive solution would help here, to make it harder and *interview* class:

/* standard recursion */
map = { 1 : [ 'a' , 'x' ] , 2 : ['b','y'] }

def _recurse(string, options, tmp='') {
   if ( empty(string) ){  options+= tmp ; return }
   head = string[0]
   tail = string[1:-1]
   for ( o :  map[ int(head) ] ){
     _recurse( tail, options, tmp + o )
   }
}

def mapper( string ){
  options = list()
  _recurse(string,options)
  options // return 
}
println( mapper('12') )

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

Solution in python. The cost is the result of multiplying each character of the string by the values that can be replaced for.

def computeCombinations(string, mapping):
    values = [ mapping[x] for x in string ]
    results = [[]]
    for v in values:
        results = [x + [y] for x in results
                           for y in v]
    return results

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

#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <stack>
#include <vector>

using namespace std;

map<char, list<char>> str = {
	{ '1', { 'a', 'b', 'c', 'd', 'e' } },
	{ '2', { 'v', 'w', 'x', 'y', 'z' } }
};
list<string> combs;

void getCombinations(string& currentCombination, string& sub) {
	if (sub.empty()) {
		combs.push_back(currentCombination);
		return;
	}
	for (auto ch : str[sub[0]]) {
		currentCombination.push_back(ch);
		getCombinations(currentCombination, sub.substr(1));
		currentCombination.pop_back();
	}
}

int main()
{
	string input = "12212";
	string currentCombination;
	getCombinations(currentCombination, input);
	
	for (auto combination : combs) {
		cout << combination << endl;
	}
	string line;
	cout << "Press enter to close" << endl;
	getline(cin, line);
	return 0;
}

- Omkar June 17, 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