Two Sigma Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
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
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))
- Deepika August 30, 2020