Google Interview Question for SDE1s


Country: United States




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

Only works if Strings contain unique characters.

public static char findInsertedChar(String s1, String s2) {
    int low = 0, high = s1.length() - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;

        char c1 = s1.charAt(mid), c2 = s2.charAt(mid);

        if (c1 == c2) {
            low = mid + 1; // find char on right-hand side
        } else if (mid == 0 || s1.charAt(mid - 1) != c2) {
            return c2;
        } else {
            high = mid - 1; // find char on left-hand side
        }
    }

    return s2.charAt(high + 1);
}

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

Ruby

def solution(s1, s2)
      n = s1.length
      return s2 if n == 0

      lo = 0
      hi = n-1

      while lo <= hi # todo
        mid = lo+((hi-lo)/2)
        c1 = s1[mid]
        c2 = s2[mid]
        if c1 == c2
          lo = mid+1
        else
          if mid == 0 || (s1[mid-1] == s2[mid-1])
            return s2[mid]
          else
            hi = mid
          end
        end
      end
      return s2[-1]
    end

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

public static void main(String[] args) {
		System.out.println(getInsertedCharacter("fhsvbfdbsrgnjnvklnvgafnvjksdfnjvksfvsjbnfdjnvdjfnvksdjfnvkjsfnv", "Zfhsvbfdbsrgnjnvklnvgafnvjksdfnjvksfvsjbnfdjnvdjfnvksdjfnvkjsfnv"));
	}
	public static Character getInsertedCharacter(String s1, String s2){
		if(s2.length()-s1.length() != 1) // Assuming s2 is always larger and not null
			return null;
		int mid = s1.length()/2;
		return getInsertedCharacter(s1, s2, mid);
	}
	public static Character getInsertedCharacter(String s1, String s2, int mid){
		if(s1.length() == 1 && s2.length() == s1.length())
			return s2.charAt(0);
		if(s2.length()==1)
			return s2.charAt(0);
		if(s1.substring(0, mid).equals(s2.substring(0, mid))){
			String temp = s1.substring(mid, s1.length());
			return getInsertedCharacter(temp, s2.substring(mid, s2.length()), temp.length()/2);			
		}else
			return getInsertedCharacter(s1.substring(0, mid), s2.substring(0, mid), mid/2);
	}

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

public static void main(String[] args) {
		System.out.println(getInsertedCharacter("fhsvbfdbsrgnjnvklnvgafnvjksdfnjvksfvsjbnfdjnvdjfnvksdjfnvkjsfnv", "Zfhsvbfdbsrgnjnvklnvgafnvjksdfnjvksfvsjbnfdjnvdjfnvksdjfnvkjsfnv"));
	}
	public static Character getInsertedCharacter(String s1, String s2){
		if(s2.length()-s1.length() != 1) // Assuming s2 is always larger and not null
			return null;
		int mid = s1.length()/2;
		return getInsertedCharacter(s1, s2, mid);
	}
	public static Character getInsertedCharacter(String s1, String s2, int mid){
		if(s1.length() == 1 && s2.length() == s1.length())
			return s2.charAt(0);
		if(s2.length()==1)
			return s2.charAt(0);
		if(s1.substring(0, mid).equals(s2.substring(0, mid))){
			String temp = s1.substring(mid, s1.length());
			return getInsertedCharacter(temp, s2.substring(mid, s2.length()), temp.length()/2);			
		}else
			return getInsertedCharacter(s1.substring(0, mid), s2.substring(0, mid), mid/2);
	}

}

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

This is a simple binary search. Just you have to update the search conditions accordingly.

int insertedString(char *s, char *p, int n)
{
	int low = 0, high = n-1, mid = 0;



	while(low <= high)
	{
		mid = (low+high)/2;
		//printf("%d\n",mid);
		if(p[mid] == s[mid])
		{
			low = mid + 1;
		}
		else if(p[mid] == s[mid-1])
		{
			high = mid - 1;
		}
		else
		{
			return mid;
		}
	}

	return mid;
}

- RandomCoder January 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wouldn't this fail for test case: "abcccc" and "cabcccc"?

- waterloojeongbum February 16, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String findCharacter(String s1, String s2) {
		if (s2.length() == 1) {
			return s2;
		}
		
		int mid = s2.length()/2;
		
		if (s1.substring(0, mid).equals(s2.substring(0, mid))) {
			return findCharacter(s1.substring(mid), s2.substring(mid));
		} 
		
		return findCharacter(s1.substring(0, mid), s2.substring(0, mid));
	}

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

public String findCharacter(String s1, String s2) {
		if (s2.length() == 1) {
			return s2;
		}
		
		int mid = s2.length()/2;
		
		if (s1.substring(0, mid).equals(s2.substring(0, mid))) {
			return findCharacter(s1.substring(mid), s2.substring(mid));
		} 
		
		return findCharacter(s1.substring(0, mid), s2.substring(0, mid));
	}

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

package practice;

public class StringExtraChar
{
    public static void main(String[] args)
    {
        System.out.println(findChar("dgfgfdgdfghghfghdgdfghdfhgdfgf", "dgfgfdgdfghghfghdgdfghdfhgdfxgf"));
    }

    private static String findChar(String s1, String s2)
    {
        if (Math.abs(s2.length() - s1.length()) != 1) {
            return null;
        }
        if (s2.length() == 1 && s1.length() == 0) { //this is our character
            return s2;
        }
        if (s1.length() == 1 && s2.length() == 0) { //this is our character
            return s1;
        }

        if (s1.length() == 1 && s2.length() == 2) {
            final String[] split = s2.split(s1);
            return split[0].isEmpty() ? split[1] : split[0];
        }

        int mid1 = s1.length() / 2;
        String left_s1 = s1.substring(0, mid1);
        String right_s1 = s1.substring(mid1);
        int mid2 = s2.length() / 2;
        String left_s2 = s2.substring(0, mid2);
        String right_s2 = s2.substring(mid2);

        if (left_s1.equals(left_s2)) {
            return findChar(right_s1, right_s2);
        }
        else if (right_s1.equals(right_s2)) {
            return findChar(left_s1, left_s2);
        }
        else {
            return left_s1.length() == left_s2.length() ?
                    findChar(left_s1, left_s2 + right_s2.charAt(0)) :
                    findChar(right_s1.substring(1), right_s2);
        }
    }
}

- Bhavesh April 11, 2019 | 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