Google Interview Question

Merging intervals Merge lists/arrays Implement read() using read4k() 1 round of distributed system and large scale system design Compress string "aabcaaaaade" to "aabc5xade". Only compress if it shortens the string. 5xa means char a is repeated 5 times. Question on 2 lists and their union/ intersection/ remove elements present in L1 which are also present in L2

Interview Answers

Anonymous

Jun 21, 2016

// helper method void append(StringBuilder sb, char cur, int count) { if (count > 3) { sb.append(count).append('x').append(cur); } else { for (int j = 0; j < count; j++) { sb.append(cur); } } } String compress(String s) { char cur = s.charAt(0); int count = 1; StringBuilder sb = new StringBuilder(); for (int i = 1; i < s.length(); i++) { char c = s.charAt(i); if (c == cur) { count++; } else { append(sb, cur, count); cur = c; count = 1; } } append(sb, cur, count); return sb.toString(); }

Anonymous

Oct 13, 2015

public static String compressString(String input) { if(input == null) throw new NullPointerException("Null string Passed"); if(input.length() 2) { String temp = String.valueOf(counter+1) + 'x' + input.charAt(c); input = input.substring(0,c) + temp + input.substring(n,input.length()); c = c + 3; } else { c=n; } } return input; }