Amazon Interview Question for Software Engineer / Developers


Team: SDE
Country: India
Interview Type: In-Person




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

public static void printBrackets(int left, int right, char[] str, int count)
	{
		if(left == 0 && right == 0)
			System.out.println(str);
		else
		{
			if(left > 0)
			{
				str[count] = '{';
				printBrackets(left -1, right, str, count + 1);
			}
			if(right > left)
			{
				str[count] = '}';
				printBrackets(left, right - 1, str, count + 1);
			}
		}
	}

- hello world February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain the logic? atleast how to call the function initially with correct params

- aqs February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for 2N braces, it can only be organized in two fashions:
(.....)(...) and (........)
In the first case, first ( and first ) meant to be a pair, second ( and second ) DOES NOT have to be a pair. so ()()() should be viewed as () ()()
In the second case, two ( ) is a pair, so ( ()() ) is valid, but not ()()()

In the first case, the first ')' can occur at i = 2, 4, .... 2N-2. For a fixed i, there are
f(i-2)*f(2n-i) cases
and for case two, f(2n-2) cases.
So f(n) = sum_{i=2, 4, ... 2N-2} ( f(i-2)*f(2n-i) ) + f(2n-2)

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

More details at stackoverflow.com/questions/3172179/valid-permutation-of-parenthesis

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

awesome answer.how do you think like that.

- uddeshyakumar.kumar February 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def paren(n):
  if n == 1: return ['()']
  else:
    pnless1 = paren(n-1)
    pn = []
    for i in pnless1:
      pn.append('()'+i)
      pn.append(i+'()')
      pn.append('('+i+')')
    pn = pn[1:] # to avoid the repeated pattern
    return pn

- amshali February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution provided by Hello World is most optimum one. We go on recursivelya dding left and right brackets to form valid strings.

We can add left brackets as long as we have left brackets remaining with us.
We can add right brackets as long as the number of right brackets remaining is > number of left brackets remaining.
When the number of right brackets and left brackets is exhausted, print the string

- maddy March 14, 2012 | 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