Two Sigma Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

__author__ = 'deepika'


"""
this problem is the simpler verison of beautification of text.
There is one more version where the output for "abcdefghi"
for intervals = [ [ 0, 4, 'b'], [ 2, 6, 'i'] ]
will be         <b>ab<i>cd</i></b><i>ef</i>ghi
instead of this <b>ab<i>cd</b>ef</i>ghi

Step 1: close what all tags you can and specific i. because for interval (2, 4) the closing tag should come before string[4]
Step 2: Open all the tags opening at 'i'
Step 3: Till you don't find next interval just add to string.

"""
class Solution:

    def _fetchOpeningTag(self, character):
        return "<" + character + ">"

    def _fetchClosingTag(self, character):
        return "</" + character + ">"

    def beautify(self, string, intervals):

        i = 0
        j = 0
        result = []
        stack = []

        while i < len(string):
            print ("result so far : ", ''.join(result), " with stack : ", stack)

            while len(stack) > 0 and i == stack[0][0]:
                popped = stack.pop(0)
                result.append(self._fetchClosingTag( popped[1] )) # put the closing tag

            while j < len(intervals) and i == intervals[j][0]:
                result.append(self._fetchOpeningTag(intervals[j][2]))
                stack.append( [intervals[j][1], intervals[j][2]] )
                j += 1

            if (j < len(intervals) and i < intervals[j][0]) or  ( j >= len(intervals)):
                result.append(string[i])
                i += 1

        while len(stack) > 0: #there might be left over tokens.
            popped = stack.pop()
            result.append(self._fetchClosingTag( popped[1] )) # put the closing tag

        return ''.join(result)



s=Solution()
intervals = [
    [ 0, 4, 'b'],
    [ 2, 6, 'i'],
]
#print(s.beautify("abcdefghi", intervals))
print(s.beautify("abc", [ [0, 1, 'b'], [1, 2, 'b'], [2, 3, 'b']]))

- Deepika August 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def html_validation(text,format):
bold_active = False #keeps track of bold tag
italic_active = False #keeps track of italics tag
b_idx = 0
i_idx = 0
output_str = ''
for i in range(len(text)):
if (format['bold'][b_idx][0] == i or format['bold'][b_idx][1] == i) and (format['italics'][i_idx][0] == i or format['italics'][i_idx][1] == i):
if bold_active == False and italic_active == False:
output_str += '<b><i>' + text[i]
bold_active = True
italic_active = True

elif bold_active == True and italic_active == True:
output_str += '</i></b>' + text[i]
bold_active = False
italic_active = False
if len(format['bold'])>b_idx+1:
b_idx += 1
if len(format['italics'])>i_idx+1:
i_idx += 1

elif bold_active == True and italic_active == False:
output_str += '</b><i>' + text[i]
bold_active = False
italic_active = True
if len(format['bold'])>b_idx+1:
b_idx += 1

elif bold_active == False and italic_active == True:
output_str += '</i><b>' + text[i]
bold_active = True
italic_active = False
if len(format['italics'])>i_idx+1:
i_idx += 1


elif format['bold'][b_idx][0] == i or format['bold'][b_idx][1] == i:
if bold_active == False:
output_str += '<b>' + text[i]
bold_active = True

elif bold_active == True:
output_str += '</i></b><i>' + text[i] if italic_active == True else '</b>' + text[i]
bold_active == False
if len(format['bold'])>b_idx+1:
b_idx += 1

elif format['italics'][i_idx][0] == i or format['italics'][i_idx][1] == i:
if italic_active == False:
output_str += '<i>' + text[i]
italic_active = True

elif italic_active == True:
output_str += '</i>' + text[i]
italic_active = False
if len(format['italics'])>i_idx+1:
i_idx += 1

else:
output_str += text[i]

return output_str

text = 'ABCDEFGHIJ'
format = {
'bold': [(0,4)],
'italics': [(2,6)]
}
print(html_validation(text,format))

# Any confusion or doubt, then comment

- AjayVardhanReddy August 28, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def html_validation(text,format):
    bold_active = False #keeps track of bold tag
    italic_active = False   #keeps track of italic tag
    b_idx = 0
    i_idx = 0
    output_str = ''
    for i in range(len(text)):
        if (format['bold'][b_idx][0] == i or format['bold'][b_idx][1] == i) and (format['italics'][i_idx][0] == i or format['italics'][i_idx][1] == i):
            if bold_active == False and italic_active == False:
                output_str += '<b><i>' + text[i]
                bold_active = True
                italic_active = True

            elif bold_active == True and italic_active == True:
                output_str += '</i></b>' + text[i]
                bold_active = False
                italic_active = False
                if len(format['bold'])>b_idx+1: 
                    b_idx += 1
                if len(format['italics'])>i_idx+1: 
                    i_idx += 1

            elif bold_active == True and italic_active == False:
                output_str += '</b><i>' + text[i]
                bold_active = False
                italic_active = True
                if len(format['bold'])>b_idx+1: 
                    b_idx += 1

            elif bold_active == False and italic_active == True:
                output_str += '</i><b>' + text[i]
                bold_active = True
                italic_active = False
                if len(format['italics'])>i_idx+1: 
                    i_idx += 1


        elif format['bold'][b_idx][0] == i or format['bold'][b_idx][1] == i:
            if bold_active == False:
                output_str += '<b>' + text[i]
                bold_active = True

            elif bold_active == True:
                output_str += '</i></b><i>' + text[i] if italic_active == True else '</b>' + text[i]
                bold_active == False
                if len(format['bold'])>b_idx+1: 
                    b_idx += 1

        elif format['italics'][i_idx][0] == i or format['italics'][i_idx][1] == i:
            if italic_active == False:
                output_str += '<i>' + text[i]
                italic_active = True

            elif italic_active == True:
                output_str += '</i>' + text[i]
                italic_active =  False
                if len(format['italics'])>i_idx+1: 
                    i_idx += 1

        else:
            output_str += text[i]
            
    return output_str

text = 'ABCDEFGHIJ'
format = {
'bold': [(0,4)],
'italics': [(2,6)]
}
print(html_validation(text,format))

- AjayVardhanReddy August 28, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Nice job! Do you think this could also be done with a Stack, for cleaner code?

- allen zhao August 28, 2020 | 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