HULU Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

public static String[] extractSubstrings(String str) {
        ArrayList<String> list = new ArrayList<String>();
        StringBuffer buffer = new StringBuffer();
        
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '(' || c == ')') {
                if (c == '(' && str.charAt(i+1) == '(') {
                    buffer.append(String.valueOf(c));
                    i++;
                } else if (c == ')' && i + 1 < str.length() && str.charAt(i+1) == ')') {
                    buffer.append(String.valueOf(c));
                    i++;
                } else if (buffer.length() > 0) {
                    list.add(buffer.toString());
                    buffer = new StringBuffer();
                }
            } else if (c == '{' || c == '}') {
                if (c == '{' && str.charAt(i+1) == '{') {
                    buffer.append(String.valueOf(c));
                    i++;
                } else if (c == '}' && i + 1 < str.length() && str.charAt(i+1) == '}') {
                    buffer.append(String.valueOf(c));
                    i++;
                } else if (buffer.length() > 0) {
                    list.add(buffer.toString());
                    buffer = new StringBuffer();
                }
            } else if (c == '[' || c == ']') {
                if (c == '[' && str.charAt(i+1) == '[') {
                    buffer.append(String.valueOf(c));
                    i++;
                } else if (c == ']' && i + 1 < str.length() && str.charAt(i+1) == ']') {
                    buffer.append(String.valueOf(c));
                    i++;
                } else if (buffer.length() > 0) {
                    list.add(buffer.toString());
                    buffer = new StringBuffer();
                }
            } else {
                buffer.append(String.valueOf(c));
            }
        }
        
        if (buffer.length() > 0) {
            list.add(buffer.toString());
        }
        
        return list.toArray(new String[0]);
    }

Better way?

- OctA October 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does in handle error condition ? Is nested string considered invalid e.g abc(def(ghi)jkl)mno ? How is it interpreted ?

- Kaushik Lele October 31, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- intramonk October 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- intramonk October 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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());

}

}

- Tan October 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can there be nested strings ? e.g. (abc(pqr)) ? If yes, how should it be interpreted ?

- Anonymous October 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is nested string considered invalid e.g abc(def(ghi)jkl)mno ? How is it interpreted ?

- Anonymous October 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is nested string considered invalid e.g abc(def(ghi)jkl)mno ? How is it interpreted ?

- Kaushik Lele October 31, 2016 | 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