Google Interview Question for Backend Developers


Country: India




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

// Use bucket sort 
//Time complexity: O(N+K)
//Space: O(K)

class Sorting
{
	public static void main (String[] args) throws java.lang.Exception
	{
		System.out.println(sorting("a390testai")); // output: 039aaeistt
	}
	
	public static String sorting(String str) {
		if (str == null || str.length() == 0)
			return str;
		
		ArrayList<Character>[] al = new ArrayList[80]; 	
		for (char c: str.toCharArray()) {
			if (c - '0' >= 0) {
				if (al[c - '0'] == null)
					al[c - '0'] = new ArrayList<Character>();
				al[c - '0'].add(c);	
			}
		}
		
		StringBuilder sb = new StringBuilder();
		for (List<Character> list: al) {
			if (list == null) continue;
			for (char c: list) {
				sb.append(c);
			}
		}
		
		return sb.toString();
	}
}

- sj April 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain the logic?

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

You could implement any sort also (merge or quicksort). Make sure the comparison is done 
on the ascii values of these characters. This will give then sorted string.

- getPDat April 10, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static String sortString(String s){
String result="";
int[] asciiArr= new int[256];
for(int i=0; i<s.length();i++){
asciiArr[s.charAt(i)]+=1;

}
for(int i=0; i<256; i++){
if(asciiArr[i]>0){
for(int j=0; j<asciiArr[i]; j++){
result+=(char)i;

}
}
}

return result;
}

- Sri Lakshmi Gurram April 13, 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