Epic Systems Interview Question
Country: United States
Iterate through the digits from left to right. When you encounter the broken key digit, decrement it and then turn all the subsequent digits to 9.
But wait! The edge cases are 0 and 9. If your 0 key is broken, then you need to backtrack one digit before doing the decrementing and 9-replacement steps. If your broken key is 9, then you need to use 8 for the subsequent digits.
I would first code this to work for digits 1 through 8, and have a little test suite. Then I'd introduce the 0/9 edge cases, adding tests for those as well.
How about finding the highest base position of the of the key (0,10,100 etc).
Then the result is the sum of the base-1 and ( the digits before the keys in the number -1)*base
Ex: if 1209 is the number and 2 is the key, the highest base position is 100.
So answer is ((1209/100)-1 )* 100 + (100-1) =11*100+99 = 1199.
Only issue is when key is 9. But i put a recursion so that it would solve itself. I believe there should be a more efficient way to accomplish this task.
public int getPosition(int number, int key){
int _num = number;
int _digit = 0;
int _index = 1;
int _position = 0;
while(_num>0){
_digit = num %10;
if(_digit==key)
_position=_index;
_num/=10;
_index*=10;
}
return _position;
}
public int getHighestNumber(int number, int key){
int _position = getPosition(num,key);
int _result;
if(_position==0)
return number;
_result = (_position-1)+(_position*(number/_position -1));
if(key==9)
return getHighestNumber(_result, key);
return result;
}
P.S: I have not tested or compiled this code.
I didn't understand the question.. Can you someone add more details..
- frankabagnale504 October 14, 2013