Google Interview Question for SDE1s


Country: United States




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

int Func2()
{
char source[] = "abc";
char search[] = "cba";
std::vector<std::vector<std::string>> stringsCount;
int lenght = strlen(source);
int i = 0;
char *ss = new char[lenght + 1]{'\0'};
for (int j = 1; j < lenght; j++)
{
strcpy_s(ss,sizeof(char)*lenght+1, source);
std::vector < std::string> strings;
for (i = 0; i<lenght; i += j)
{
char temp = ss[0];
ss[0] = ss[i];
ss[i] = temp;
strings.push_back(std::string(ss));
if (!strcmp(ss, search))
{
stringsCount.push_back(strings);
break;
}
}
}
if (stringsCount.size() > 0)
{
int min = stringsCount[0].size();
for (int i=0; i < stringsCount.size(); i++)
{
if (min>stringsCount[i].size())
min = stringsCount[i].size();

}
return min;
}
return -1;

}

- leonzakaim February 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int countMoves(String s, String t) {
    int n = s.length(), moves = 0;

    if (n != t.length()) return -1;

    for (int i = n - 1, j = n - 1; i >= 0; i--) {
        if (s.charAt(i) == t.charAt(j)) {
            j--;
        } else {
            moves++;
        }
    }

    return moves;
}

- jgriesser February 03, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Main {
    public static void main(String[] args) {
        System.out.println(check("ABD","BAD"));
    }
    static int check(String S, String D){
        int i=S.length()-1;
        int j=D.length()-1;
        int result=0;
        while(i>0 && j>0){
            char s=S.charAt(i);
            char d=D.charAt(j);
            if(s==d){
                i--;
                j--;
                continue;
            }
            result++;
            i--;
        }
        return result;
  }
}

- viveksingh.heritage February 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countMoves(string src, string dst) {
	int moves = 0;
	for (int i = src.size() - 1; i >= 0; --i) {
		if (src[i] != dst[i + moves]) {
			++moves;
		}
	}
	return moves;
}

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

int countMoves(string src, string dst) {
	int moves = 0;
	for (int i = src.size() - 1; i >= 0; --i) {
		if (src[i] != dst[i + moves]) {
			++moves;
		}
	}
	return moves;
}

- Anonymous February 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static int minimumTurns(String s, String t){
		if(s.length() != t.length())
			return -1;
		
		if(s.equals(t))
			return 0;
		
		int index = t.length()-1;
		while(index >= 0 && s.charAt(index) == t.charAt(index))
			index--;
		
		if(t.charAt(index) == s.charAt(0))
			return index;
		else
			return index + 1;
		
	}

- Tarun February 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Source : abc. Target : acb. The answer is 2.
abc => cab => acb, 2 swaps required.
Your solution give 3

- Nishagrawa March 16, 2019 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Returns correct results for the following test cases:

public static void main(String[] args) {
        System.out.println("expected 2 | moves count " + countMoves("abc", "cba"));
        System.out.println("expected 3 | moves count " + countMoves("abc", "acb"));
        System.out.println("expected 1 | moves count " + countMoves("abc", "bac"));
        System.out.println("expected 0 | moves count " + countMoves("abc", "abc"));
        System.out.println("expected 3 | moves count " + countMoves("abcd", "cbda"));
        System.out.println("expected 2 | moves count " + countMoves("abcd", "cbad"));
    }

public static int countMoves(String src, String dst) {
        int count = 0;
        List<Character> list = new ArrayList<>();
        for (char c : src.toCharArray()) list.add(c);

        boolean bWasSrcModified = false;
        for (int i = dst.length() - 1; i >= 0; i--) {
            char dstChar = dst.charAt(i);
            int listIndex = list.lastIndexOf(dstChar);
            if (listIndex == 0) {
                bWasSrcModified = true;
                continue;
            }
            if (!bWasSrcModified && listIndex == i) continue;
            list.add(0, dstChar);
            bWasSrcModified = true;
            count++;
        }
        return count;
    }

- exdeny February 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Source : abc. Target : acb. The answer is 2.
abc => cab => acb, 2 swaps required.

- Nishagrawa March 16, 2019 | Flag


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