Uber Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for coaching on interview preparation?
Visit AONECODE.COM for ONE-TO-ONE private lessons by FB, Google and Uber engineers!

System Design (for candidates of FB, LinkedIn, AMZ, Google and Uber etc)
Algorithms (DP, Greedy, Graph etc. advanced algorithms and clean coding)
Interview questions sorted by companies
Mock Interviews

Ace G, U, FB, Amazon, LinkedIn, MS and other top-tier interviews in weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!


SOLUTION:

final String[] largeNumbers = new String[] {""," thousand"," million"," billion"," trillion"," quintillion"};//...etc
    final String[] digits = new String[] {"", " one", " two", " three", " four", " five", " six"," seven"," eight", " nine"};
    final String[] tens = new String[] {"", " ten"," twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"};
    final String[] teens = new String[] {" ten", " eleven"," twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen"," eighteen"," nineteen"};

    String digitsToEnglish(String num) {
        if(num.length() == 1) {
            return digits[Integer.parseInt(num)];
        }
        int len = num.length();
        StringBuilder english = new StringBuilder();
        int largeNumIdx = 0, tripletIdx = 0;
        while(tripletIdx < len) {
            StringBuilder triplet = new StringBuilder();
            if(len > tripletIdx) {
                triplet.append(num.charAt(len - 1 - tripletIdx));
            }
            if(len > tripletIdx + 1) {
                triplet.insert(0, num.charAt(len - 1 - tripletIdx - 1));
            }
            if(len > tripletIdx + 2) {
                triplet.insert(0, num.charAt(len - 1 - tripletIdx - 2));
            }
            if(Integer.parseInt(triplet.toString()) != 0) {
                StringBuilder current = new StringBuilder();
                if (triplet.length() == 3 && triplet.charAt(0) != '0') {
                    //current.append(' ');
                    current.append(digits[triplet.charAt(0) - '0']);
                    current.append(' ');
                    current.append("hundred");
                }
                if (triplet.length() >= 2 && triplet.charAt(triplet.length() - 2) == '1') {
                    //current.append(' ');
                    current.append(teens[triplet.charAt(triplet.length() - 1) - '0']);
                } else {
                    if (triplet.length() >= 2) {
                        //current.append(' ');
                        current.append(tens[triplet.charAt(triplet.length() - 2) - '0']);
                    }
                    //current.append(' ');
                    current.append(digits[triplet.charAt(triplet.length() - 1) - '0']);
                }
                //current.append(' ');
                current.append(largeNumbers[largeNumIdx]);
                english.insert(0, current);
            }
            largeNumIdx += 1;
            tripletIdx += 3;
        }
        return english.toString();
    }

- aonecoding February 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'''
479
xyz

x - hundred
y - ninety, eighty, seventy, ..., twenty, 
z - 

nineteen, eighteen, seventeen, ..., twelve, eleven, ten
nine, eight, seven, six, ..., two, one
'''
DIGIT_MAP = {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven',
    8: 'eight',
    9: 'nine'
}

DOUBLES_MAP = {
    2: 'twenty',
    3: 'thirty',
    4: 'fourty',
    5: 'fifty',
    6: 'sixty',
    7: 'seventy',
    8: 'eighty',
    9: 'ninety'
}

TEENS_MAP = {
    10: 'ten',
    11: 'eleven',
    12: 'twelve',
    13: 'thirteen',
    14: 'fourteen',
    15: 'fifteen',
    16: 'sixteen',
    17: 'seventeen',
    18: 'eighteen',
    19: 'nineteen'
}

