Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

How about this:
1) Convert the expression to post fix notation
2) Convert the post fix notation back to infix

- Varun April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

but postfix may not include '(' and ')' how can you add this during converting postfix to infix?

- Anonymous April 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the priority of operators is "++" > "* /" > "+ -" > "( or ) or boundary of the string"
So for expression like "opA(...op1...opN...)opB", if the priority of Min{op1....opN} is >= Max{opA, opB}, then the () can be removed.
Firstly, remove redundant brackets like ((....))
Secondly, process the () from outter to inner (when processing the outter "()" ingore the operators inside the inner "()")

- leonard2chelsea April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Which team asked this ?

- Anon April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Little cowboy coding but it should work. The idea is to go recursive and solve each bracket seprately. Follow the BODMAS rule and set priorities (except 'B' has a -ve priority because w want to remove that.

expression = "(a+(b*c))*(d*(f*j))"
operations = {'*':2,'+':1,'(':-1,')':-1}
operators = ['*','+']
bracket_stack = []
brackets = ['(',')']

def remove_brackets(expr,start_location,total_length,left_operator,bracket_stack):
    i = start_location+1
    is_bracket_removable = True

    while expr[i] != ')':
        element = expr[i]
        if element == '(':
            i = remove_brackets(expr,i,total_length,operations[expr[i-1]],bracket_stack)
        else:
            if element in operators and is_bracket_removable:
                if left_operator > operations[element]:
                    is_bracket_removable = False
        i+=1

    if is_bracket_removable and i+1 < total_length and operations[expr[i+1]] > left_operator:
        is_bracket_removable = False

    if is_bracket_removable:
        expr[start_location] = ''
        expr[i] = ''
    return i

i=0
expr = list(expression)
while i<len(expr):
    c = expr[i]
    if c == '(':
        i = remove_brackets(expr,i,len(expr),-1 if i==0 else operations[expr[i-1]],bracket_stack)
    i+=1

print ''.join([c for c in expr if c!=''])

- antonydeepak April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If an expression that has brackets results in the same postfix expression as the one without any brackets. Then all the brackets can be removed.

Algorithm - assuming that brackets are legally placed
1) Compute the postfix expression of the given expression (say STR) with brackets, lets call this postFix(STR) = orgPost
2) For each pair of bracket with STR
a) Remove the pair of bracket and compute STR'
b) if postFix(STR')=orgPost then STR=STR'
3) Return STR

- IntwPrep November 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Keep on pushing the operators into a stack and keep a track of the braces corresponding to the two pushed operators. If the operators are of unequal priority, let the braces remain, otherwise, remove them. Please correct me if I am wrong.

- alex April 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

First create a binary tree for given expression. Then depending upon priority of operator create expression string again.

- Hemant May 03, 2013 | Flag


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