Microsoft Interview Question for Software Engineer / Developers






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

pseudoCode :

void generate(string p,int o,int c ,int n)
{
if( o==n && c==n )
{
PRINT(p);
return ;
}

if(c>o)return;

if( o >=c && o<n )
generate(p+'{' ,o+1,c,n );

if( c<o )
generate( p+ '}' ,o,c+1,n );

}

- JD January 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops, this seems much more elegent than mine...
good job!

- Bingsuns January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@JD: very cool!!!

- Anonymous January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@JD: What do 'o' and 'c' mean?

- Anonymous January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think 'o' means open parenthesis and 'c' means close parenthesis

- Anonymous January 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great solution!

- russoue February 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if i call the function with generate( "", 0,0, 2 ), the output contains only {{}}. Am I missing something?

- Joe February 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops, I missed the second recursive. The function is fine.

- Joe February 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

nice solution.

- spsneo January 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone explain the solution pls

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

nice one

- deaGle January 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

U can look into the catalan number approach. learn to generate catalan numbers through C.

- nikhil January 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Catalan gives the number of patterns of balanced parentheses. But how can you generate the pattern itself with the catalan number. Please explain.

- spsneo January 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Nikhil:

Can you explain how to generate using catalan numbers ???

- Sumanth Lingala February 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <assert.h>
#include <algorithm>

using namespace std;

//Q:
//Write a function to generate all possible n pairs of balanced parentheses. For example, if n=1
//{}
//for n=2
//{}{}
//{{}}

void pairs(int);
void pairs(int, int, int, string); //aux function for recursive

int main(int argc, char* argv[])
{
int n = 0;
while(n<=0)
cin>>n;

pairs(n);
return 0;
}

void pairs(int n)
{
pairs(n, n, 0, "");
}

void pairs(int left, int right, int diff, string s)
{
if(left == 0 && right == 0)
{
cout<<s<<endl;
return;
}

if(diff == 0)
{
pairs(left-1, right, diff+1, s+'{');
return;
}
else
{
if(left>0)
pairs(left-1, right, diff+1, s+'{');
pairs(left, right-1, diff-1, s+'}');
}
}

- Bingsuns January 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Same idea with JD but refine in comparision

void createParentheses(int pos, int length, int open, int close){
		if (close == length){ 
			cout << str << endl;
			return;
		}
		else{
			if (open > close){
				str[pos] = '}';
				createParentheses(pos+1,length,open,close+1);
			}
			if (open < length){
				str[pos] = '{';
				createParentheses(pos+1,length,open+1,close);
			}
		}
	}

Please note that open always >= close

- Silence January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another recursive approach could be in this way:
F(n) = F(i) + F(n - i - 1), for all the i = 1..., n -1

- Anonymous January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
Any one think about the complexity? These recursive approaches will call the same function multiple times, just link using recursion to solve Fibonacci. like will call generate("", 3, 3, 3) generate("{", 2, 3, 3) generate("{{", 1, 3, 3) generate("{{{", 0, 3, 3) .... **generate("{{}", 1, 2, 3) .... generate("{}", 2, 2, 3) **generate("{}{", 1, 2, 3) .... and if we could save the result of generate("", 1, 2, 3), then just append them to "{{}" and "{}{", the algorithm will run much faster. - Fan January 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's some C# code, based on JD, but simplified. (I believe some cases were unnecessary).

// Call with generate ( "", 0, 0, n );
  static void generate ( string p, int o, int c, int n ) {
    if ( o==n && c==n ) {
      Console.Write ( p ) ;
      return;
    }

    if ( o < n ) {
      generate ( p + '{', o+1, c, n ) ;
    }

    if ( c < o ) {
      generate ( p + '}', o, c+1, n ) ;
    }

    Console.WriteLine (  ) ;
  }

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

Here's some C# code, based on JD, but simplified. (I believe some cases were unnecessary).

// Call with generate ( "", 0, 0, n );
  static void generate ( string p, int o, int c, int n ) {
    if ( o==n && c==n ) {
      Console.Write ( p ) ;
      return;
    }

    if ( o < n ) {
      generate ( p + '{', o+1, c, n ) ;
    }

    if ( c < o ) {
      generate ( p + '}', o, c+1, n ) ;
    }

    Console.WriteLine (  ) ;
  }

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

wat is the complexity of jd's soln

- spandan February 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
{
int n = 5;
breaknext("", n);
Console.ReadLine();
}

static void breaknext(string pre, int n)
{
for (int head = 1; head <= n; head++) {
int next = n - head;
if (next > 1) {
breaknext(pre + deep(head), next);
} else if (next == 0) {
Console.WriteLine(pre + deep(head));
} else if (next == 1) {
Console.WriteLine(pre + deep(head) + deep(next));
}
}
}
static string deep(int deep)
{
string result = "";
for (int i = 0; i < deep; i++) {
result = "{" + result + "}";
}
return result;
}

- masa March 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code in C#:

private void GenerateBrackets(int openCount, int closeCount, string str, HashSet<string> bracketList)
        {
            if ((openCount == 0) && (closeCount == 0))
            {
                bracketList.Add(str);
            }

            if ((openCount <= closeCount) && (openCount > 0))
            {
                GenerateBrackets(openCount - 1, closeCount, str + "(", bracketList);
            }
            
            if (closeCount > 0)
            {
                GenerateBrackets(openCount, closeCount - 1, str + ")", bracketList);
            }
        }

        /// <summary>
        /// Given number of brackets, find all permutations of open / close sets
        /// e.g. n = 4, ()()()(), (())(()), ((()))(), (((())))),....
        /// </summary>
        [TestMethod]
        public void BracketCombinations()
        {
            int n = 4;
            HashSet<string> bracketList = new HashSet<string>();
            GenerateBrackets(n, n, string.Empty, bracketList);
        }

- Ashish Kaila March 11, 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