Google Interview Question for Developer Program Engineers


Country: China
Interview Type: Phone Interview




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

#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

string convert(const string &s){
	stringstream o;

  	for(char i : s){
    		if(i < ' ')
      			o << "\\" << setw(3) << setfill('0') << oct << (int)i;
    		else
     			 o << i;
  	}
  	return o.str();
}

int main(int argc, char *argv[]) {
  	cout << convert("A\tB") << endl;
  	return 0;
}

output : A\011B

- tmc March 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use a lookup table for the 32 special ASCII characters and then simply search and replace.


public class OctalEscape {

public static void main(String args[]) {
System.out.println(escape("\000sdjfj k\004ljf lafj \100\040\100 asdfj akl\010jsdfklf"));
}

static String lookup[];
static {
buildLookupTable();
}

static void buildLookupTable() {
lookup = new String['0'];
char octal[] = new char[] {'0','0','0'};
int c = 0;
for (char i = '0'; i <= '7'; ++i) {
octal[0] = i;
for (char j = '0'; j <= '7'; ++j) {
octal[1] = j;
for (char k = '0'; k <= '7'; ++k) {
octal[2] = k;
lookup[c++] = "\\" + octal[0] + octal[1] + octal[2];
if (c == ' ') {
return;
}
}
}
}
}

static String escape(String s) {
StringBuffer result = new StringBuffer();
char sArray[] = s.toCharArray();
for (char c : sArray) {
if (c < ' ') {
result.append(lookup[c]);
}
else {
result.append(c);
}
}
return result.toString();
}
}

- Anonymous March 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Hopefully this formats correctly...

public class OctalEscape {

	public static void main(String args[]) {
		System.out.println(escape("\000sdjfj k\004ljf lafj   \100\040\100 asdfj akl\010jsdfklf"));
	}
	
	static String lookup[];
	static {
		buildLookupTable();
	}
	
	static void buildLookupTable() {
		lookup = new String['0'];
		char octal[] = new char[] {'0','0','0'};
		int c = 0;
		for (char i = '0'; i <= '7'; ++i) {
			octal[0] = i;
			for (char j = '0'; j <= '7'; ++j) {
				octal[1] = j;
				for (char k = '0'; k <= '7'; ++k) {
					octal[2] = k;
					lookup[c++] = "\\" + octal[0] + octal[1] + octal[2];
					if (c == ' ') {
						return;
					}
				}
			}
		}
	}
	
	static String escape(String s) {
		StringBuffer result = new StringBuffer();
		char sArray[] = s.toCharArray();
		for (char c : sArray) {
			if (c < ' ') {
				result.append(lookup[c]);
			}
			else {
				result.append(c);
			}
		}
		return result.toString();
	}
}

- Anonymous March 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution is good. But I think you don't have to build this entire lookup table.
"oct" function in std will anyway do this for you.

- Psycho October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi

Can you please elaborate ?

- aasshishh March 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aasshishh what's your point?? It's really my google interview question. I did't need to cheat.

- ywdong8809 March 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

he's just asking you to clarify the problem. you have problems understanding?

- zyfo2 March 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, basically, nothing should be modified for the original string, since the new char '\ooo' would be the same as the previous char.

- shengzhc March 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

So if I understand you correctly if the input string is: "A#B" output should be "A\043B" as ascii character '#' maps to decimal 35 and octodecimal 43. Right?

- hyp March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes!! And The ascii characters that smaller than space are regarded as special characters.

- ywdong8809 March 09, 2013 | Flag


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