Google Interview Question for Software Engineer / Developers


Country: United States




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

public class Test {
		//add this line
		public static int count = 0;
		public static void main(String [] args){
			printParenthesis(3);
		}
		public static void printParenthesis(int n){
			char buffer[] = new char[n*2];
			printP(buffer,0,n,0,0);
		}
		public static void printP(char buffer[], int index, int n, int open, int close){
			if(close == n){
				// change System.out.println(new String(buffer)); to the following line
				System.out.println(++count+"." + new String(buffer));
			}else{
				if(open > close){
					buffer[index] = ']';
					printP(buffer, index+1, n, open, close+1);
				}
				if(open < n ){
					buffer[index] = '[';
					printP(buffer,index+1,n,open+1,close);
				}
			}
		}
	}

- mahdi.oraei February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Bingo.

- Pete F. February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Now execute printParenthesis(3); two, three and more times

- qwe February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

qwe, it doesn't explain what the behavior should be if printParenthesis is called more than once. Maybe they want it to reset the count, maybe not. That's not part of the problem.

- Pete F. February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yes, I think the interviewer wanted to test static variable. If we can change the signature of the method, we can introduce an integer variable to keep track of the recursion level. Reset the count at the end if the level is 0.

public static void printP(char buffer[], int index, int n, int open, int close, int level) {
		if (close == n) {
			System.out.print(++count);
			System.out.println(new String(buffer));
		} else {
			if (open > close) {
				buffer[index] = ']';
				printP(buffer, index + 1, n, open, close + 1, level+1);
			}
			if (open < n) {
				buffer[index] = '[';
				printP(buffer, index + 1, n, open + 1, close, level+1);
			}
		}
		//reset the count
		if(level == 0) {
			count = 0;
		}
	}

- Kyle.L February 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If two people test this code running at the same time running on the same JVM there might be interference with the static variable (not thread safe). We can create a new instance of the line counter every time the printParenthesis method is being executed.

public class Test {
	public static void main(String [] args){
		printParenthesis(3);
	}
	public static void printParenthesis(int n){
		char buffer[] = new char[n*2];
		Line line = new Test().new Line();   
		printP(buffer,0,n,0,0, line);
	}
	public static void printP(char buffer[], int index, int n, int open, int close, Line line){
		if(close == n){
			// change System.out.println(new String(buffer)); to the following line
			System.out.println(line.next() + ". " + new String(buffer));
		}else{
			if(open > close){
				buffer[index] = ']';
				printP(buffer, index+1, n, open, close+1, line);
			}
			if(open < n ){
				buffer[index] = '[';
				printP(buffer,index+1,n,open+1,close, line);
			}
		}
	}
	
	class Line {
		private int count = 1;
		
		private int next() {
			return count++;
		}
	}

}

- Guy March 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

How about using a static int variable to count everytime that "count == n" and then print the number before you print buffer.

- Ehsan February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printParenthesis(int n){
		char buffer[] = new char[n*2 + 2];
                buffer[0] = 1;
                buffer[1] = '.';
		printP(buffer,2,n,0,0);
	}
..............
		if(close == n){
			System.out.println((int)buffer[0]++ + new String(buffer, 1, buffer.length - 1));
		}

- aaa February 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is someone trolling me? you're supposed to just add a syso.println() at the end of he printParanthesis method. Or am I missing something here?...

- Andrada February 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah, I'm totally stupid and rush to conclusions

- Andrada February 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If two people test this code running at the same time running on the same JVM there might be interference with the static variable (not thread safe). We can create a new instance of the line counter every time the printParenthesis method is being executed.

public class Test {
	public static void main(String [] args){
		printParenthesis(3);
	}
	public static void printParenthesis(int n){
		char buffer[] = new char[n*2];
		Line line = new Test().new Line();   
		printP(buffer,0,n,0,0, line);
	}
	public static void printP(char buffer[], int index, int n, int open, int close, Line line){
		if(close == n){
			// change System.out.println(new String(buffer)); to the following line
			System.out.println(line.next() + ". " + new String(buffer));
		}else{
			if(open > close){
				buffer[index] = ']';
				printP(buffer, index+1, n, open, close+1, line);
			}
			if(open < n ){
				buffer[index] = '[';
				printP(buffer,index+1,n,open+1,close, line);
			}
		}
	}
	
	class Line {
		private int count = 1;
		
		private int next() {
			return count++;
		}
	}

}

- Guy March 06, 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