## Google Interview Question for Software Engineers

Country: United States

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

Looking for interview experience sharing and coaching?

Visit AONECODE.COM for ONE-TO-ONE private lessons by FB, Google and Uber engineers!

SYSTEM DESIGN Courses (highly recommended for candidates of FB, LinkedIn, Amazon, Google & Uber etc.)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algorithms & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!

SOLUTION:
s1Derives and s2Derives are the words reachable by swapping characters in s1 and s2 in any random way. If s1and s2 are anagrams, for sure s1Derives and s2Derives would join at some point. Find out all possible derivatives 'nextLevel' of s1/s2. Then find out all derivatives of words in 'nextLevel'......until the earliest joint of s1Derives and s2Derives occurs. Return the number of levels gone through.

``````int minNumberOfSwaps(String s1,String s2) {
//if(!isAnagram(s1, s2)) return Integer.MAX_VALUE; //assume s1 and s2 are anagrams
int step = 0;
Set<String> s1Derives = new HashSet<>();
Set<String> s2Derives = new HashSet<>();
Set<String> visited = new HashSet<>();
int len = s1.length();
while(!containsSameString(s1Derives, s2Derives)) {
Set<String> set = step % 2 == 0 ? s1Derives: s2Derives;
Set<String> nextLevel = new HashSet<>();
for(String s: set) {
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
if(s.charAt(i) != s.charAt(j)) {
char[] charArray = swap(s.toCharArray(), i, j);
String derived = new String(charArray);
if(!visited.contains(new String(derived))) {
}
}
}
}
}
if(step % 2 == 0) s1Derives = nextLevel;
else s2Derives = nextLevel;
step++;
}
return step;
}``````

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

1) select only the dissimilar elements in the same position from both string
2) create a list of source and dest.
3) do a BFS starting on source and each distance is one swap.
4) stop when you reach dest string

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

What do you mean by swap. Can swap be possible within 2nd string only?

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

@ajay.raj Can you please provide an example..

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

O(n2), edit distance

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

int minNumberOfSwaps(String s1,String s2) {
//if(!isAnagram(s1, s2)) return Integer.MAX_VALUE; //assume s1 and s2 are anagrams
int step = 0;
Set<String> s1Derives = new HashSet<>();
Set<String> s2Derives = new HashSet<>();
Set<String> visited = new HashSet<>();
int len = s1.length();
while(!containsSameString(s1Derives, s2Derives)) {
Set<String> set = step % 2 == 0 ? s1Derives: s2Derives;
Set<String> nextLevel = new HashSet<>();
for(String s: set) {
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
if(s.charAt(i) != s.charAt(j)) {
char[] charArray = swap(s.toCharArray(), i, j);
String derived = new String(charArray);
if(!visited.contains(new String(derived))) {
}
}
}
}
}
if(step % 2 == 0) s1Derives = nextLevel;
else s2Derives = nextLevel;
step++;
}
return step;
}

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

Is it not

``````Swap[n-1] = Swap[n-2] + 1 if A[n-1] != B[n-1]
= Swap[n-2] otherwise``````

?

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

``````from collections import deque

def f(s, t):
h = set([s])
s = [list(s), 0]
q = deque([s])
while q:
e, n = q.popleft()
if "".join(e) == t:
return n
else:
for i in range(len(e)):
for j in range(len(e)):
temp = e[i]
e[i] = e[j]
e[j] = temp
str = "".join(e)
if str not in h:
q.append([e[:], n+1])
e[j] = e[i]
e[i] = temp``````

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

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int FindMinimum(int i, string s1, string& s2)
{
if(s1[i] == s2[i])
{
return 0;
}
int length = s1.length();
string final;
int MIN = INT_MAX;
for(int j=0; j<length; j++)
{
if(s1[j] != s2[j] && s1[i] == s2[j])
{
string dummy = s2;
dummy[j] = s2[i];
dummy[i] = s1[i];
int min = FindMinimum(j, s1, dummy);
if(min < MIN)
{
MIN = min;
final = dummy;
}
}
}
s2 = final;
return MIN + 1;
}
int main() {
string s1;
string s2;
cin>>s1;
cin>>s2;
int count = 0;
int length = s1.length();
for(int i=0; i<length; i++)
{
count += FindMinimum(i, s1, s2);
}
cout<<count;
}

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.

### 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.