Interview Question for SDE-2s


Country: United States




Comment hidden because of low score. Click to expand.
1
of 1 vote

Here you go. The algorithm is pretty neat, but recursive. I don't like the recursion, but - it should work well.

/*
Should be pretty neat, 
Taking a number and making it word by word 
Using recursion, next step - removing that  
*/
_names_ = [[1,'one'], [2,'two' ], [3,'three'],[ 4 , 'four' ] , [5,'five'] ,
          [6,'six'], [7,'seven' ], [8,'eight'],[ 9 , 'nine' ] , [10,'ten'],
          [11,'eleven'], [12,'twelve' ], [13,'thirteen'],[ 14 , 'fourteen' ] , [15,'fifteen'],
          [16,'sixteen'], [17,'seventeen' ], [18,'eighteen'],[ 19 , 'nineteen' ] ,
          [20,'twenty'], [30,'thirty'], [40,'forty'], [50,'fifty'], [60,'sixty'], [70,'seventy'],
          [80,'eighty'], [90,'ninety'], [100,'hundred'] , [1000, 'thousand' ] ,
          [ 1000000 , 'million' ], [1000000000, 'billion' ] , [1000000000000 , 'trillion' ] ]

_keys_ = dict( _names_ )          
def in_words( n ){
  if ( n == 0 ) return 'zero'
  // try to find the index of the item from the right side 
  // which has the property that division by that item > 0
  i = rindex( _names_ ) :: { (n / $.0 ) > 0 }
  // i would have the index , get result and reminder 
  res = (n / _names_[i][0] )
  rem = (n % _names_[i][0] )
  // recursion, string rep of result is given by ... 
  s_res = ( res @ _keys_ ) ? ( (res != 1 || n >=100 ) ? _keys_[res] : '' ) : in_words(res)
  // recursion, string rep of the reminder is given by ... 
  s_rem = ( rem @ _keys_ ) ? _keys_[rem] : (rem != 0 ? in_words(rem) : '' )
  // finally merge them, and we have our answer - careful not to merge empty strings 
  s_res + (empty(s_res)?'' : ' ')  + _names_[i][1] + (empty(s_rem)?'' : ' ') + s_rem
}

println( in_words(0) )
println( in_words(10) )
println( in_words(1012) )
println( in_words(983) )
println( in_words(87891729817981987) )
println( in_words(4312112121121) )

The results of course are:

zero
ten
one thousand twelve
nine hundred eighty three
eighty seven thousand eight hundred ninety one trillion seven hundred twenty nine billion eight hundred seventeen million nine hundred eigh
ty one thousand nine hundred eighty seven
four trillion three hundred twelve billion one hundred twelve million one hundred twenty one thousand one hundred twenty one

- NoOne June 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void init(){
		
		map.put(0, "Zero");
		map.put(1, "One");
		map.put(2, "Two");
		map.put(3, "Three");
		map.put(4, "Four");
		map.put(5, "Five");
		map.put(6, "Six");
		map.put(7, "Seven");
		map.put(8, "Eight");
		map.put(9, "Nine");
		
		map.put(11, "Eleven");
		map.put(12, "Twelve");
		map.put(13, "Thirteen");
		map.put(14, "Fourteen");
		map.put(15, "Fifteen");
		map.put(16, "Sixteen");
		map.put(17, "Seventeen");
		map.put(18, "Eighteen");
		map.put(19, "Nineteen");
		
		map.put(10, "Ten");
		map.put(20, "Twenty");
		map.put(30, "Thirty");
		map.put(40, "Fourty");
		map.put(50, "Fifty");
		map.put(60, "Sixty");
		map.put(70, "Seventy");
		map.put(80, "Eightty");
		map.put(90, "Ninety");
		
		map.put(100, "Hundred and");
		map.put(1000, "Thousand");
		map.put(100000, "Lacks");
	}
	
	static String convert(int number, int divisor){
		
//		Single digit - return
		if(number < 10) { return map.get(number); }
		
//		Double digits
		if(number < 100) { return map.get(number - (number%10)) + " " + map.get(number%10); }
		
		while(number/ divisor > 9){
			divisor *= 10;
		}
		
		String currentString = "";
		if(!map.containsKey(divisor)){ currentString = map.get(Integer.valueOf(number/divisor + "0")); }
		else { currentString = map.get(number/divisor) + " " + map.get(divisor); }
		
		return currentString + " " + convert(number%divisor, 1);

	}

- Anonymous July 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void init(){
		
		map.put(0, "Zero");
		map.put(1, "One");
		map.put(2, "Two");
		map.put(3, "Three");
		map.put(4, "Four");
		map.put(5, "Five");
		map.put(6, "Six");
		map.put(7, "Seven");
		map.put(8, "Eight");
		map.put(9, "Nine");
		
		map.put(11, "Eleven");
		map.put(12, "Twelve");
		map.put(13, "Thirteen");
		map.put(14, "Fourteen");
		map.put(15, "Fifteen");
		map.put(16, "Sixteen");
		map.put(17, "Seventeen");
		map.put(18, "Eighteen");
		map.put(19, "Nineteen");
		
		map.put(10, "Ten");
		map.put(20, "Twenty");
		map.put(30, "Thirty");
		map.put(40, "Fourty");
		map.put(50, "Fifty");
		map.put(60, "Sixty");
		map.put(70, "Seventy");
		map.put(80, "Eightty");
		map.put(90, "Ninety");
		
		map.put(100, "Hundred and");
		map.put(1000, "Thousand");
		map.put(100000, "Lacks");
	}
	
	static String convert(int number, int divisor){
		
//		Single digit - return
		if(number < 10) { return map.get(number); }
		
//		Double digits
		if(number < 100) { return map.get(number - (number%10)) + " " + map.get(number%10); }
		
		while(number/ divisor > 9){
			divisor *= 10;
		}
		
		String currentString = "";
		if(!map.containsKey(divisor)){ currentString = map.get(Integer.valueOf(number/divisor + "0")); }
		else { currentString = map.get(number/divisor) + " " + map.get(divisor); }
		
		return currentString + " " + convert(number%divisor, 1);

	}

- Deep July 03, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More