Problem: Given any integer value, return a sequence using capital alphabets which are unique. For example, 0 changes to A, 1 changes to B, 25 changes to Z, 26 changes to BA, 676 changes to BAA, 17576 changes to BAAA. This could be useful for example if you need to generate short unique names for files.
Question: Will AAA sequence ever be returned? If yes, will ZAA sequence ever be returned? (The answer to one of the questions should be No. If "Yes", ask how will 0 be represented?)
Question: What are the maximum characters needed to support full range for 4 byte long integer, i.e. how long will be the sequence for 2^31-1=2,147,483,647? (The answer should be a small algorithm or quick calculation to evaluate this. Exact answer is 6 characters.)
Solution for numbers up to 25 (1 character). Marks: 10/100
Solution for numbers up to 675 (2 character). Marks: 25/100
Solution for numbers up to 17575 (3 character). Marks: 80/100
Solution for any number. Marks: 100/100
Solution for any number, plus valid answers to both the question. Why are you still continuing the interview? Offer him or her the position. :-)
(Mathematically, the problem is analogous to converting a number from base 10 to base 26. The solution is simpler if thought that way.)
Friday, March 12, 2010
Subscribe to:
Posts (Atom)