## Riverbed Interview Question

Software Engineer / Developers**Country:**United States

**Interview Type:**Written Test

11 always irrespective of the number of digits

00 - 11

090 - 101

0990 - 1001

09990 - 10001

etc.,

The way I saw it,

if the current reading is 12321

then we start at the middle digit, in this case 3

and add 1 to it

then the new number becomes 12421 which is the next smallest palindromic number.

If the number of digits is even like 123321

then we take two middle digits in this case 3 and 3

and add 1 to both which becomes 124421.

In case of 9's we process it a little differently

eg 1239321

here middle digit is 9

so instead of adding 1 to it, we put 0 in that position

and look at next and previous digits of 9 in that number.

In this case they are 3 and 3.

so we can add 1s to both of them, so we put 1s in those places

and then append the 0s to complete the number.

Here final number will be 10100 which gives us palindromic number 1249421

Number 99999... is a special case. Just adding 2 to this number will give us the answer.

Here's the C code for it.

Please let me know if I missed something.

(The code assumes that we get he reading in the form of an array of integers)

```
double palindRead( const int * reading,
const int n ) //size of the array
{
//if the odometer reading is just 1 digit
if(n < 2) {
if(reading[0] == 9) {
return 2;
} else {
return 1;
}
}
int index1, index2;
double result;
index1 = n/2;
if(n%2 != 0) {
index2 = index1;
result = pow(10,(n-index2-1));
} else {
index2 = index1 - 1;
result = pow(10, (n-index2-1)) + pow(10, (n-index1-1));
}
printf("%f\n", result);
printf("%d\n", index1);
printf("%d\n", index2);
while(index1 < n) {
if(reading[index1]+1 < 10
&& reading[index2]+1 < 10) {
break;
} else if(index1 == n-1
&& reading[index1] == 9) {
return 2;
} else {
index2--;
index1++;
result = pow(10, (n-index2-1)) + pow(10, (n-index1-1));
}
}
return result;
}
```

Perhaps as 999999 overflows to 000000 , is the answer 1 ?

- Jay January 25, 2012