Directi Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
Covers all the cases I can think of. Some of the cases that I thought about, that you haven't mentioned in your example are below. This might help someone test their code.
[ ] ( ) { }
{ [ ( ) ] }
Incorrect ones that need a change:
{ ( ) ] [ }
"You can edit any bracket and change it to remaining types for accomplishing the task."
I don't get what you mean by that part. But if we were to ignore that part...make a stack, and every time a brace is opened, push it onto the stack. Every time a brace is closed, pop off the topmost element in the stack and see if the popped element is of the same type of brace as the closing brace. If yes, keep going; if no, return false. If we are at the end of the string, return true iff the stack is empty (all braces that were opened are now closed).
public static boolean isValid(String symbol){
LinkedList<Character> stack=new LinkedList<Character>();
char [] ch=symbol.toCharArray();
for (char c:ch){
if (stack.size()!=0){
char tmp=stack.getFirst();
if (tmp=='{'&&c=='}'){
stack.pop();
} else if (tmp=='['&&c==']'){
stack.pop();
} else if (tmp=='('&&c==')'){
stack.pop();
}else{
stack.push(c);
}
}else{
stack.push(c);
}
}
return stack.size()==0?true:false;
}
int fun(char exp[MAX])
{
int flag=1,i=0;
while(exp[i])
{
if(exp[i]=='{' || exp[i]=='(' || exp[i]=='[')
push(exp[i]);
else if(exp[i]=='}' || exp[i]==')' || exp[i]==']')
{
char c=pop();
if(exp[i]=='}' && c!='{')
{
flag=0;
break;
}
else if(exp[i]==']' && c!='[')
{
flag=0;
break;
}
else if(exp[i]==')' && c!='(')
{
flag=0;
break;
}
}
else
{
printf("\nInvaid characters entered!!!! Please enter only {}[]()\n\n");
return -2; //implies invalid expression
}
i++;
}
if(flag==0 || !isemptystack())
return -1; //implies invalid expression
else
return 1; //implies valid expression
}
import java.util.Scanner;
import java.util.Stack;
public class Bracket {
public static void main(String args[]){
Scanner scan= new Scanner(System.in);
String str=scan.next();
String open="{(<[";
String close="})>]";
Stack <Character> stack=new Stack<Character>();
String nstr="";
for(int i=0;i<str.length();i++){
Character vali=str.charAt(i);
if(open.contains(Character.toString(vali))){
nstr=nstr+str.charAt(i);
stack.push(str.charAt(i));
}
else if(close.contains(Character.toString(vali))){
Character temp = null;
if(!stack.isEmpty())
temp=stack.pop();
if(temp!=open.charAt(close.indexOf(vali))){
nstr=nstr+close.charAt(open.indexOf(temp));
}
else{
nstr=nstr+vali;
}
}
}
System.out.print(nstr);
}
}
The question seems oversimplified. If I am only tasked with making sure brackets are nested properly, and I can change any of the brackets to whatever I please, since the string is always an even number of characters, I can solve this by making the first half ( and the second half )
My reading of the problem is to change as little as possible of the incoming string such as to make it valid.
- Rusty May 03, 2012Define functions: boolean isMatch(char p1, char p2), boolean leftParen (char p), and char matchParen(char p)
As suggested, push all left parens on stack, and when encountering a right paren, if it is not a match to stack-top, modify the string, else leave alone, pop stack and move on to next element in string. At any time if the number of elements on the stack is equal to the number of elements still remaining to be processed on string, modify all remaining elements on string as necessary. This clause is to take care of strings of the form "((((", "(({[" etc. This aspect is triggered after the first two elements are pushed on stack. If stack is empty and a right paren is encountered, arbitrarily modify it to a left paren and insert it on stack and proceed as before.