Uber Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

Here's a backtracking solution written in C#:

public class WordSegment
    {
        public static HashSet<string> dict;
        public static bool TryToSegment(string s)
        {
            if (dict.Contains(s))
                return true;
            
            int end = 1;

            while (end <= s.Length)
            {
                if (dict.Contains(s.Substring(0, end)))
                {
                    bool success = true;
                    if (end < s.Length)
                    {
                        success = TryToSegment(s.Substring(end));
                    }

                    if (success)
                        return true;
                }                                
                end++;
            }
            return false;
        }
    }

- swerddogg May 12, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

//ZoomBA : Imperative Style , almost 
def part_word( word , dictionary ){
  len = #|word| 
  for ( n : [ 0 : 2 ** ( len-1) ] ){
    bm = str(n,2)
    bm = '0' ** ( len -  #|bm| -1 ) + bm 
    println( bm )
    last = len - 1 
    start = 0 
    splits = list()
    for ( i = len-2 ; i >=0 ; i -= 1){
       if ( bm[i] == '1' ){  
          splits += word [ i + 1: last ]
          last = i  
       }
    }
    splits += word[ start : last ]
    println ( splits )
    successful = !exists(splits) :: { ! ( $.o @ dictionary ) }
    if ( successful ) return true  
  }
  return false 
}

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

Dictionary Tree version

class Node{
    boolean isWord = false;
    HashMap<Character, Node> children = new HashMap<>();
}

class DictTree{
    private Node root = new Node();
    private int maxLen = 0;

    void addWord(String s){
        Node cur = root;
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(!cur.children.containsKey(ch)){
                Node newNode = new Node();
                cur.children.put(ch, newNode);
                cur = newNode;
            }
            else{
                cur = cur.children.get(ch);
            }
        }
        cur.isWord = true;
        if(s.length() > maxLen) {
            maxLen = s.length();
        }
    }

    List<Integer> getPrefix(String s){
        List<Integer> ret = new ArrayList<>();
        int len = Math.min(s.length(), maxLen);
        Node cur = root;
        for(int i = 0; i < len; i++){
            char ch = s.charAt(i);
            if(!cur.children.containsKey(ch)){
                return ret;
            }
            cur = cur.children.get(ch);
            if(cur.isWord){
                ret.add(i + 1);
            }
        }
        return ret;
    }
}

class Solution {
    public boolean tryToSegment(String s, List<String> dict) {
        if(s.length() == 0){
            return false;
        }
        DictTree dt = new DictTree();
        for (String str : dict) {
            dt.addWord(str);
        }
        return tryToSegment2(s, dt);
    }

    private boolean tryToSegment2(String s, DictTree dt){
        if(s.length() == 0) return true;
        List<Integer> ret = dt.getPrefix(s);
        for(Integer i : ret){
            if(tryToSegment2(s.substring(i), dt)){
                return true;
            }
        }
        return false;
    }
}

- williamni May 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

s = "leetcode"
dict = ["leet", "code"]
def segment(s, dict):
    n = len(s)
    for i in xrange(1, len(s), 1):
        if s[0:i] in dict and s[i:n] in dict:
            return True
print segment(s, dict)

- Indraja.Punna June 11, 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