Turing Software Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

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.

String x[] = {"","ty","hundred","thousand","million","billion"};
	String y[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
	String z[] = {"ten","eleven","twelve","thirteen","fourteen","fifteen","sizteen","seventeen","eighteen","nineteen"};
	String w[] = {"","twen","thir","for","fif","six","seven","eigh","nin",""};
	
	
	protected stringtest (){}
	public static stringtest instance(){
		return new stringtest();
	}
	public void printIntWrp(int num){
		StringBuffer buf = new StringBuffer();
		int index = (num>999?3:0);
		while(num > 0){
			String temp = num>999?x[index++]+" ":"";
			temp = temp+printInt(num % 1000);
			buf.insert(0, temp);
			num /= 1000;
		}
		System.out.print(buf.toString());
	}
	
	protected StringBuffer printInt(int num){ //will work for 999,...,0
		StringBuffer bf = new StringBuffer();
		int lak = 0;
		while(num > 0){
			printIntImp(num,bf,lak++);
			num /= 10;
		}
		//System.out.print(bf.toString());
		return bf;
	}
	
	protected void printIntImp(int num, StringBuffer bs, int lak){
		//base case
		if(lak == 0){
			if((num / 10 % 10) == 1) // 10,11,...,19
				bs.append(z[num%10]);
			else
				bs.append(y[num%10-1]);
			return;
		}						
		if(lak == 1) // 10,11,...,19 <already taken care of>
		{
			if(num % 10 != 1) //skip 10,11,...,19
			{
				bs.insert(0, (w[num%10-1]+x[lak])+" ");
			}else;
		}else{
			bs.insert(0, (y[num%10-1]+x[lak])+" ");
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		stringtest st = stringtest.instance();
		st.printIntWrp(122817);
	}

- Anonymous November 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanx dude ...gud soln!!

- jai November 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are you sure it works for 101? Actually to any number formed X0Y?
And for 0? [OK, this is easy to add]

- Selmeczy, Péter December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Create a map of num[1] = "one" ...
num[3] = "three"
......
num[9]
num[100]="
num[1000] =
while(num
{
print num[i%10];
}

- Anonymous November 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Adrian November 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you paid attention he is not proposing to create a array element for each number only key ones...

- Anonymous November 08, 2011 | Flag


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