## Google Interview Question

Software Engineers**Country:**United States

**Interview Type:**In-Person

```
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));
}
```

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".

'''

- aonecoding January 05, 2018