Directi Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

My reading of the problem is to change as little as possible of the incoming string such as to make it valid.
Define 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.

- Rusty May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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:
{ ( ) ] [ }

- GuruK May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"My reading of the problem is to change as little as possible of the incoming string such as to make it valid."
_
That seems like a reasonable interpretation. It's a more interesting problem that way.

- eugene.yarovoi May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

"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).

- eugene.yarovoi May 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just valid or invalid?

- milo May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Scott May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
}

- Anonymous May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo:
for each character in string
if its a left part i.e. (,{,[ Push in stack
else if its a right part pop from stack and match whether is appropriate left part of parenthesis.
any case doesn't match return FALSE.

return TRUE.

- Punit Jain May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think the 2nd comment on my blog seems to be correct solution of this problem ., please comment there if you think anything wrong there ?

shashank7s dot blogspot dot in/2012/05/you-are-given-string-of-n-characters dot html

PS: Replace dot with . and remove spaces.

- WgpShashank May 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is this valid string : {[}] ??

- time June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
	}
}

- Anonymous October 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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 )

- deltree May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@deltfree: That's not always possilble.
Say suppose you have the string ({()()[]{}}).
Divide and conquer isn't the path you would choose here.
The stack implementation is always the best approach to such problems.

- Akshar May 04, 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