Interview Question

Interview San Jose, CA

given a string,which is filled with either "(" or ")", for

  example "(())()())", write a function to check if the string is "proper". A "proper" string means if there is a "(" in the string, there should be a ")" which corresponds to the "(" in the string. For example, "))()("
c++ programming

Interview Answer

3 Answers


two sub tasks. One is to check if the number of "(" and ")" in the entire string are equal. The other task is to check the number of "(" is always no less than the number of ")" when traversal the string from the head of string to the end.

Interview Candidate on Dec 16, 2010

you can traverse through the string in a loop and increment a counter on occurrence of '(' and decrement on occurrence of ')' .Return false if counter becomes negative anytime in the loop.

noty on Mar 28, 2011

Just a single stack, pop for ')' and push for '('. if isEmpty() when you encounter ')' means string is not proper

Anonymous on Dec 24, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.