Google Interview Question for Software Engineers


Country: United States




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

const getevenAndOddFreqs = str => [...str].reduce(([evenFreqsAcc, oddFreqsAcc], char, i) => {
	if (i % 2) {
		return [{...evenFreqsAcc, [char]: (evenFreqsAcc[char] || 0) + 1}, oddFreqsAcc]
	}
	return [evenFreqsAcc, {...oddFreqsAcc, [char]: (oddFreqsAcc[char] || 0) + 1}] 
}, [{}, {}])

const areEquivalent = (str1, str2) => {
	if (str1.length !== str2.length) return false

	const [evenStr1Freqs, oddStr1Freqs]  = getEvenAndOddFreqs(str1)
	const [evenStr2Freqs, oddStr2Freqs] = getEvenAndOddFreqs(str2)

	return Object.keys(evenStr1Freqs).every(char => evenStr1Freqs[char] === evenStr2Freqs[char]) &&
	Object.keys(evenStr2Freqs).every(char => evenStr2Freqs[char] === evenStr1Freqs[char]) &&
	Object.keys(oddStr1Freqs).every(char => oddStr1Freqs[char] === oddStr2Freqs[char]) &&
	Object.keys(oddstr2Feqs).every(char => oddStr2Freqs[char] === oddStr2Freqs[char])

}

- Anonymous August 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As soon as there's no operation that allows to swap a character on odd position with a character on even position, this problem can be split into two subproblems;
1) is a set of characters from odd positions of the first string equal to a set of characters from odd positions of the second string,
2) is a set of characters from even positions of the first string equal to a set of characters from even positions of the second string ?

Assuming that there're only lowercase letters, the code will look like:

std::array<int32_t, 26> odd_s1{}, odd_s2{};
  std::array<int32_t, 26> even_s1{}, even_s2{};

  auto count = [](const std::string &s, const int32_t begin,
                          std::array<int32_t, 26> &set) -> void {
                           for (int32_t i = begin, length = s.length(); i < length; i += 2) {
                               ++set[s[i] - 'a'];
                           }
                       };
    count(s1, 0, even_s1); count(s1, 1, odd_s1);
    count(s2, 0, even_s2); count(s2, 1, odd_s2);

    std::cout << s1 << " "
              << (even_s1 == even_s2 && odd_s1 == odd_s2 ? "" : "Not ")
              << "equivalent to "
              << s2 << '\n';

- np August 10, 2018 | 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