Uber Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 3 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


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