HULU Interview Question
Software EngineersCountry: United States
Interview Type: Phone Interview
Python:
CLOSE_TO_OPEN = {'}':'{', ']':'[', ')':'('} # this is reversed - for lookup purposes
OPEN_DELIMITERS = CLOSE_TO_OPEN.values()
CLOSE_DELIMITERS = CLOSE_TO_OPEN.keys()
def transfer_char_buffer_to_words(char_buffer, words):
if len(char_buffer) == 0:
return
words.append(''.join(char_buffer))
del char_buffer[:]
return
def extract_substrings(string):
open_delimiter = False # false or the delimiter active
char_buffer = []
words = []
escape_active = False # bool
for i in range(0, len(string)):
char = string[i]
if escape_active:
escape_active = False
char_buffer.append(char)
continue
if char in CLOSE_DELIMITERS or char in OPEN_DELIMITERS:
if open_delimiter and (char not in CLOSE_DELIMITERS or CLOSE_TO_OPEN[char] != open_delimiter):
# Ruler #3 actually supersedes all - IMPORTANT!!!
# if there's an active delimiter,
# everything other than the closing delimiter is treated as a normal char,
# so, just append and move on!
char_buffer.append(char)
continue
if i+1 < len(string) and string[i+1] == char:
# Rule #2 - escaping
escape_active = True
continue
# Rule #1 - Now we actually deal with the delimiting part
if char in OPEN_DELIMITERS:
transfer_char_buffer_to_words(char_buffer, words)
open_delimiter = char
elif char in CLOSE_DELIMITERS:
if not open_delimiter:
# ERROR? question doesn't talk about this case.
# But if I had to guess - it should be just treated as a non-delimiter
char_buffer.append(char)
continue
transfer_char_buffer_to_words(char_buffer, words)
open_delimiter = False
else:
char_buffer.append(char)
# don't forget what's left in the buffer!
transfer_char_buffer_to_words(char_buffer, words)
return words
Python:
CLOSE_TO_OPEN = {'}':'{', ']':'[', ')':'('} # this is reversed - for lookup purposes
OPEN_DELIMITERS = CLOSE_TO_OPEN.values()
CLOSE_DELIMITERS = CLOSE_TO_OPEN.keys()
def transfer_char_buffer_to_words(char_buffer, words):
if len(char_buffer) == 0:
return
words.append(''.join(char_buffer))
del char_buffer[:]
return
def extract_substrings(string):
open_delimiter = False # false or the delimiter active
char_buffer = []
words = []
escape_active = False # bool
for i in range(0, len(string)):
char = string[i]
if escape_active:
escape_active = False
char_buffer.append(char)
continue
if char in CLOSE_DELIMITERS or char in OPEN_DELIMITERS:
if open_delimiter and (char not in CLOSE_DELIMITERS or CLOSE_TO_OPEN[char] != open_delimiter):
# Ruler #3 actually supersedes all - IMPORTANT!!!
# if there's an active delimiter,
# everything other than the closing delimiter is treated as a normal char,
# so, just append and move on!
char_buffer.append(char)
continue
if i+1 < len(string) and string[i+1] == char:
# Rule #2 - escaping
escape_active = True
continue
# Rule #1 - Now we actually deal with the delimiting part
if char in OPEN_DELIMITERS:
transfer_char_buffer_to_words(char_buffer, words)
open_delimiter = char
elif char in CLOSE_DELIMITERS:
if not open_delimiter:
# ERROR? question doesn't talk about this case.
# But if I had to guess - it should be just treated as a non-delimiter
char_buffer.append(char)
continue
transfer_char_buffer_to_words(char_buffer, words)
open_delimiter = False
else:
char_buffer.append(char)
# don't forget what's left in the buffer!
transfer_char_buffer_to_words(char_buffer, words)
return words
public class DelimiterString {
public static List<String> getDimitedOutput(String s){
if(s==null || s.length() ==0) return null;
int index =0;
boolean haveParent = false;
char lastDelimiter = ' ';
List<String> list =new ArrayList<String>();
StringBuilder sb = new StringBuilder();
for(char ch:s.toCharArray()){
if(ch=='{' || ch=='(' || ch=='[' || ch=='}'|| ch==')'|| ch==']'){
if(s.length()-1>index && s.charAt(index+1) == ch) {
sb.append(ch);
index= index+2;
continue;
}
if(ch=='{' && lastDelimiter=='(') {
haveParent = true;
sb.append(ch);
index++;
continue;
}
if(ch=='}' && haveParent){
sb.append(ch);
index++;
continue;
}
lastDelimiter= ch;
haveParent= false;
list.add(sb.toString());
sb = new StringBuilder();
index++;
continue;
}
sb.append(ch);
index++;
}
if(sb.toString().length()!=0) list.add(sb.toString());
return list;
}
public static void main(String[] args) {
List<String> result = DelimiterString.getDimitedOutput("abc((g{h}j)ujj");
System.out.println(result.toString());
}
}
Better way?
- OctA October 29, 2016