Uber interview question

/* Some examples: isMatch(“aa”,”a”) ? false isMatch(“aa”,”aa”) ? true isMatch(“aaa”,”aa”) ? false isMatch(“aa”, “a*”) ? true isMatch(“aa”, “.*”) ? true isMatch(“ab”, “.*”) ? true isMatch(“aab”, “c*a*b”) ? true Write isMatch() */

Interview Answers

Anonymous

16 Nov 2015

public boolean isMatch(String s, String p) { if(p.length() == 0) return s.length() == 0; //p's length 1 is special case if(p.length() == 1 || p.charAt(1) != '*'){ if(s.length() < 1 || (p.charAt(0) != '.' && s.charAt(0) != p.charAt(0))) return false; return isMatch(s.substring(1), p.substring(1)); }else{ int len = s.length(); int i = -1; while(i

2

Anonymous

17 Dec 2015

#10 on leetcode

2

Anonymous

15 Feb 2016

// This is the text editor interface. // Anything you type or change here will be seen by the other person in real time. import java.util.Scanner; class Main { public static void main(String args[]){ Scanner in = new Scanner(System.in); String text, pattern; String sTrue = "true", sFalse = "false"; while(in.hasNext()){ text = in.next(); pattern = in.next(); boolean matched = isMatch(text,pattern); String result = matched ? sTrue : sFalse; System.out.printf("isMatch(\"%s\", \"%s\") = %s%n",text,pattern,result); } } static boolean isMatch(String text, String pattern){ return text.matches(pattern); } }

Anonymous

5 Nov 2015

/* Given a string that contains only digits 0-9 and a target value, print out all possibilities to add binary operators + and - between the digits so they evaluate to the target value. "123", 6 -> ["1+2+3"] "105", 5 -> ["10-5"] */ List res; public List addOperators(String num, int target) { res = new ArrayList(); helper(num, target, "", 0, 0); return res; } private void helper(String num, int target, String tmp, long currRes, long prevNum){ if(currRes == target && num.length() == 0){ String exp = new String(tmp); res.add(exp); return; } for(int i = 1; i 1 && currStr.charAt(0) == '0'){ return; } long currNum = Long.parseLong(currStr); String next = num.substring(i); if(tmp.length() != 0){ helper(next, target, tmp+"+"+currNum, currRes + currNum, currNum); helper(next, target, tmp+"-"+currNum, currRes - currNum, -currNum); } else { helper(next, target, currStr, currNum, currNum); } }