Amazon Interview Question

Convert a number to an array of characters without using to_s

Interview Answers

Anonymous

Jan 16, 2012

I think the interviewer was looking for some kind of use of modular math to separate the digits out, and then assuming that the string could be represented as an array of ASCII digits.

Anonymous

Jan 22, 2012

Here's an easy to understand version in python. Instead of chr(48 + nNum) you could just do str(nNum) but it can't hurt to demonstrate familiarity with ASCII. def to_string(number): chars = [] while number > 0: nNum = number % 10 number /= 10 chars.append(chr(48 + nNum)) chars.reverse() return chars Which would give something like >>> to_string(34839) ['3', '4', '8', '3', '9']