Google 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 interview experience sharing and coaching?

Visit AONECODE.COM for private lessons by FB, Google and Uber engineers

Our ONE TO ONE class offers

SYSTEM DESIGN Courses (highly recommended for candidates of FB, LinkedIn, Amazon, Google & Uber etc.)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!

'''
This is sexagesimal addition.
e.g.
Start from the least significant bit (right-hand side).
Check if there is a number bigger than the current digit available.
If yes,
For least significant digit in '15:31', '3' is the next number bigger than '1'.
Then there is no carry to bring forward. Stop adding and return the result '15:33'.

If no,
For example '14:59', nothing is bigger than '9'. Then update this bit with the min number and proceed
to find the number just bigger than the next bit '5'.
It's similar to bringing the carry bit forward in an add operation.

When looking for the number bigger than '5', note that this is a sexagesimal number.
Therefore although '9' is bigger than '5', but it can't be taken since it's over '6'. So set '5' to the min number '1'.
And proceed to the next number '4'.

'4' has a next bigger number '5'. So set it to '5' and the carry stops here.

Result for '14:59' is '15:11".
'''

def nextMoment(time):
    result = list(time)
    numbers = list(time)
    numbers.pop(2)
    numbers = sorted(set(numbers))
    
    result[-1] = findNextNumber(numbers, time[-1], '9')
    if result[-1] >= time[-1]:
        return "".join(result)
    
    result[-2] = findNextNumber(numbers, time[-2], '5')
    if result[-2] > time[-2]:
        return "".join(result)
        
    result[1] = findNextNumber(numbers, time[1], '9')
    if result[1] > time[1]:
        return "".join(result)
        
    result[0] = findNextNumber(numbers, time[0], '5')
    if result[0] >= time[0]:
        return "".join(result)
    return "".join(result)

#find the next available number that's bigger than num.
#if nothing is bigger, then return the smallest number.
def findNextNumber(sorted_arr, num, upper_bound): 
    idx = sorted_arr.index(num) + 1
    if idx >= len(sorted_arr) or sorted_arr[idx] > upper_bound:
        return sorted_arr[0]
    return sorted_arr[idx]

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

static String findNextTime(String time) {
        ArrayList<Integer> arr = new ArrayList<>();
        StringBuilder ret = new StringBuilder();
        int len = time.length();
        for (int i = 0; i < len; i++) {
            char ch = time.charAt(i);
            if (ch == ':') continue;
            int x = ch-'0';
            arr.add(x);
        }
        Collections.sort(arr);
        for (int i = 1; i < arr.size(); i++) {
            if (arr.get(i) == arr.get(i-1)) {
                arr.remove(i);
                i--;
            }
        }
        int sz = arr.size();
        boolean flag = false;
        for (int i = len-1; i >= 0; i--) {
            char ch = time.charAt(i);
            if (flag) {
                ret.insert(0, ch);
            } else {
                if (ch == ':') {
                    ret.insert(0, ch);
                } else {
                    int x = ch-'0';
                    int y = 0;
                    if (i == len-1 || i == 1) y = findNext(x, arr, 9, sz);
                    else if (i == len-2) y = findNext(x, arr, 5, sz);
                    else y = findNext(x, arr, 2, sz);
                    if (y != arr.get(0)) flag = true;
                    ret.insert(0, (char)(y+'0'));
                }
            }
        }
        int check = Integer.parseInt(ret.substring(0, 2));
        if (check > 23) {
            ret.deleteCharAt(0);
            ret.deleteCharAt(0);
            ret.insert(0, (char)(arr.get(0) + '0'));
            ret.insert(0, (char)(arr.get(0) + '0'));
        }
        return ret.toString();
    }
    private static int findNext(int x, ArrayList<Integer> arr, int upperLimit, int size) {
        if (x == arr.get(size-1)) return arr.get(0);
        if (x == arr.get(0)) return (arr.get(1) <= upperLimit ? arr.get(1) : arr.get(0));
        if (x == arr.get(1)) return (arr.get(2) <= upperLimit ? arr.get(2) : arr.get(0));
        return (arr.get(3) <= upperLimit ? arr.get(3) : arr.get(0));
    }

- Aim_Google January 10, 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