Salesforce Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

Use Stack.
Push into the stack until you a get a closing brace.
On getting a closing brace, pop out an element from the stack.
If (you got anything but a brace continue popping until you get a brace)
check the brace is a matching one else return "wrong string"
continue with the string
else(if you got a brace on first pop) it would be an extra brace
return "wrong string"

private static boolean checkBracesSequence(String string) {
		Stack st = new Stack();
		
		for(char c: string.toCharArray()) {
			if( c != ')' && c != '}' && c!= ']') {
				st.push(c);
			} 
			else {
				int count =0;
				while(!st.isEmpty()) {
					char t = st.pop().toString().charAt(0);
					if(t =='(' || t == '{' || t == '[') {
						if(count == 0) {
							System.out.println("Extra Brackets");
							return false;
						}
						if(c == ')' && t == '(') {
							break;
						} else if(c=='}' && t=='{') {
							break;
						} else if (c==']' && t=='[') {
							break;
						}
					}
					
					count++;
				}
			}
		}
		
	while(!st.isEmpty()) {
		char t = st.pop().toString().charAt(0);
		if(t =='(' || t == '{' || t == '['){
			return false;
		}
	}
	
	return true;
		
	}

- hm January 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a stack:

We loop through the string. If we encounter an open parenthesis, we put it on the stack. If we find a close parenthesis, we remove an open parenthesis of the stack- In this case, if we find that there is no open parenthesis on the stack, we know that we have invalid parentheses.
Likewise, If we find that by the end of the string, we still have open parentheses on the stack, it is invalid.

Some code:

boolean isValid(String s) {

     Stack stack = new Stack();

     for (int i = 0; i < s.length(); i++) {

          if (s[i] == '(') { stack.push("("); }

               else if (s[i] == ')') {

                    if ( stack.size() > 0) { stack.pop();}
                    else { return false; }
               }
     }

     return (stack.size() == 0);
}

- SK January 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interviewer seems to asked for Number of wrong sets in given string .i.e. He doesn't want to check whether parenthesis is balanced or not .

- sonus January 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In that case, then we can simply keep a second stack and add closing brackets if it is unable to pop from the first stack. Then at the end, return the sum of the sizes of both stacks

- Skor January 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsValidParenthesis(char* expression)
{
stack<char> parenthesis;
char* start = expression;
bool consecutive_push_flag = false;
bool consecutive_pop_flag = false;

while (*start != '\0')
{
if (*start == '(')
{
if (consecutive_push_flag == true)
{
parenthesis.push('#');
}
parenthesis.push(*start);
consecutive_push_flag = true;
consecutive_pop_flag = false;
}
else if (*start == ')')
{
if (parenthesis.empty())
{
return false;
}
if (consecutive_pop_flag == true && parenthesis.top() == '#')
{
return false;
}
consecutive_push_flag = false;
consecutive_pop_flag = true;
parenthesis.pop();
}
else
{
consecutive_push_flag = false;
consecutive_pop_flag = false;
}
++start;
}

if (!parenthesis.empty())
{
return false;
}

return true;
}

- Anonymous January 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsValidParenthesis(char* expression)
{
	stack<char> parenthesis;
	char* start = expression;
	bool consecutive_push_flag = false;
	bool consecutive_pop_flag = false;

	while (*start != '\0')
	{
		if (*start == '(')
		{
			if (consecutive_push_flag == true)
			{
				parenthesis.push('#');
			}
			parenthesis.push(*start);
			consecutive_push_flag = true;
			consecutive_pop_flag = false;
		}
		else if (*start == ')')
		{
			if (parenthesis.empty())
			{
				return false;
			}
			if (consecutive_pop_flag == true && parenthesis.top() == '#')
			{
				return false;
			}
			consecutive_push_flag = false;
			consecutive_pop_flag = true;
			parenthesis.pop();
		}
		else
		{
			consecutive_push_flag = false;
			consecutive_pop_flag = false;
		}
		++start;
	}

	if (!parenthesis.empty())
	{
		return false;
	}

	return true;

}

- Anonymous January 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I do not understand why people are using stack to store braces and count. Why dont you just do one iteration of string and get the result without taking any additional data structure.
Let me know if my approach is slower than the one with stack. I have just started solving questions.

var exp = "((Hello))))))))))))))))";

var openBrCt = 0;
var closeBrCt = 0;



for (var i=0; i<exp.length; i++)
{
	if(exp[i]==='(')
		openBrCt++;
  else if(exp[i]===')')
  	closeBrCt++;   
}

if(openBrCt-closeBrCt == 0)
	alert("No error in expression");
else
	alert("Open and Close:" + openBrCt + "  " + closeBrCt)
	alert("There are: " + (parseInt(openBrCt)-parseInt(closeBrCt)) + " wrong braces in expression")

- del.rl.yahoo December 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if the expression is )A( ? Is the code expected to catch this issue?

- Anonymous February 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;


public class Main {
public static void main(String[] args) {
String s = "(((((A))((B))))(()))";
System.out.println("isBalanced: " + isBalanced(s));
}

private static boolean isBalanced(String s) {
Stack<Character> sk = new Stack<Character>();
boolean bNeverHad = true;
for (char c: s.toCharArray()) {
if ((c != ')') && (c != '}') && (c != ']')) {
// ( ( A ( B
if (!bNeverHad && sk.isEmpty()) {
return false;
}
sk.push(c);
bNeverHad = false;
System.out.println("push: " + c);
} else {
// )
char popc;
if (!sk.isEmpty()) {
popc = sk.pop();
System.out.println(" POP: " + popc);
while ((!sk.isEmpty()) &&
(popc != '(') && (popc != '{') && (popc != '[')) {
popc = sk.pop();
System.out.println(" POP: " + popc);
break;
}
} else {
return false;
}
}
} // for
if (sk.size() > 0)
return false;
return true;
}

/* (non-Java-doc)
* @see java.lang.Object#Object()
*/
public Main() {
super();
}

}

- stevelee1111 February 24, 2016 | Flag Reply


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