Google Interview Question for SDE1s


Country: United States




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

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

unordered_map<string, int> Preprocess(vector<pair<string, int>> const &a)
{
	unordered_map<string, int> m;
	for (auto &el : a) {
		string const &w = el.first;
		int val = el.second;
		for (int i = 1; i < w.size(); ++i) {
			for (int j = i + 1; j < w.size(); ++j) {
				string key = w.substr(0, i) + '|' + w.substr(j);
				m[key] = max(m[key], val);
			}
		}
		m[w + '|'] = max(m[w], val);
		m['|' + w] = max(m[w], val);
	}
	return m;
}

int GetMax(unordered_map<string, int> const &m, string const &pref, string const &suff)
{
	auto it = m.find(pref + '|' + suff);
	return it == m.end() ? -1 : it->second;
}

int main()
{
	auto m = Preprocess({
		{"google", 30},
		{"gogle", 20},
	});
	cout << GetMax(m, "go", "le") << "\n";
}

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

def matching_prefix_postfix(M, pre, post):
L = len(pre) + len(post)
for i in M:
if( L <= len(i[0]) and
pre == i[0][:2] and
post == i[0][-2:]):
return i[1]

M =[['piikukp', 40],
['pickup', 41],
['pickkkp', 42],
['pikp', 43],
['upckpi', 44]]

print("Matching number:", matching_prefix_postfix(M, 'pi', 'up'))

G =[['google', 30],
['gogle', 20]]
print("Matching number:", matching_prefix_postfix(G, 'go', 'le'))

#Output => 41, 30

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

def matching_prefix_postfix(M, pre, post):
    L = len(pre) + len(post)
    for i in M:
        if( L <= len(i[0]) and
            pre == i[0][:2] and 
            post == i[0][-2:]):
           return i[1]

    
M =[['piikukp', 40],
    ['pickup', 41],
    ['pickkkp', 42],
    ['pikp', 43],
    ['upckpi', 44]]

print("Matching number:", matching_prefix_postfix(M, 'pi', 'up'))

G =[['google', 30],
    ['gogle', 20]]
print("Matching number:", matching_prefix_postfix(G, 'go', 'le'))

#Output : 41, 30

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

Keep two tries one for original strings and one for reverse strings. Do prefix search for queries and merge common values.

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

@sri. E.g. the query is "g", "e". In the first trie we'll find 1 million of words beginning with "g", in the second trie we'll find 1 million words ending with "e". How would we merge the two sets efficiently?

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

@Alex @sri

Do a prefix search and store all hits in a hashmap with value of 0. Then do a suffix search and only update the value is already present in the hashmap (aka hits from prefix search). Do a final iteration and get word with Max value .

- Shah December 15, 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