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
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Sorting
{
	public static void main (String[] args) throws java.lang.Exception
	{
		System.out.println(sortString("a390testai")); // output: 039aaeistt
	}

	public static String sortString (String str){
		String result = "";
		String[] arr = new String[256];
		for (char c: str.toCharArray()) {
			int index = c - '0';
			arr[index] = c + getNonNullVal(arr[index]);
		}
		for (String s : arr) {
			result += getNonNullVal(s);
		}
		return result;
	}
	
	private static String getNonNullVal(String s){
		return (s == null) ? "" : s;
	}

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

I/P: "a390testai"
O/P: "039aaeistt"

String originalString = "a390testai";
        List<Character> originalArrayList = new ArrayList<>();
        for(char c : originalString.toCharArray())
            originalArrayList.add(c);
        System.out.println(originalArrayList);
        String sortedString = "";
        while (originalArrayList.size() > 0) {
            char minChar = originalArrayList.get(0);
            for (int i=1; i<originalArrayList.size(); i++) {
                if(minChar > originalArrayList.get(i))
                    minChar = originalArrayList.get(i);
            }
            sortedString += minChar;
            originalArrayList.remove((Character) minChar);
        }
        System.out.println(sortedString);

- Younus October 12, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String originalString = "a390testai";
        List<Character> originalArrayList = new ArrayList<>();
        for(char c : originalString.toCharArray())
            originalArrayList.add(c);
        System.out.println(originalArrayList);
        String sortedString = "";
        while (originalArrayList.size() > 0) {
            char minChar = originalArrayList.get(0);
            for (int i=1; i<originalArrayList.size(); i++) {
                if(minChar > originalArrayList.get(i))
                    minChar = originalArrayList.get(i);
            }
            sortedString += minChar;
            originalArrayList.remove((Character) minChar);
        }
        System.out.println(sortedString);

- Younus October 12, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String originalString = "a390testai";
        List<Character> originalArrayList = new ArrayList<>();
        for(char c : originalString.toCharArray())
            originalArrayList.add(c);
        System.out.println(originalArrayList);
        String sortedString = "";
        while (originalArrayList.size() > 0) {
            char minChar = originalArrayList.get(0);
            for (int i=1; i<originalArrayList.size(); i++) {
                if(minChar > originalArrayList.get(i))
                    minChar = originalArrayList.get(i);
            }
            sortedString += minChar;
            originalArrayList.remove((Character) minChar);
        }
        System.out.println(sortedString);

- Younus October 12, 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