Google Interview Question for Software Engineer Interns


Country: United States




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

package google.g5094190039957504;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.plaf.SliderUI;

public class Main {

	private static char spliteChar='a';
	private static char escapeChar='b';
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		List<String> testList = new ArrayList<String>();

		testList.add("abcd");
		testList.add("abcd");
		testList.add("aaabbbbaaaaffbbb");
		testList.add("1234");

		List<String> resultList = decode(encode(testList));

		boolean corrent = true;
		if (testList == null && resultList == null) {
			System.out.println("funtions are not used");
		} 
		else if(testList ==null || resultList==null){
			System.out.println("somethng wrong");
		}
		else if (resultList.size() != testList.size()) {
			System.out.println("wrong list size");
			corrent = false;
		} else {
			for (Iterator<String> t1 = testList.iterator(), t2 = resultList
					.iterator(); t1.hasNext() && t2.hasNext();) {
				String s1 = t1.next();
				String s2 = t2.next();

				if (!s1.equals(s2)) {
					System.out.println("wrong content:" + s1 + " " + s2);
					corrent = false;
				}
			}
		}

		if (corrent) {
			System.out.println("done");
		} else {
			System.out.println("code wrong");
		}

	}

	private static List<String> decode(String input) throws Exception {
		if(input==null){
			return null;
		}
		StringBuilder buff=new StringBuilder();
		 List<String> re=new ArrayList<>();
		char [] all=input.toCharArray();
		for(int i1=0;i1<all.length;i1++){
			char c=all[i1];
			if(c ==spliteChar){
				//end a string
				re.add(buff.toString());
				buff.setLength(0);
				
			}
			else if(c== escapeChar){
				i1++;
				if(i1==all.length){
					//error, wrong format
					throw new Exception("wrong format");
				}
				c=all[i1];
				if(c == escapeChar ||c==spliteChar){
					buff.append(c);
				}
				else{
					throw new Exception("wrong format");
				}
			}
			else{
				buff.append(all[i1]);
			}
		}
		return re;
	}

	private static String encode(List<String> testList) {
		
		if(testList ==null){
			return null;
		}
		StringBuilder re=new StringBuilder();
		for(String s:testList){
			
			char  [] all=s.toCharArray();
			
			for(int i1=0;i1<all.length;i1++){
				char c=all[i1];
				
				if(c==spliteChar || c==escapeChar){
					re.append(escapeChar);
					re.append(c);
				}
				else {
					re.append(c);
					
				}
			}
			re.append(spliteChar);
		}
		
		return re.toString();
	}

}

- MT October 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def encode(strings):
    result = ''
    count = str(len(strings))
    result += count + '|'

    for s in strings:
        result += str(len(s)) + '|' + s

    return result



def decode(s):
    index = s.find('|')
    count = int(s[:index])
    result = []

    s = s[index + 1:]

    for i in range(count):
        index = s.find('|')
        count = int(s[:index])
        s = s[index + 1:]
        result.append(s[:count])
        s = s[count:]

    return result

- glebstepanov1992 October 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code won't work if the string itself contains the character '|'

- Interested geeky November 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def encode(strings):
    result = ''
    count = str(len(strings))
    result += count + '|'

    for s in strings:
        result += str(len(s)) + '|' + s

    return result



def decode(s):
    index = s.find('|')
    count = int(s[:index])
    result = []

    s = s[index + 1:]

    for i in range(count):
        index = s.find('|')
        count = int(s[:index])
        s = s[index + 1:]
        result.append(s[:count])
        s = s[count:]

    return result

- glebstepanov1992 October 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in C#

string encode(List<string> input)
{
// the string will be like "{ word1, word 2, word3, word4, word5 }"
string s = input.ToLogString();
//remove "{" and "}"
s = s.Substring(1, s.Length - 2);
return s.Trim();
}

List<string> decode(string input)
{
//input="word1, word 2, word3, word4, word5"
List<string> list=new List<string>();
if (string.IsNullOrEmpty(input))
return list;

int i = input.IndexOf(",");
while (i > 0)
{
string temp=input.Substring(0,i);
list.Add(temp.Trim());
input = input.Substring(i+1);
i = input.IndexOf(",");
}
list.Add(input.Trim());

return list;
}

- Anonymous October 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def encode(l):
    for i in xrange(len(l)): l[i] = str(l[i])
    return ",".join(str(len(i)) for i in l) + "|" + "".join(l)

def decode(s):
    lens, s = s.split('|', 1)
    lens = [int(i) for i in lens.split(',')]
    l = []
    offset = 0
    for i in lens:
        l.append(s[offset:offset+i])
        offset += i
    return  l

- Anonymous November 09, 2014 | 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