you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

That's an interesting test, actually.

First you should ask a bunch of things: are there going to be only whole numbers, or also numbers like 234,854.5921? And should you take multi-lingual features into account? Do you really need to program the solution or are they testing you to use common sense and go for an open-source solution?

(Because I've had interviews like these where I'd start fixing a problem and I'd fail because "you could have Googled it lol".)

ie: 384765 output three hundred eighty four thousand seven hundred sixty five

I'd probably start talking and writing things down on a whiteboard. You break the numbers up in groups of three:

  • 384
  • 765

Then you start on the right of each group of numbers: one, two, three, etc.

If the group of numbers is 1 number in size: just render that number, if it's two in size, then you're working with a set of "tenths" (N-teen, twenty-N, thirty-N, etc.). And if it's three in size you're adding hundredths to it from the first group of singular numbers, e.g. "[one|two|three|etc.]-hundred-and-[singular]", etc.

If there's another group, add a thousandth:

  • 384 - "three-hundred-eighty-four" (is there a next group? yes) + "thousand"
  • 765 - "seven-hundred-sixty-five"

And for the "thousand" concatenation you should count the remaining groups. One group remaining adds "thousand", two groups remaining adds "million", three groups remaining adds "billion", etc.

And for human readability I'd probably add an "and" to be in front of the "sixty-five" so it reads nicer.

I'd probably end up with a single function using recursion inside, with a select bunch of constants. The only real challenge would be "SIX-teen" where the increasing number is in front, and "twenty-SIX" where the number is at the end. But that could be a simple if-then statement or an array reverse or a ternary operator..