Google Interview Question for Java Developers


Country: United States




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

public static int longestUniqueChars(Iterator<Character> chars) {
		if (chars == null) {
			return 0;
		}
		int startUnique = 0;
		int curr = 0;
		int maxUniqueLength = 0;
		int currUniqueLength = 0;
		int[] lastCharIndex = new int[26];
		for (int i = 0; i < 26; i++) {
			lastCharIndex[i] = -1;
		}
		
		while (chars.hasNext()) {
			char ch = chars.next();
			if (lastCharIndex[ch - 'a'] == -1 || lastCharIndex[ch - 'a'] < startUnique) {
				currUniqueLength++;
			} else {
				startUnique = lastCharIndex[ch - 'a'] + 1;
				currUniqueLength = curr - startUnique +1;
			}
			if (currUniqueLength > maxUniqueLength) {
				maxUniqueLength = currUniqueLength;
			}
			lastCharIndex[ch - 'a'] = curr;
			curr++;
		}
		return maxUniqueLength;

}

- ashu1.220791 January 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To get the actual max unique string as is expected here, I am using below method:

public static String longestUniqueChars(Iterator<Character> chars) {
		if (chars == null) {
			return null;
		}
		int startUnique = 0;
		int curr = 0;
		int maxUniqueLength = 0;
		int currUniqueLength = 0;
		String uniqueString = "";
		String maxUniqueString = null;
		int[] lastCharIndex = new int[26];
		for (int i = 0; i < 26; i++) {
			lastCharIndex[i] = -1;
		}

		while (chars.hasNext()) {
			char ch = chars.next();
			uniqueString += ch;
			if (lastCharIndex[ch - 'a'] == -1 || lastCharIndex[ch - 'a'] < startUnique) {
				currUniqueLength++;
			} else {
				startUnique = lastCharIndex[ch - 'a'] + 1;
				currUniqueLength = curr - startUnique + 1;
				int indexOfRepChar = uniqueString.indexOf(ch);
				uniqueString = uniqueString.substring(indexOfRepChar + 1, uniqueString.length());
			}
			if (currUniqueLength > maxUniqueLength) {
				maxUniqueLength = currUniqueLength;
				maxUniqueString = uniqueString;
			}
			lastCharIndex[ch - 'a'] = curr;
			curr++;
		}
		return maxUniqueString;

}

Tested with following string "abcacdefghijkklabcdefghijklmnoabc" and it gave expected answer which is "abcdefghijklmno"

- ashu1.220791 January 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a c++ code:

int longestSub(string& s)
{
	int index[26], len = 0, max=0;
	for (int i = 0; i < 26; i++)
		index[i]=-1;
	for (int i = 0; i < s.size(); i++)
	{
		int id = tolower(s[i]) - 'a';
		if (index[id] != -1)
		{
			//reset
			if (max < len)
				max = len;
			len = i - index[id];
			index[id] = i;
		}
		else
		{
			index[id] = i;
			len++;
		}
	}
	if (max < len)
		max = len;
	return max;
}

- Aman April 15, 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