def hundreds_group_to_words(num, groups, suffix=None):
    if num == 0:
        return

    x = (num // 100) % 10
    y = (num // 10) % 10
    z = (num // 1) % 10

    group = []
    if x > 0:
        group.append(DIGIT_MAP[x])
        group.append('hundred')

    if y > 0:
        if y > 1:
            group.append(DOUBLES_MAP[y])
            if z > 1:
                group.append(DIGIT_MAP[z])
        else:
            group.append(TEENS_MAP[y * 10 + z])
    elif z > 0:
        group.append(DIGIT_MAP[z])

    if suffix is not None:
        group.append(suffix)

    groups.append(group)

def digits_to_words(s):
    # 0 <= num < 1000000000000 (1 trillion)
    n = len(s)
    assert n > 0, f"s must be non-empty: {s}"
    if s[0] == '-':
        n -= 1
        negative = True
    else:
        negative = False
    assert n < 13, f"s must be less than 1000000000000: {s}"
    num = -int(s) if negative else int(s)
    groups = []

    # Pre-thousands
    pre_k = num % 1000
    hundreds_group_to_words(pre_k, groups)

    # Pre-millions
    num //= 1000
    pre_m = num % 1000
    hundreds_group_to_words(pre_m, groups, 'thousand')

    # Pre-billions
    num //= 1000
    pre_g = num % 1000
    hundreds_group_to_words(pre_g, groups, 'million')

    # Pre-trillions
    num //= 1000
    pre_t = num % 1000
    hundreds_group_to_words(pre_t, groups, 'billion')

    if negative:
        groups.append(['negative'])

    # for group in groups[::-1]:
    #     print(group)
    #     tmp = ' '.join(group)
    #     print(tmp.__repr__())

    return ' '.join(' '.join(group) for group in groups[::-1])


print(digits_to_words('-100904'))
print(digits_to_words('1234567890'))

- asdf March 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'''
479
xyz

x - hundred
y - ninety, eighty, seventy, ..., twenty, 
z - 

nineteen, eighteen, seventeen, ..., twelve, eleven, ten
nine, eight, seven, six, ..., two, one
'''
DIGIT_MAP = {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven',
    8: 'eight',
    9: 'nine'
}

DOUBLES_MAP = {
    2: 'twenty',
    3: 'thirty',
    4: 'fourty',
    5: 'fifty',
    6: 'sixty',
    7: 'seventy',
    8: 'eighty',
    9: 'ninety'
}

TEENS_MAP = {
    10: 'ten',
    11: 'eleven',
    12: 'twelve',
    13: 'thirteen',
    14: 'fourteen',
    15: 'fifteen',
    16: 'sixteen',
    17: 'seventeen',
    18: 'eighteen',
    19: 'nineteen'
}

def hundreds_group_to_words(num, groups, suffix=None):
    if num == 0:
        return

    x = (num // 100) % 10
    y = (num // 10) % 10
    z = (num // 1) % 10

    group = []
    if x > 0:
        group.append(DIGIT_MAP[x])
        group.append('hundred')

    if y > 0:
        if y > 1:
            group.append(DOUBLES_MAP[y])
            if z > 1:
                group.append(DIGIT_MAP[z])
        else:
            group.append(TEENS_MAP[y * 10 + z])
    elif z > 0:
        group.append(DIGIT_MAP[z])

    if suffix is not None:
        group.append(suffix)

    groups.append(group)

def digits_to_words(s):
    # 0 <= num < 1000000000000 (1 trillion)
    n = len(s)
    assert n > 0, f"s must be non-empty: {s}"
    if s[0] == '-':
        n -= 1
        negative = True
    else:
        negative = False
    assert n < 13, f"s must be less than 1000000000000: {s}"
    num = -int(s) if negative else int(s)
    groups = []

    # Pre-thousands
    pre_k = num % 1000
    hundreds_group_to_words(pre_k, groups)

    # Pre-millions
    num //= 1000
    pre_m = num % 1000
    hundreds_group_to_words(pre_m, groups, 'thousand')

    # Pre-billions
    num //= 1000
    pre_g = num % 1000
    hundreds_group_to_words(pre_g, groups, 'million')

    # Pre-trillions
    num //= 1000
    pre_t = num % 1000
    hundreds_group_to_words(pre_t, groups, 'billion')

    if negative:
        groups.append(['negative'])

    # for group in groups[::-1]:
    #     print(group)
    #     tmp = ' '.join(group)
    #     print(tmp.__repr__())

    return ' '.join(' '.join(group) for group in groups[::-1])


print(digits_to_words('-100904'))
print(digits_to_words('1234567890'))

- closingtime March 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stack<String> w = new Stack<String>();
		//int n = 55; // 110111
		//int n = 53; // 110101
		int n = 54; // 110110
		//int n = 52; // 110100

		int thousands = (n&((~(0xFFFFFFFF<<3))<<3))>>3;
		int hundreds = n&(~(0xFFFFFFFF<<3));
		
		if((hundreds&(1<<1)) > 0){
			if((hundreds&1) > 0)
				w.push("eleven");
			else
				w.push("ten");
		}else {
			if((hundreds&1) > 0)
				w.push("one");
		}
		if((hundreds&(1<<2)) > 0) {
			w.push("one hundred");
		}
		
		if(thousands >0) {
			w.push("and");
			if((thousands&(1<<1)) > 0){
				if((thousands&1) > 0)
					w.push("eleven thousand");
				else
					w.push("ten thousand");
			}else {
				if((thousands&1) > 0)
					w.push("one thousand");
			}
			if((thousands&(1<<2)) > 0) {
				w.push("one hundred");
			}
		}
		
		while(!w.empty())
			System.out.print(w.pop() + " ");

- lareinev April 28, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

func convertToString(input: Int) -> String {
    let number = NSNumber(integerLiteral: input)
    let numberFormatter = NumberFormatter()
    numberFormatter.numberStyle = .spellOut
    return numberFormatter.string(from: number) ?? ""

}

- Anonymous May 25, 2018 | 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