Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Traverse the string of parenthesis gives. At any point no of open brackets >= no of closed brackets

- learner October 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use stack.. If you get open bracket push else pop. At the end of expression the stack should be empty.

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

Using stack you would need extra memory.

using a counter (initial value 0). we can do it just by parsing the string from left, increment counter if it is a open bracket '[' and decrement if it is a closed bracket ']'. At any point if counter is negative return false.
after traversing the string if counter is positive return false. otherwise return true.

int isValidParenthesis(char *str, int len) {
int count = 0, i;
for(i = 0; i < len; i++) {
if (str[i] == '[') {
count++;
}
else if (str[i] == ']') {
count--;
}
if (count < 0) {
return FALSE;
}
}
if (count > 0) {
return FALSE;
}
return TRUE;
}

- nagaraju October 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean validateparanthesis(String expression){
    StringBuilder sb = new StringBuilder();
    char[] characters = expression.toCharArray();
    for(int i = 0; i< characters.length; i++){
        if(characters[i] == '('){
           stack.push(characters[i]+"");
        }else if(characters[i] == ')'){
          sb.append(characters[i]);
            if(stack.empty()){
                return false;
            } else
                sb.append(stack.pop());
        }

    }
     String result = sb.toString();
        if ((result.length()%2) == 0) {
            return true;
        }else
            return false;

    }

- JoshMachine October 27, 2011 | 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