Google Interview Question
Software Engineer / DevelopersCountry: United States
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.
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;
}
}
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++;
}
}
}
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?...
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++;
}
}
}
- mahdi.oraei February 25, 2014