Turing Software Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Create a map of num[1] = "one" ...
num[3] = "three"
......
num[9]
num[100]="
num[1000] =
while(num
{
print num[i%10];
}
Is there a dumber solution than this to the problem above?
First - Do you realize what memory you'll use if the number is Int64.MaxValue (9,223,372,036,854,775,807) ??
Second - In your case 111 will return "one hundred ten one" ??
We used so solve these kind of problems in high school.
You need an array (or a big switch block) which will contain:
all strings up to 20 (to take care of "teen"s.
Then you'll need to compose the string from right to left.
I'll try to add the code later, time allowing.
The trick is to implement PrintInt(int num) to work on the number range [999,...,0]. Then keep reuse this code on every 10^3 such as Million and Thousand (Please see PrintIntWrp( ) the wrapper function).
Similarly for PrintInt(0<=num<=999) will implement by keep dividing by ten and try to print the num%10. We will work this backward until deduct num down to zero; The special cases, of cause, is the "teens" and "tys" which the tricks in arrays w[], z[] plus the implementation in PrintIntImp(int num) will address this requirement.
- Anonymous November 09, 2011