Adobe Interview Question for Software Engineer / Developers






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

Let us start by understanding the problem.
Here are a few examples:

( )               --  correct
   ( ( ) )           --  correct
   ( ( ) ) ( )       --  correct
   ( ( ( ) ) ( ) )   --  correct
   ( ) )             --  incorrect (closed paren with no open paren)
   ) (               --  incorrect
   ( ( ) ) ) (       --  incorrect

As you can see, the number of open and closed parens being equal does not guarantee that the parens are matching. Their order is important too.

We take care of this order using a stack.

An algorithm that checks whether the matching in a given text is correct is as follows:

0. Create an empty stack S.
  1. While( there are characters left ){
  2.   Read a character ch. 
  3.   If is ch an opening paren (of any kind), push it onto S
  4.   Else 
  5.      If  ch  is a closing paren (of any kind), look at the top of S.
  6.        If S is empty as this point, report failure. 
  7.        If the top of S is the opening paren that corresponds to c,
            then pop S and continue to 1, this paren matches OK.
  8.        Else report failure.  
  9. If at the end of input the stack S is not empty, return failure.
     Else return success.

- Mayank Jaiswal January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int matchParenthesis (char str[], int len) {
  int i = 0 ;
  stack<char> s ;

  for ( ; i < len ; i ++ ) {
    if ( str[i] == '(' || str[i] == '{' || str[i] == '[' )
      s.push (str[i]) ;
    else {
      switch (str[i]) {
        case ')' : if ( s.top() != '(' )
                     return 0 ;
                   s.pop () ;
                   break ;
        case '}' : if ( s.top() != '{' )
                     return 0 ;
                   s.pop () ;
                   break ;
        case ']' : if ( s.top() != '[' )
                     return 0 ;
                   s.pop () ;
                   break ;
      }
    }
  }
  return (s.empty() ? 1 : 0) ;
}

- Psycho September 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean correctBrackets(String str) {

		int open = 0;

		for (char c : str.toCharArray()) {
			if ('{' == c)
				open++;
			else if ('}' == c && open > 0)
				open--;
			else if ('}' == c && open < 1)
				return false;
		}

		if (open == 0)
			return true;
		return false;
	}

- anfab August 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static void paranthesis() {

String str = "{}{{}}{}}";
int i = 0,count=0;

while(i<str.length()){

char c1 = str.charAt(i++);

if(c1=='{')
count = count +1;
else
count = count -1;

if(count<0){
System.out.println("noo! ");
break;
}
}

if(count>0)
System.out.println("noo ");
else if(count==0)
System.out.println("yes ");

}

- Anonymous August 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or, just use a stack.

- Anonymous August 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

check on }}{{

- ..... August 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above solution is wrong! It returns true even on input "}}{{".
For correct solution, a stack must be used.

- Mayank Jaiswal January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no it doesn't. Mayak can you please explain, why is it wrong for your case?

- elijah.resurrects February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there should be two counters one for { and for }. so that though if the expression is wrong we can compare by single check like
if (count1< count2) invalid expression.

- Manju February 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

bool paranthesis(char str[])
{
int i=0, count = 0;

for(i=0; i < strlen(str); i++)
{
if(str[i] == '}')
    count++;
else if(str[i] == '{')
{
count--;
if(count == -1)
{
printf("Paranthesis don't match")
exit(0);
}
}
}
}

- Rahul October 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for the bool return type, it can be made void.

This code shall print No if the starting paranthesis is '}' in the string.
Example "}}{{"

- Rahul October 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am sorry Rahul but above solution is incorrect because it doesn't take care of the fact that an opening parenthesis should come before its corresponding closing parenthesis.

In parenthesis matching, parenthesis must open before closing just like any valid mathematical expression.
Correct examples:
1. ((()))
2. () () ()
3. (()) ()

Incorrect :
1. ((( ))))
Incorrect because no. of opening parenthesis is not equal to no. of closing parenthesis.
2. )(
Incorrect because closing parenthesis comes before corresponding opening parenthesis.

- Mayank Jaiswal January 17, 2012 | 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