Google Interview Question for SDE1s


Country: United States




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

@alex

Your first method fails since all it does it checks to see if the character is unique in the string. It doesn't take into account or the character in the pattern is being repeated.
Ex:
S= abcdef
Pattern = abcabc
Your function will return true.


Also, your second method also fails since all it does is it captures the unique character count. It doesn't check if it matches the pattern. For ex:

Pattern= abcdef

S= kaulpj

The pattern is that each char is one off the previous where as the string doesn't capture that. This will pass since all chars are unique in both pattern and s. Is this relevant in for this question? Not sure :0

- Shah December 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

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

using namespace std;

bool IsEncoded(string const &s, string const &pattern)
{
	if (s.size() != pattern.size()) {
		return false;
	}
	unordered_map<char, char> seen_in_s, seen_in_p;
	for (int i = 0; i < s.size(); ++i) {
		auto it = seen_in_s.find(s[i]);
		if (it != seen_in_s.end()) {
			if (pattern[i] != it->second) {
				return false;
			}
		} else {
			seen_in_s[s[i]] = pattern[i];
		}

		it = seen_in_p.find(pattern[i]);
		if (it != seen_in_p.end()) {
			if (s[i] != it->second) {
				return false;
			}
		} else {
			seen_in_p[pattern[i]] = s[i];
		}
	}
	return true;
}

class EncodingChecker {
	public:
		EncodingChecker(vector<string> const &patterns)
		{
			for (auto &p : patterns) {
				normalized_patterns_.insert(Normalize(p));
			}
		}
		bool IsEncoded(string const &s) const
		{
			return normalized_patterns_.find(Normalize(s)) != normalized_patterns_.end();
		}

	private:
		string Normalize(string const &s) const
		{
			unordered_map<char, char> seen;
			char val = 0;
			string out;
			for (char c : s) {
				auto it = seen.find(c);
				if (it != seen.end()) {
					out += it->second;
				} else {
					out += val;
					seen[c] = val++;
				}
			}
			return out;
		}
		unordered_set<string> normalized_patterns_;
};

int main()
{
	cout << IsEncoded("abcdef", "abcabc") << "\n";
	cout << IsEncoded("cbzabc", "abcabc") << "\n";
	cout << IsEncoded("xyzxyz", "abcabc") << "\n";

	EncodingChecker checker({"abcabc", "yzyzyz", "aaabbbaaa"});
	cout << checker.IsEncoded("123123") << "\n";
	cout << checker.IsEncoded("hahaha") << "\n";
	cout << checker.IsEncoded("777999777") << "\n";
	cout << checker.IsEncoded("121213") << "\n";
}

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

@Shah. Thank you! Fixed the first part.

Regarding the second part. I assume that the pattern doesn't force distance between characters. I.e. I assume that pattern "aza" matches string "121", doesn't taking in account that distance from 'a' to 'z' is not the same as distance from '1' to '2'. If there was an interviewer, it's definitely a good question to ask if the pattern should force distance or not.

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

@Alex solution is what I'd do as well. Normalize pattern and string.

- Chris January 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Check if following works -

class EncodingChecker { 
String patternString=null;
EncodingChecker (String pattern) {
	patternString = getpattern(pattern);
}  

boolean isEncoded (String s) {
	return patternString.equals(getpattern(s));
} 

String getpattern(String str){
	String[] arr = str.toChar();
	Map<String,int> map = new HashMap<String,int>();
	StringBuilder pattern = new StringBuilder(str.length);
	int index =1;	
	for(String s:arr){
		if(map.containsKey(s)){
			pattern.append(map.get(s));
		}else{
			map.put(s,index);
			pattern.append(index++);
		}
	}
}

}

- Kunal December 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

class EncodingChecker { 
String patternString=null;
EncodingChecker (String pattern) {
	patternString = getpattern(pattern);
}  

boolean isEncoded (String s) {
	return patternString.equals(getpattern(s));
} 

String getpattern(String str){
	String[] arr = str.toChar();
	Map<String,int> map = new HashMap<String,int>();
	StringBuilder pattern = new StringBuilder(str.length);
	int index =1;	
	for(String s:arr){
		if(map.containsKey(s)){
			pattern.append(map.get(s));
		}else{
			map.put(s,index);
			pattern.append(index++);
		}
	}
}

}

- kunalkhanvilkar.pune December 18, 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