abc Interview Question for Senior Software Development Engineers


Country: India
Interview Type: In-Person




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

You can use stack and do the following steps
- If the current character is an open parenthesis like “{“ or “(“ or “[“ , push it to the stack
- If the current character is an close parenthesis like “}” or “)” or “]”
o check the latest value in the stack if it is the closer of the latest element on the stack, pop the latest value from the stack
o Otherwise, break from the loop and this string will be invalid.
- Finally, check if there is any element on the stack that is meaning this string is invalid, otherwise, it is valid.

See the below java example

import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
import java.util.Stack;

import static org.junit.Assert.*;
import org.junit.Test;

public class CheckParenthesesInString {
	HashMap<Character, Character> mappedSpecialCharacters = new HashMap<>();
	Set<Character> opens = null; // Open Parenthesis
	Collection<Character> closes = null; // Close Parenthesis
	Stack<Character> stringParenthesis = null;

	public CheckParenthesesInString() {
		mappedSpecialCharacters.put('{', '}');
		mappedSpecialCharacters.put('(', ')');
		mappedSpecialCharacters.put('[', ']');
		opens = mappedSpecialCharacters.keySet();
		closes = mappedSpecialCharacters.values();
	}

	public boolean validate(String input) {
		boolean valid = true;
		stringParenthesis = new Stack<>();
		for (char ch : input.toCharArray()) {
			if (opens.contains(ch)) {
				stringParenthesis.push(ch);
			} else if (closes.contains(ch)) {
				if (!stringParenthesis.isEmpty() && ch == mappedSpecialCharacters.get(stringParenthesis.peek())) {
					stringParenthesis.pop();
				} else {
					valid = false;
					break;
				}
			}
		}
		return valid && stringParenthesis.isEmpty();
	}

	@Test
	public void testParenthesis() {
		assertTrue(validate("{}"));
		assertFalse(validate("[[]"));
		assertTrue(validate("{[()]}[]()"));
		assertFalse(validate("{}[]()("));
	}

}

- Badawy December 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

step 1 : if there is a opening character increment by 1, start with 0, if there is a closing character then decrement that by 1.
step 2: repeat the step no 1 by applying each characters set '()', '{}, or []
step 3: make sure the result should be 0 at the end
step 4 : make sure the result will never be in negative in the process.

- kshitiz baral December 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My thinking would be starting with and empty string and then going through on the input string and keep them concatenating to my empty string variable. Then, at each iteration you would only care for last two characters of this variable! if they are a match pair eliminate them. So for example:

starting initialization:

s = "" // empty string

1st input character {

s = "{"

since we do not have yet 2 characters at least, we continue

2nd input character [

s = "{["

the last two characters do not match, so we do nothing

3rd character (

s = "{[("

the last two characters of s are "[" and "(" they do not match so we do nothing

4th character )

s = "{[()" the last two characters are a matching pair "()" so we remove them se becomes:

s = "{["

next character, and so on

if at the end we get an empty string the the brackets are valid

- Hungary December 19, 2017 | 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